org.apache.kafka.streams.processor.ProcessorContext Java Examples
The following examples show how to use
org.apache.kafka.streams.processor.ProcessorContext.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StatementOutputFormatterTest.java From rya with Apache License 2.0 | 6 votes |
@Test public void binaryResult() { // Create the input binding set. final ValueFactory vf = SimpleValueFactory.getInstance(); final MapBindingSet bindingSet = new MapBindingSet(); bindingSet.addBinding("subject", vf.createIRI("urn:Alice")); bindingSet.addBinding("predicate", vf.createIRI("urn:age")); bindingSet.addBinding("object", vf.createLiteral(34)); final VisibilityBindingSet visBs = new VisibilityBindingSet(bindingSet, "a"); // Mock the processor context that will be invoked. final ProcessorContext context = mock(ProcessorContext.class); // Run the test. final StatementOutputFormatter formatter = new StatementOutputFormatter(); formatter.init(context); formatter.process("key", ProcessorResult.make(new BinaryResult(Side.LEFT, visBs))); // Verify the mock was invoked with the expected output. final VisibilityStatement expectedStmt = new VisibilityStatement(vf.createStatement( vf.createIRI("urn:Alice"), vf.createIRI("urn:age"), vf.createLiteral(34)), "a"); verify(context, times(1)).forward(eq("key"), eq(expectedStmt)); }
Example #2
Source File: KafkaStreamsFunctionStateStoreTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 6 votes |
@Bean public java.util.function.Consumer<KTable<Object, String>> hello() { return input -> { input.toStream().process(() -> new Processor<Object, String>() { @Override @SuppressWarnings("unchecked") public void init(ProcessorContext context) { state3 = (KeyValueStore<Long, Long>) context.getStateStore("my-store"); state4 = (WindowStore<Long, Long>) context.getStateStore("other-store"); } @Override public void process(Object key, String value) { processed2 = true; } @Override public void close() { } }, "my-store", "other-store"); }; }
Example #3
Source File: MovingAverageProcessor.java From kafka-streams-ex with MIT License | 6 votes |
/** 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)); }
Example #4
Source File: KafkaStreamsStateStoreIntegrationTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 6 votes |
@StreamListener("input") @KafkaStreamsStateStore(name = "mystate", type = KafkaStreamsStateStoreProperties.StoreType.WINDOW, lengthMs = 300000, retentionMs = 300000) @SuppressWarnings({ "deprecation", "unchecked" }) public void process(KStream<Object, Product> input) { input.process(() -> new Processor<Object, Product>() { @Override public void init(ProcessorContext processorContext) { state = (WindowStore) processorContext.getStateStore("mystate"); } @Override public void process(Object s, Product product) { processed = true; } @Override public void close() { if (state != null) { state.close(); } } }, "mystate"); }
Example #5
Source File: AggregationProcessorSupplier.java From rya with Apache License 2.0 | 6 votes |
@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); }
Example #6
Source File: StockPerformanceMetricsTransformer.java From kafka-streams-in-action with Apache License 2.0 | 6 votes |
@Override @SuppressWarnings("unchecked") public void init(final ProcessorContext processorContext) { keyValueStore = (KeyValueStore) processorContext.getStateStore(stocksStateStore); this.processorContext = processorContext; this.processorContext.schedule(5000, PunctuationType.WALL_CLOCK_TIME, this::doPunctuate); final String tagKey = "task-id"; final String tagValue = processorContext.taskId().toString(); final String nodeName = "StockPerformanceProcessor_"+count.getAndIncrement(); metricsSensor = processorContext.metrics().addLatencyAndThroughputSensor("transformer-node", nodeName, "stock-performance-calculation", Sensor.RecordingLevel.DEBUG, tagKey, tagValue); }
Example #7
Source File: TimeCheckDemo.java From Kafka-Streams-Real-time-Stream-Processing with The Unlicense | 6 votes |
public static void main(String[] args) { Properties props = new Properties(); props.put(StreamsConfig.APPLICATION_ID_CONFIG, AppConfigs.applicationID); props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, AppConfigs.bootstrapServers); StreamsBuilder streamsBuilder = new StreamsBuilder(); KStream<String, PosInvoice> KS0 = streamsBuilder.stream(AppConfigs.posTopicName, Consumed.with(PosSerdes.String(), PosSerdes.PosInvoice()) .withTimestampExtractor(new InvoiceTimeExtractor()) ); KS0.transformValues(() -> new ValueTransformer<PosInvoice, PosInvoice>() { private ProcessorContext context; @Override public void init(ProcessorContext processorContext) { this.context = processorContext; } @Override public PosInvoice transform(PosInvoice invoice) { logger.info("Invoice Time: " + new Timestamp(invoice.getCreatedTime()) + " Event Time: " + new Timestamp(context.timestamp())); return invoice; } @Override public void close() { } }); logger.info("Starting Kafka Streams"); KafkaStreams myStream = new KafkaStreams(streamsBuilder.build(), props); myStream.start(); Runtime.getRuntime().addShutdownHook(new Thread(myStream::close)); }
Example #8
Source File: StatementOutputFormatterTest.java From rya with Apache License 2.0 | 6 votes |
@Test public void unaryResult() { // Create the input binding set. final ValueFactory vf = SimpleValueFactory.getInstance(); final MapBindingSet bindingSet = new MapBindingSet(); bindingSet.addBinding("subject", vf.createIRI("urn:Alice")); bindingSet.addBinding("predicate", vf.createIRI("urn:age")); bindingSet.addBinding("object", vf.createLiteral(34)); final VisibilityBindingSet visBs = new VisibilityBindingSet(bindingSet, "a"); // Mock the processor context that will be invoked. final ProcessorContext context = mock(ProcessorContext.class); // Run the test. final StatementOutputFormatter formatter = new StatementOutputFormatter(); formatter.init(context); formatter.process("key", ProcessorResult.make(new UnaryResult(visBs))); // Verify the mock was invoked with the expected output. final VisibilityStatement expectedStmt = new VisibilityStatement(vf.createStatement( vf.createIRI("urn:Alice"), vf.createIRI("urn:age"), vf.createLiteral(34)), "a"); verify(context, times(1)).forward(eq("key"), eq(expectedStmt)); }
Example #9
Source File: LogMetricAndContinueExceptionHandler.java From ksql-fork-with-deep-learning-function with Apache License 2.0 | 6 votes |
@Override public DeserializationHandlerResponse handle( final ProcessorContext context, final ConsumerRecord<byte[], byte[]> record, final Exception exception ) { log.warn( "Exception caught during Deserialization, " + "taskId: {}, topic: {}, partition: {}, offset: {}", context.taskId(), record.topic(), record.partition(), record.offset(), exception ); MetricCollectors.recordError(record.topic()); return DeserializationHandlerResponse.CONTINUE; }
Example #10
Source File: StatementOutputFormatterTest.java From rya with Apache License 2.0 | 6 votes |
@Test public void missingSubject() { // Create the input binding set. final ValueFactory vf = SimpleValueFactory.getInstance(); final MapBindingSet bindingSet = new MapBindingSet(); bindingSet.addBinding("predicate", vf.createIRI("urn:age")); bindingSet.addBinding("object", vf.createLiteral(34)); final VisibilityBindingSet visBs = new VisibilityBindingSet(bindingSet, "a"); // Mock the processor context that will be invoked. final ProcessorContext context = mock(ProcessorContext.class); // Run the test. final StatementOutputFormatter formatter = new StatementOutputFormatter(); formatter.init(context); formatter.process("key", ProcessorResult.make(new UnaryResult(visBs))); // Verify the mock was never invoked. verify(context, times(0)).forward(any(), any()); }
Example #11
Source File: StatementOutputFormatterTest.java From rya with Apache License 2.0 | 6 votes |
@Test public void subjectWrongType() { // Create the input binding set. final ValueFactory vf = SimpleValueFactory.getInstance(); final MapBindingSet bindingSet = new MapBindingSet(); bindingSet.addBinding("subject", vf.createLiteral("Alice")); bindingSet.addBinding("predicate", vf.createIRI("urn:age")); bindingSet.addBinding("object", vf.createLiteral(34)); final VisibilityBindingSet visBs = new VisibilityBindingSet(bindingSet, "a"); // Mock the processor context that will be invoked. final ProcessorContext context = mock(ProcessorContext.class); // Run the test. final StatementOutputFormatter formatter = new StatementOutputFormatter(); formatter.init(context); formatter.process("key", ProcessorResult.make(new UnaryResult(visBs))); // Verify the mock was never invoked. verify(context, times(0)).forward(any(), any()); }
Example #12
Source File: StatementOutputFormatterTest.java From rya with Apache License 2.0 | 6 votes |
@Test public void missingPredicate() { // Create the input binding set. final ValueFactory vf = SimpleValueFactory.getInstance(); final MapBindingSet bindingSet = new MapBindingSet(); bindingSet.addBinding("subject", vf.createIRI("urn:Alice")); bindingSet.addBinding("object", vf.createLiteral(34)); final VisibilityBindingSet visBs = new VisibilityBindingSet(bindingSet, "a"); // Mock the processor context that will be invoked. final ProcessorContext context = mock(ProcessorContext.class); // Run the test. final StatementOutputFormatter formatter = new StatementOutputFormatter(); formatter.init(context); formatter.process("key", ProcessorResult.make(new UnaryResult(visBs))); // Verify the mock was never invoked. verify(context, times(0)).forward(any(), any()); }
Example #13
Source File: StatementOutputFormatterTest.java From rya with Apache License 2.0 | 6 votes |
@Test public void predicateWrongType() { // Create the input binding set. final ValueFactory vf = SimpleValueFactory.getInstance(); final MapBindingSet bindingSet = new MapBindingSet(); bindingSet.addBinding("subject", vf.createIRI("urn:Alice")); bindingSet.addBinding("predicate", vf.createLiteral("age")); bindingSet.addBinding("object", vf.createLiteral(34)); final VisibilityBindingSet visBs = new VisibilityBindingSet(bindingSet, "a"); // Mock the processor context that will be invoked. final ProcessorContext context = mock(ProcessorContext.class); // Run the test. final StatementOutputFormatter formatter = new StatementOutputFormatter(); formatter.init(context); formatter.process("key", ProcessorResult.make(new UnaryResult(visBs))); // Verify the mock was never invoked. verify(context, times(0)).forward(any(), any()); }
Example #14
Source File: BindingSetOutputFormatterTest.java From rya with Apache License 2.0 | 6 votes |
@Test public void binaryResult() { // Create the input binding set. final ValueFactory vf = SimpleValueFactory.getInstance(); final MapBindingSet bindingSet = new MapBindingSet(); bindingSet.addBinding("person", vf.createIRI("urn:Alice")); bindingSet.addBinding("age", vf.createLiteral(34)); final VisibilityBindingSet visBs = new VisibilityBindingSet(bindingSet, "a"); // Mock the processor context that will be invoked. final ProcessorContext context = mock(ProcessorContext.class); // Run the test. final BindingSetOutputFormatter formatter = new BindingSetOutputFormatter(); formatter.init(context); formatter.process("key", ProcessorResult.make(new BinaryResult(Side.LEFT, visBs))); // Verify the mock was invoked with the expected output. verify(context, times(1)).forward(eq("key"), eq(visBs)); }
Example #15
Source File: KafkaStreamsStateStoreIntegrationTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 6 votes |
@StreamListener("input3") @SuppressWarnings({"unchecked" }) public void process(KStream<Object, Product> input) { input.process(() -> new Processor<Object, Product>() { @Override public void init(ProcessorContext processorContext) { state = (WindowStore) processorContext.getStateStore("mystate"); } @Override public void process(Object s, Product product) { processed = true; } @Override public void close() { if (state != null) { state.close(); } } }, "mystate"); }
Example #16
Source File: ComputeFunction.java From kafka-graphs with Apache License 2.0 | 5 votes |
public Callback(ProcessorContext context, K key, TimestampedKeyValueStore<K, Map<K, EV>> edgesStore, Map<String, ?> previousAggregates, Map<String, Map<K, ?>> aggregators) { this.context = context; this.previousAggregates = previousAggregates; this.aggregators = aggregators; this.key = key; this.edgesStore = edgesStore; }
Example #17
Source File: CPUMetricStreamHandler.java From kafka-streams-example with Apache License 2.0 | 5 votes |
@Override public void init(ProcessorContext pc) { this.pc = pc; this.pc.schedule(12000); //invoke punctuate every 12 seconds this.machineToAvgCPUUsageStore = (KeyValueStore<String, Double>) pc.getStateStore(AVG_STORE_NAME); this.machineToNumOfRecordsReadStore = (KeyValueStore<String, Integer>) pc.getStateStore(NUM_RECORDS_STORE_NAME); PROC_LOGGER.info("Processor initialized"); }
Example #18
Source File: MonitorProcessorSupplier.java From DBus with Apache License 2.0 | 5 votes |
@Override public void init(ProcessorContext context) { super.init(context); // 两分钟执行一次punctuate方法 context().schedule(2 * 60 * 1000); this.store = (KeyValueStore<String, String>) context.getStateStore("zkInfo"); }
Example #19
Source File: PregelComputation.java From kafka-graphs with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") private static Consumer<byte[], byte[]> internalConsumer(ProcessorContext context) throws NoSuchFieldException, IllegalAccessException { // Consumer is created in a different thread, so can't use ThreadLocal; use reflection instead Field taskField = ProcessorContextImpl.class.getDeclaredField("task"); taskField.setAccessible(true); StreamTask streamTask = (StreamTask) taskField.get(context); Field consumerField = AbstractTask.class.getDeclaredField("consumer"); consumerField.setAccessible(true); return (Consumer<byte[], byte[]>) consumerField.get(streamTask); }
Example #20
Source File: PregelComputation.java From kafka-graphs with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") @Override public void init(final ProcessorContext context) { this.context = context; this.localSolutionSetStore = (KeyValueStore<K, Tuple4<Integer, VV, Integer, VV>>) context.getStateStore(localSolutionSetStoreName); this.verticesStore = (TimestampedKeyValueStore<K, VV>) context.getStateStore(vertices.queryableStoreName()); this.edgesStore = (TimestampedKeyValueStore<K, Map<K, EV>>) context.getStateStore(edgesGroupedBySource.queryableStoreName()); }
Example #21
Source File: WordCountProcessor.java From KafkaExample with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") @Override public void init(ProcessorContext context) { this.context = context; this.context.schedule(1000); this.kvStore = (KeyValueStore<String, Integer>) context.getStateStore("Counts"); }
Example #22
Source File: StockPerformanceCancellingProcessor.java From kafka-streams-in-action with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") @Override public void init(ProcessorContext processorContext) { super.init(processorContext); keyValueStore = (KeyValueStore) context().getStateStore(stateStoreName); StockPerformancePunctuator punctuator = new StockPerformancePunctuator(differentialThreshold, context(), keyValueStore); cancellable = context().schedule(10000, PunctuationType.WALL_CLOCK_TIME, punctuator); }
Example #23
Source File: KafkaStreamsStateStoreIntegrationTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 5 votes |
@StreamListener @KafkaStreamsStateStore(name = "mystate", type = KafkaStreamsStateStoreProperties.StoreType.WINDOW, lengthMs = 300000, retentionMs = 300000) @SuppressWarnings({ "deprecation", "unchecked" }) public void process(@Input("input1")KStream<Object, Product> input, @Input("input2")KStream<Object, Product> input2) { input.process(() -> new Processor<Object, Product>() { @Override public void init(ProcessorContext processorContext) { state = (WindowStore) processorContext.getStateStore("mystate"); } @Override public void process(Object s, Product product) { processed = true; } @Override public void close() { if (state != null) { state.close(); } } }, "mystate"); //simple use of input2, we are not using input2 for anything other than triggering some test behavior. input2.foreach((key, value) -> { }); }
Example #24
Source File: KafkaStreamsTracingTest.java From brave with Apache License 2.0 | 5 votes |
@Test public void nextSpan_uses_current_context() { ProcessorContext fakeProcessorContext = processorContextSupplier.apply(new RecordHeaders()); Span child; try (Scope ws = tracing.currentTraceContext().newScope(parent)) { child = kafkaStreamsTracing.nextSpan(fakeProcessorContext); } child.finish(); assertThat(child.context().parentIdString()) .isEqualTo(parent.spanIdString()); }
Example #25
Source File: StockSummaryProcessor.java From kafka-streams with Apache License 2.0 | 5 votes |
@Override @SuppressWarnings("unchecked") public void init(ProcessorContext context) { this.context = context; this.context.schedule(10000); summaryStore = (KeyValueStore<String, StockTransactionSummary>) this.context.getStateStore("stock-transactions"); Objects.requireNonNull(summaryStore, "State store can't be null"); }
Example #26
Source File: KafkaStreamsTracingTest.java From brave with Apache License 2.0 | 5 votes |
@Test public void nextSpan_should_tag_app_id_and_task_id() { ProcessorContext fakeProcessorContext = processorContextSupplier.apply(new RecordHeaders()); kafkaStreamsTracing.nextSpan(fakeProcessorContext).start().finish(); assertThat(spans.get(0).tags()) .containsOnly( entry("kafka.streams.application.id", TEST_APPLICATION_ID), entry("kafka.streams.task.id", TEST_TASK_ID)); }
Example #27
Source File: FilterProcessorTest.java From rya with Apache License 2.0 | 5 votes |
@Test public void showFilterFunctionIsCalled() throws Exception { // Read the filter object from a SPARQL query. final Filter filter = RdfTestUtil.getFilter( "SELECT * " + "WHERE { " + "FILTER(?age < 10)" + "?person <urn:age> ?age " + "}"); // Create a Binding Set that will be passed into the Filter function based on the where clause. final ValueFactory vf = SimpleValueFactory.getInstance(); final MapBindingSet bs = new MapBindingSet(); bs.addBinding("person", vf.createIRI("urn:Alice")); bs.addBinding("age", vf.createLiteral(9)); final VisibilityBindingSet inputVisBs = new VisibilityBindingSet(bs, "a"); // Mock the processor context that will be invoked. final ProcessorContext context = mock(ProcessorContext.class); // Run the test. final FilterProcessor processor = new FilterProcessor( FilterEvaluator.make(filter), result -> ProcessorResult.make(new UnaryResult(result))); processor.init(context); processor.process("key", ProcessorResult.make(new UnaryResult(inputVisBs))); // Verify the binding set was passed through. verify(context, times(1)).forward(eq("key"), eq(ProcessorResult.make(new UnaryResult(inputVisBs)))); }
Example #28
Source File: JoinProcessorSupplier.java From rya with Apache License 2.0 | 5 votes |
@Override public void init(final ProcessorContext context) { // Hold onto the context so that we can forward results. this.context = context; final String appId = context.applicationId(); final UUID queryId = UuidUtils.extractUuidFromStringEnd(appId); // Get a reference to the state store that keeps track of what can be joined with. final KeyValueStore<String, VisibilityBindingSet> stateStore = (KeyValueStore<String, VisibilityBindingSet>) context.getStateStore( stateStoreName ); joinStateStore = new KeyValueJoinStateStore( stateStore, queryId.toString(), joinVars, allVars ); }
Example #29
Source File: ProjectionProcessorTest.java From rya with Apache License 2.0 | 5 votes |
@Test public void showProjectionFunctionIsCalled() throws Exception { // A query whose projection does not change anything. final Projection projection = RdfTestUtil.getProjection( "SELECT (?person AS ?p) (?employee AS ?e) ?business " + "WHERE { " + "?person <urn:talksTo> ?employee . " + "?employee <urn:worksAt> ?business . " + "}"); // Create a Binding Set that contains the result of the WHERE clause. final ValueFactory vf = SimpleValueFactory.getInstance(); final MapBindingSet inputBs = new MapBindingSet(); inputBs.addBinding("person", vf.createIRI("urn:Alice")); inputBs.addBinding("employee", vf.createIRI("urn:Bob")); inputBs.addBinding("business", vf.createIRI("urn:TacoJoint")); final VisibilityBindingSet inputVisBs = new VisibilityBindingSet(inputBs, "a"); // The expected binding set changes the "person" binding name to "p" and "employee" to "e". final MapBindingSet expectedBs = new MapBindingSet(); expectedBs.addBinding("p", vf.createIRI("urn:Alice")); expectedBs.addBinding("e", vf.createIRI("urn:Bob")); expectedBs.addBinding("business", vf.createIRI("urn:TacoJoint")); final VisibilityBindingSet expectedVisBs = new VisibilityBindingSet(expectedBs, "a"); // Show it resulted in the correct output BindingSet. final ProcessorContext context = mock(ProcessorContext.class); final ProjectionProcessor processor = new ProjectionProcessor( ProjectionEvaluator.make(projection), result -> ProcessorResult.make(new UnaryResult(result))); processor.init(context); processor.process("key", ProcessorResult.make(new UnaryResult(inputVisBs))); // Verify the expected binding set was emitted. final ProcessorResult expected = ProcessorResult.make(new UnaryResult(expectedVisBs)); verify(context, times(1)).forward(eq("key"), eq(expected)); }
Example #30
Source File: KafkaStreamsTracing.java From brave with Apache License 2.0 | 5 votes |
Span nextSpan(ProcessorContext context) { TraceContextOrSamplingFlags extracted = extractor.extract(context.headers()); // Clear any propagation keys present in the headers if (!extracted.equals(emptyExtraction)) { clearHeaders(context.headers()); } Span result = tracer.nextSpan(extracted); if (!result.isNoop()) { addTags(context, result); } return result; }