Search in sources :

Example 6 with Joiner

use of com.google.common.base.Joiner in project jstorm by alibaba.

the class Utils method prettyUptime.

/**
     * seconds to string like '30m 40s' and '1d 20h 30m 40s'
     *
     * @param secs
     * @return
     */
public static String prettyUptime(int secs) {
    String[][] PRETTYSECDIVIDERS = { new String[] { "ms", "1000" }, new String[] { "s", "60" }, new String[] { "m", "60" }, new String[] { "h", "24" }, new String[] { "d", null } };
    int diversize = PRETTYSECDIVIDERS.length;
    LinkedList<String> tmp = new LinkedList<>();
    int div = secs;
    for (int i = 0; i < diversize; i++) {
        if (PRETTYSECDIVIDERS[i][1] != null) {
            Integer d = Integer.parseInt(PRETTYSECDIVIDERS[i][1]);
            tmp.addFirst(div % d + PRETTYSECDIVIDERS[i][0]);
            div = div / d;
        } else {
            tmp.addFirst(div + PRETTYSECDIVIDERS[i][0]);
        }
        if (div <= 0)
            break;
    }
    Joiner joiner = Joiner.on(" ");
    return joiner.join(tmp);
}
Also used : Joiner(com.google.common.base.Joiner) LinkedList(java.util.LinkedList)

Example 7 with Joiner

use of com.google.common.base.Joiner in project flink by apache.

the class StateBackendTestBase method testListState.

@Test
@SuppressWarnings("unchecked,rawtypes")
public void testListState() {
    try {
        CheckpointStreamFactory streamFactory = createStreamFactory();
        AbstractKeyedStateBackend<Integer> backend = createKeyedBackend(IntSerializer.INSTANCE);
        ListStateDescriptor<String> kvId = new ListStateDescriptor<>("id", String.class);
        kvId.initializeSerializerUnlessSet(new ExecutionConfig());
        TypeSerializer<Integer> keySerializer = IntSerializer.INSTANCE;
        TypeSerializer<VoidNamespace> namespaceSerializer = VoidNamespaceSerializer.INSTANCE;
        TypeSerializer<String> valueSerializer = kvId.getElementSerializer();
        ListState<String> state = backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, kvId);
        @SuppressWarnings("unchecked") InternalKvState<VoidNamespace> kvState = (InternalKvState<VoidNamespace>) state;
        Joiner joiner = Joiner.on(",");
        // some modifications to the state
        backend.setCurrentKey(1);
        assertEquals(null, state.get());
        assertEquals(null, getSerializedList(kvState, 1, keySerializer, VoidNamespace.INSTANCE, namespaceSerializer, valueSerializer));
        state.add("1");
        backend.setCurrentKey(2);
        assertEquals(null, state.get());
        assertEquals(null, getSerializedList(kvState, 2, keySerializer, VoidNamespace.INSTANCE, namespaceSerializer, valueSerializer));
        state.add("2");
        backend.setCurrentKey(1);
        assertEquals("1", joiner.join(state.get()));
        assertEquals("1", joiner.join(getSerializedList(kvState, 1, keySerializer, VoidNamespace.INSTANCE, namespaceSerializer, valueSerializer)));
        // draw a snapshot
        KeyGroupsStateHandle snapshot1 = FutureUtil.runIfNotDoneAndGet(backend.snapshot(682375462378L, 2, streamFactory, CheckpointOptions.forFullCheckpoint()));
        // make some more modifications
        backend.setCurrentKey(1);
        state.add("u1");
        backend.setCurrentKey(2);
        state.add("u2");
        backend.setCurrentKey(3);
        state.add("u3");
        // draw another snapshot
        KeyGroupsStateHandle snapshot2 = FutureUtil.runIfNotDoneAndGet(backend.snapshot(682375462379L, 4, streamFactory, CheckpointOptions.forFullCheckpoint()));
        // validate the original state
        backend.setCurrentKey(1);
        assertEquals("1,u1", joiner.join(state.get()));
        assertEquals("1,u1", joiner.join(getSerializedList(kvState, 1, keySerializer, VoidNamespace.INSTANCE, namespaceSerializer, valueSerializer)));
        backend.setCurrentKey(2);
        assertEquals("2,u2", joiner.join(state.get()));
        assertEquals("2,u2", joiner.join(getSerializedList(kvState, 2, keySerializer, VoidNamespace.INSTANCE, namespaceSerializer, valueSerializer)));
        backend.setCurrentKey(3);
        assertEquals("u3", joiner.join(state.get()));
        assertEquals("u3", joiner.join(getSerializedList(kvState, 3, keySerializer, VoidNamespace.INSTANCE, namespaceSerializer, valueSerializer)));
        backend.dispose();
        // restore the first snapshot and validate it
        backend = restoreKeyedBackend(IntSerializer.INSTANCE, snapshot1);
        snapshot1.discardState();
        ListState<String> restored1 = backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, kvId);
        @SuppressWarnings("unchecked") InternalKvState<VoidNamespace> restoredKvState1 = (InternalKvState<VoidNamespace>) restored1;
        backend.setCurrentKey(1);
        assertEquals("1", joiner.join(restored1.get()));
        assertEquals("1", joiner.join(getSerializedList(restoredKvState1, 1, keySerializer, VoidNamespace.INSTANCE, namespaceSerializer, valueSerializer)));
        backend.setCurrentKey(2);
        assertEquals("2", joiner.join(restored1.get()));
        assertEquals("2", joiner.join(getSerializedList(restoredKvState1, 2, keySerializer, VoidNamespace.INSTANCE, namespaceSerializer, valueSerializer)));
        backend.dispose();
        // restore the second snapshot and validate it
        backend = restoreKeyedBackend(IntSerializer.INSTANCE, snapshot2);
        snapshot2.discardState();
        ListState<String> restored2 = backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, kvId);
        @SuppressWarnings("unchecked") InternalKvState<VoidNamespace> restoredKvState2 = (InternalKvState<VoidNamespace>) restored2;
        backend.setCurrentKey(1);
        assertEquals("1,u1", joiner.join(restored2.get()));
        assertEquals("1,u1", joiner.join(getSerializedList(restoredKvState2, 1, keySerializer, VoidNamespace.INSTANCE, namespaceSerializer, valueSerializer)));
        backend.setCurrentKey(2);
        assertEquals("2,u2", joiner.join(restored2.get()));
        assertEquals("2,u2", joiner.join(getSerializedList(restoredKvState2, 2, keySerializer, VoidNamespace.INSTANCE, namespaceSerializer, valueSerializer)));
        backend.setCurrentKey(3);
        assertEquals("u3", joiner.join(restored2.get()));
        assertEquals("u3", joiner.join(getSerializedList(restoredKvState2, 3, keySerializer, VoidNamespace.INSTANCE, namespaceSerializer, valueSerializer)));
        backend.dispose();
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : Joiner(com.google.common.base.Joiner) BlockerCheckpointStreamFactory(org.apache.flink.runtime.util.BlockerCheckpointStreamFactory) ListStateDescriptor(org.apache.flink.api.common.state.ListStateDescriptor) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) InternalKvState(org.apache.flink.runtime.state.internal.InternalKvState) Test(org.junit.Test)

Example 8 with Joiner

use of com.google.common.base.Joiner in project randomizedtesting by randomizedtesting.

the class ExecutionTimesReport method writeHints.

/**
   * Writes back hints file. 
   */
public static void writeHints(File file, Map<String, List<Long>> hints) throws IOException {
    Closer closer = Closer.create();
    try {
        BufferedWriter w = closer.register(Files.newWriter(file, Charsets.UTF_8));
        if (!(hints instanceof SortedMap)) {
            hints = new TreeMap<String, List<Long>>(hints);
        }
        Joiner joiner = Joiner.on(',');
        for (Map.Entry<String, List<Long>> e : hints.entrySet()) {
            w.write(e.getKey());
            w.write("=");
            joiner.appendTo(w, e.getValue());
            w.write("\n");
        }
    } catch (Throwable t) {
        throw closer.rethrow(t);
    } finally {
        closer.close();
    }
}
Also used : Closer(com.google.common.io.Closer) Joiner(com.google.common.base.Joiner) SortedMap(java.util.SortedMap) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap) BufferedWriter(java.io.BufferedWriter)

Example 9 with Joiner

use of com.google.common.base.Joiner in project crate by crate.

the class InsertFromSubQueryAnalyzer method validateColumnsAndAddCastsIfNecessary.

/**
     * validate that result columns from subquery match explicit insert columns
     * or complete table schema
     */
private static void validateColumnsAndAddCastsIfNecessary(List<Reference> targetColumns, QuerySpec querySpec) {
    if (targetColumns.size() != querySpec.outputs().size()) {
        Joiner commaJoiner = Joiner.on(", ");
        throw new IllegalArgumentException(String.format(Locale.ENGLISH, "Number of target columns (%s) of insert statement doesn't match number of source columns (%s)", commaJoiner.join(Iterables.transform(targetColumns, Reference.TO_COLUMN_NAME)), commaJoiner.join(Iterables.transform(querySpec.outputs(), SymbolPrinter.FUNCTION))));
    }
    int failedCastPosition = querySpec.castOutputs(Iterators.transform(targetColumns.iterator(), Symbol::valueType));
    if (failedCastPosition >= 0) {
        Symbol failedSource = querySpec.outputs().get(failedCastPosition);
        Reference failedTarget = targetColumns.get(failedCastPosition);
        throw new IllegalArgumentException(String.format(Locale.ENGLISH, "Type of subquery column %s (%s) does not match is not convertable to the type of table column %s (%s)", failedSource, failedSource.valueType(), failedTarget.ident().columnIdent().fqn(), failedTarget.valueType()));
    }
}
Also used : Joiner(com.google.common.base.Joiner) Symbol(io.crate.analyze.symbol.Symbol) DynamicReference(io.crate.analyze.symbol.DynamicReference)

Example 10 with Joiner

use of com.google.common.base.Joiner in project che by eclipse.

the class FactoryServiceClientImpl method queryString.

/**
     * Forms the query string from collection of query params
     *
     * @param pairs
     *         an iterable collection of query params
     * @return query string
     */
private static String queryString(Collection<Pair<String, String>> pairs) {
    if (pairs.isEmpty()) {
        return "";
    }
    final Joiner joiner = Joiner.on("&");
    final List<String> params = new LinkedList<>();
    for (Pair<String, String> pair : pairs) {
        params.add(pair.first + '=' + pair.second);
    }
    return '?' + joiner.join(params);
}
Also used : Joiner(com.google.common.base.Joiner) LinkedList(java.util.LinkedList)

Aggregations

Joiner (com.google.common.base.Joiner)47 ArrayList (java.util.ArrayList)10 HashMap (java.util.HashMap)6 IOException (java.io.IOException)5 Map (java.util.Map)4 BufferedWriter (java.io.BufferedWriter)3 LinkedHashMap (java.util.LinkedHashMap)3 List (java.util.List)3 ResultSet (com.google.cloud.spanner.ResultSet)2 Statement (com.google.cloud.spanner.Statement)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 DBException (com.yahoo.ycsb.DBException)2 File (java.io.File)2 URI (java.net.URI)2 Iterator (java.util.Iterator)2 LinkedList (java.util.LinkedList)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)2 FileLineDifferenceIterator (org.apache.jackrabbit.oak.commons.FileIOUtils.FileLineDifferenceIterator)2 CConfiguration (co.cask.cdap.common.conf.CConfiguration)1