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());
}
}
use of com.google.common.base.Joiner in project hadoop by apache.
the class AMRMClientImpl method addContainerRequest.
@Override
public synchronized void addContainerRequest(T req) {
Preconditions.checkArgument(req != null, "Resource request can not be null.");
Set<String> dedupedRacks = new HashSet<String>();
if (req.getRacks() != null) {
dedupedRacks.addAll(req.getRacks());
if (req.getRacks().size() != dedupedRacks.size()) {
Joiner joiner = Joiner.on(',');
LOG.warn("ContainerRequest has duplicate racks: " + joiner.join(req.getRacks()));
}
}
Set<String> inferredRacks = resolveRacks(req.getNodes());
inferredRacks.removeAll(dedupedRacks);
// check that specific and non-specific requests cannot be mixed within a
// priority
checkLocalityRelaxationConflict(req.getAllocationRequestId(), req.getPriority(), ANY_LIST, req.getRelaxLocality());
// check that specific rack cannot be mixed with specific node within a
// priority. If node and its rack are both specified then they must be
// in the same request.
// For explicitly requested racks, we set locality relaxation to true
checkLocalityRelaxationConflict(req.getAllocationRequestId(), req.getPriority(), dedupedRacks, true);
checkLocalityRelaxationConflict(req.getAllocationRequestId(), req.getPriority(), inferredRacks, req.getRelaxLocality());
// check if the node label expression specified is valid
checkNodeLabelExpression(req);
if (req.getNodes() != null) {
HashSet<String> dedupedNodes = new HashSet<String>(req.getNodes());
if (dedupedNodes.size() != req.getNodes().size()) {
Joiner joiner = Joiner.on(',');
LOG.warn("ContainerRequest has duplicate nodes: " + joiner.join(req.getNodes()));
}
for (String node : dedupedNodes) {
addResourceRequest(req.getPriority(), node, req.getExecutionTypeRequest(), req.getCapability(), req, true, req.getNodeLabelExpression());
}
}
for (String rack : dedupedRacks) {
addResourceRequest(req.getPriority(), rack, req.getExecutionTypeRequest(), req.getCapability(), req, true, req.getNodeLabelExpression());
}
// corresponding rack
for (String rack : inferredRacks) {
addResourceRequest(req.getPriority(), rack, req.getExecutionTypeRequest(), req.getCapability(), req, req.getRelaxLocality(), req.getNodeLabelExpression());
}
// Off-switch
addResourceRequest(req.getPriority(), ResourceRequest.ANY, req.getExecutionTypeRequest(), req.getCapability(), req, req.getRelaxLocality(), req.getNodeLabelExpression());
}
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()));
}
}
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);
}
use of com.google.common.base.Joiner in project buck by facebook.
the class ReplaceManifestPlaceholdersStep method replacePlaceholders.
@VisibleForTesting
static String replacePlaceholders(String content, ImmutableMap<String, String> placeholders) {
Iterable<String> escaped = Iterables.transform(placeholders.keySet(), Pattern::quote);
Joiner joiner = Joiner.on("|");
String patternString = Pattern.quote("${") + "(" + joiner.join(escaped) + ")" + Pattern.quote("}");
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(content);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, placeholders.get(matcher.group(1)));
}
matcher.appendTail(sb);
return sb.toString();
}
Aggregations