本文整理汇总了Java中org.apache.kafka.streams.state.KeyValueStore类的典型用法代码示例。如果您正苦于以下问题:Java KeyValueStore类的具体用法?Java KeyValueStore怎么用?Java KeyValueStore使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
KeyValueStore类属于org.apache.kafka.streams.state包,在下文中一共展示了KeyValueStore类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: shouldDriveGlobalStore
import org.apache.kafka.streams.state.KeyValueStore; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Test
public void shouldDriveGlobalStore() throws Exception {
final StateStoreSupplier storeSupplier = Stores.create("my-store")
.withStringKeys().withStringValues().inMemory().disableLogging().build();
final String global = "global";
final String topic = "topic";
final TopologyBuilder topologyBuilder = this.builder
.addGlobalStore(storeSupplier, global, STRING_DESERIALIZER, STRING_DESERIALIZER, topic, "processor", define(new StatefulProcessor("my-store")));
driver = new ProcessorTopologyTestDriver(config, topologyBuilder);
final KeyValueStore<String, String> globalStore = (KeyValueStore<String, String>) topologyBuilder.globalStateStores().get("my-store");
driver.process(topic, "key1", "value1", STRING_SERIALIZER, STRING_SERIALIZER);
driver.process(topic, "key2", "value2", STRING_SERIALIZER, STRING_SERIALIZER);
assertEquals("value1", globalStore.get("key1"));
assertEquals("value2", globalStore.get("key2"));
}
示例2: doTable
import org.apache.kafka.streams.state.KeyValueStore; //导入依赖的package包/类
private <K, V> KTable<K, V> doTable(final AutoOffsetReset offsetReset,
final Serde<K> keySerde,
final Serde<V> valSerde,
final TimestampExtractor timestampExtractor,
final String topic,
final StateStoreSupplier<KeyValueStore> storeSupplier,
final boolean isQueryable) {
final String source = newName(KStreamImpl.SOURCE_NAME);
final String name = newName(KTableImpl.SOURCE_NAME);
final ProcessorSupplier<K, V> processorSupplier = new KTableSource<>(storeSupplier.name());
addSource(offsetReset, source, timestampExtractor, keySerde == null ? null : keySerde.deserializer(),
valSerde == null ? null : valSerde.deserializer(),
topic);
addProcessor(name, processorSupplier, source);
final KTableImpl<K, ?, V> kTable = new KTableImpl<>(this, name, processorSupplier,
keySerde, valSerde, Collections.singleton(source), storeSupplier.name(), isQueryable);
addStateStore(storeSupplier, name);
connectSourceStoreAndTopic(storeSupplier.name(), topic);
return kTable;
}
示例3: doFilter
import org.apache.kafka.streams.state.KeyValueStore; //导入依赖的package包/类
private KTable<K, V> doFilter(final Predicate<? super K, ? super V> predicate,
final StateStoreSupplier<KeyValueStore> storeSupplier,
boolean isFilterNot) {
Objects.requireNonNull(predicate, "predicate can't be null");
String name = topology.newName(FILTER_NAME);
String internalStoreName = null;
if (storeSupplier != null) {
internalStoreName = storeSupplier.name();
}
KTableProcessorSupplier<K, V, V> processorSupplier = new KTableFilter<>(this, predicate, isFilterNot, internalStoreName);
topology.addProcessor(name, processorSupplier, this.name);
if (storeSupplier != null) {
topology.addStateStore(storeSupplier, name);
}
return new KTableImpl<>(topology, name, processorSupplier, this.keySerde, this.valSerde, sourceNodes, internalStoreName, internalStoreName != null);
}
示例4: doMapValues
import org.apache.kafka.streams.state.KeyValueStore; //导入依赖的package包/类
private <V1> KTable<K, V1> doMapValues(final ValueMapper<? super V, ? extends V1> mapper,
final Serde<V1> valueSerde,
final StateStoreSupplier<KeyValueStore> storeSupplier) {
Objects.requireNonNull(mapper);
String name = topology.newName(MAPVALUES_NAME);
String internalStoreName = null;
if (storeSupplier != null) {
internalStoreName = storeSupplier.name();
}
KTableProcessorSupplier<K, V, V1> processorSupplier = new KTableMapValues<>(this, mapper, internalStoreName);
topology.addProcessor(name, processorSupplier, this.name);
if (storeSupplier != null) {
topology.addStateStore(storeSupplier, name);
return new KTableImpl<>(topology, name, processorSupplier, this.keySerde, valueSerde, sourceNodes, internalStoreName, true);
} else {
return new KTableImpl<>(topology, name, processorSupplier, sourceNodes, this.queryableStoreName, false);
}
}
示例5: createKeyValueStore
import org.apache.kafka.streams.state.KeyValueStore; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Override
protected <K, V> KeyValueStore<K, V> createKeyValueStore(
ProcessorContext context,
Class<K> keyClass,
Class<V> valueClass,
boolean useContextSerdes) {
StateStoreSupplier supplier;
if (useContextSerdes) {
supplier = Stores.create("my-store").withKeys(context.keySerde()).withValues(context.valueSerde()).inMemory().maxEntries(10).build();
} else {
supplier = Stores.create("my-store").withKeys(keyClass).withValues(valueClass).inMemory().maxEntries(10).build();
}
KeyValueStore<K, V> store = (KeyValueStore<K, V>) supplier.get();
store.init(context, store);
return store;
}
示例6: createKeyValueStore
import org.apache.kafka.streams.state.KeyValueStore; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Override
protected <K, V> KeyValueStore<K, V> createKeyValueStore(final ProcessorContext context,
final Class<K> keyClass,
final Class<V> valueClass,
final boolean useContextSerdes) {
final Stores.PersistentKeyValueFactory<?, ?> factory;
if (useContextSerdes) {
factory = Stores
.create("my-store")
.withKeys(context.keySerde())
.withValues(context.valueSerde())
.persistent();
} else {
factory = Stores
.create("my-store")
.withKeys(keyClass)
.withValues(valueClass)
.persistent();
}
final KeyValueStore<K, V> store = (KeyValueStore<K, V>) factory.build().get();
store.init(context, store);
return store;
}
示例7: newKeyValueStore
import org.apache.kafka.streams.state.KeyValueStore; //导入依赖的package包/类
public static <K, V> KeyValueStore<K, V> newKeyValueStore(final String name,
final String applicationId,
final Class<K> keyType,
final Class<V> valueType) {
final InMemoryKeyValueStoreSupplier<K, V> supplier = new InMemoryKeyValueStoreSupplier<>(name,
null,
null,
new MockTime(),
false,
Collections.<String, String>emptyMap());
final StateStore stateStore = supplier.get();
stateStore.init(
new MockProcessorContext(
StateSerdes.withBuiltinTypes(
ProcessorStateManager.storeChangelogTopic(applicationId, name),
keyType,
valueType),
new NoOpRecordCollector()),
stateStore);
return (KeyValueStore<K, V>) stateStore;
}
示例8: shouldSupportRangeAcrossMultipleKVStores
import org.apache.kafka.streams.state.KeyValueStore; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Test
public void shouldSupportRangeAcrossMultipleKVStores() throws Exception {
final KeyValueStore<String, String> cache = newStoreInstance();
stubProviderTwo.addStore(storeName, cache);
stubOneUnderlying.put("a", "a");
stubOneUnderlying.put("b", "b");
stubOneUnderlying.put("z", "z");
cache.put("c", "c");
cache.put("d", "d");
cache.put("x", "x");
final List<KeyValue<String, String>> results = toList(theStore.range("a", "e"));
assertTrue(results.contains(new KeyValue<>("a", "a")));
assertTrue(results.contains(new KeyValue<>("b", "b")));
assertTrue(results.contains(new KeyValue<>("c", "c")));
assertTrue(results.contains(new KeyValue<>("d", "d")));
assertEquals(4, results.size());
}
开发者ID:YMCoding,项目名称:kafka-0.11.0.0-src-with-comment,代码行数:22,代码来源:CompositeReadOnlyKeyValueStoreTest.java
示例9: shouldSupportAllAcrossMultipleStores
import org.apache.kafka.streams.state.KeyValueStore; //导入依赖的package包/类
@Test
public void shouldSupportAllAcrossMultipleStores() throws Exception {
final KeyValueStore<String, String> cache = newStoreInstance();
stubProviderTwo.addStore(storeName, cache);
stubOneUnderlying.put("a", "a");
stubOneUnderlying.put("b", "b");
stubOneUnderlying.put("z", "z");
cache.put("c", "c");
cache.put("d", "d");
cache.put("x", "x");
final List<KeyValue<String, String>> results = toList(theStore.all());
assertTrue(results.contains(new KeyValue<>("a", "a")));
assertTrue(results.contains(new KeyValue<>("b", "b")));
assertTrue(results.contains(new KeyValue<>("c", "c")));
assertTrue(results.contains(new KeyValue<>("d", "d")));
assertTrue(results.contains(new KeyValue<>("x", "x")));
assertTrue(results.contains(new KeyValue<>("z", "z")));
assertEquals(6, results.size());
}
开发者ID:YMCoding,项目名称:kafka-0.11.0.0-src-with-comment,代码行数:23,代码来源:CompositeReadOnlyKeyValueStoreTest.java
示例10: createKeyValueStore
import org.apache.kafka.streams.state.KeyValueStore; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Override
protected <K, V> KeyValueStore<K, V> createKeyValueStore(
ProcessorContext context,
Class<K> keyClass,
Class<V> valueClass,
boolean useContextSerdes) {
StateStoreSupplier supplier;
if (useContextSerdes) {
supplier = Stores.create("my-store").withKeys(context.keySerde()).withValues(context.valueSerde()).inMemory().build();
} else {
supplier = Stores.create("my-store").withKeys(keyClass).withValues(valueClass).inMemory().build();
}
KeyValueStore<K, V> store = (KeyValueStore<K, V>) supplier.get();
store.init(context, store);
return store;
}
示例11: testDrivingStatefulTopology
import org.apache.kafka.streams.state.KeyValueStore; //导入依赖的package包/类
@Test
public void testDrivingStatefulTopology() {
String storeName = "entries";
driver = new ProcessorTopologyTestDriver(config, createStatefulTopology(storeName));
driver.process(INPUT_TOPIC_1, "key1", "value1", STRING_SERIALIZER, STRING_SERIALIZER);
driver.process(INPUT_TOPIC_1, "key2", "value2", STRING_SERIALIZER, STRING_SERIALIZER);
driver.process(INPUT_TOPIC_1, "key3", "value3", STRING_SERIALIZER, STRING_SERIALIZER);
driver.process(INPUT_TOPIC_1, "key1", "value4", STRING_SERIALIZER, STRING_SERIALIZER);
assertNoOutputRecord(OUTPUT_TOPIC_1);
KeyValueStore<String, String> store = driver.getKeyValueStore("entries");
assertEquals("value4", store.get("key1"));
assertEquals("value2", store.get("key2"));
assertEquals("value3", store.get("key3"));
assertNull(store.get("key4"));
}
示例12: get
import org.apache.kafka.streams.state.KeyValueStore; //导入依赖的package包/类
/**
* Retrieves a StreamedMetricAggregation from the passed store
* @param streamedMetric The streamed metric to get the aggregator for
* @param sticky true if this metric is sticky across periods, false if metric is fully reset
* @param period The aggregation period
* @param unit The aggregation period unit
* @param store The aggregation store
* @return the StreamedMetricAggregation
*/
public static StreamedMetricAggregation get(final StreamedMetric streamedMetric, final boolean sticky, final long period, final TimeUnit unit, final KeyValueStore<String, StreamedMetricAggregation> store) {
if(streamedMetric==null) throw new IllegalArgumentException("The passed streamed metric was null");
if(store==null) throw new IllegalArgumentException("The passed state store was null");
final String key = streamedMetric.metricKey() + "-" + period + TimeUnitSymbol.symbol(unit);
StreamedMetricAggregation sma = store.get(key);
if(sma==null) {
synchronized(store) {
sma = store.get(key);
if(sma==null) {
sma = new StreamedMetricAggregation(streamedMetric.getMetricName(), sticky, streamedMetric.isValued() ? streamedMetric.forValue(-1L).isDoubleValue() : false, period, unit);
sma.tags.putAll(streamedMetric.getTags());
sma.apply(streamedMetric, sticky, store);
// store.put(key, sma);
}
}
}
return sma;
}
示例13: initializeIfNotAndGet
import org.apache.kafka.streams.state.KeyValueStore; //导入依赖的package包/类
private NFA<K, V> initializeIfNotAndGet(List<Stage<K, V>> stages) {
if( this.nfa == null ) {
LOG.info("Initializing NFA for topic={}, partition={}", context.topic(), context.partition());
KVSharedVersionedBuffer.Factory<K, V> bufferFactory = KVSharedVersionedBuffer.getFactory();
KeyValueStore<TopicAndPartition, NFAStateValue<K, V>> nfaStore = getNFAStore();
TopicAndPartition tp = new TopicAndPartition(context.topic(), context.partition());
NFAStateValue<K, V> nfaState = nfaStore.get(tp);
KVSharedVersionedBuffer<K, V> buffer = bufferFactory.make(context, bufferStateStoreName);
if (nfaState != null) {
LOG.info("Loading existing nfa states for {}, latest offset {}", tp, nfaState.latestOffset);
this.nfa = new NFA<>(new StateStoreProvider(queryName, context), buffer, nfaState.runs, nfaState.computationStages);
this.highwatermark = nfaState.latestOffset;
} else {
this.nfa = new NFA<>(new StateStoreProvider(queryName, context), buffer, stages);
}
}
return this.nfa;
}
示例14: init
import org.apache.kafka.streams.state.KeyValueStore; //导入依赖的package包/类
/** Initializes the state store with the name "FAST-store", where
* `type` is the type specified in the constructor.
*
* {@inheritDoc}
*/
@Override
@SuppressWarnings("unchecked")
public void init(ProcessorContext context) {
this.context = context;
// Schedules the `punctuate` method for every second.
this.context.schedule(1000L);
state = (KeyValueStore) context.getStateStore("FAST-store");
// Print the contents of the state store.
state.all().forEachRemaining(
kv -> System.out.println(System.currentTimeMillis() +
" INIT: " + kv.key +
", " + kv.value));
}
示例15: init
import org.apache.kafka.streams.state.KeyValueStore; //导入依赖的package包/类
@Override
public void init(final ProcessorContext context) {
this.context = context;
// Sort the group by vars so that they will be written to the state store in the same order every time.
final List<String> groupByVars = Lists.newArrayList(aggNode.getGroupBindingNames());
groupByVars.sort(Comparator.naturalOrder());
// Get a reference to the state store that keeps track of aggregation state.
final KeyValueStore<String, AggregationState> stateStore =
(KeyValueStore<String, AggregationState>) context.getStateStore( stateStoreName );
aggStateStore = new KeyValueAggregationStateStore(stateStore, groupByVars);
// Create the aggregation evaluator.
evaluator = AggregationsEvaluator.make(aggStateStore, aggNode, groupByVars);
}