/
KafkaSample.spl
59 lines (51 loc) · 1.66 KB
/
KafkaSample.spl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// *******************************************************************************
// * Copyright (C)2014, International Business Machines Corporation and *
// * others. All Rights Reserved. *
// *******************************************************************************
//
namespace com.ibm.streamsx.messaging.sample.kafka;
use com.ibm.streamsx.messaging.kafka::*;
/**
* Example of using a [KafkaProducer] and [KafkaConsumer]
* This assumes that the topic has already been created
*
*/
composite KafkaSample {
param
expression<rstring> $topic : "sampleTopic" ;
graph
//create some messages and send to Kafka
stream<rstring topic, rstring key, rstring message> OutputStream = Beacon() {
param
initDelay : 5.0;
period : 0.2;
output OutputStream: topic = $topic,
message = "Reality is merely an illusion, albeit a very persistent one." , key = "Einstein";
}
() as KafkaSinkOp = KafkaProducer(OutputStream) {
param
propertiesFile : "etc/producer.properties";
}
/*
* If you want to have consumers running in parallel, just uncomment the
* @parallel and set the width to the number of consumers you want.
* Remember, there can be at most one consumer per partition in a
* consumer group, so width should never be greater than the number
* of partitions in your topic.
*/
// @parallel(width = 3)
stream<rstring key, rstring message> KafkaStream = KafkaConsumer()
{
param
propertiesFile : "etc/consumer.properties";
topic : $topic;
}
() as SinkOp = Custom(KafkaStream)
{
logic
onTuple KafkaStream :
{
printStringLn("Message: " + (rstring) KafkaStream);
}
}
}