7
7
// 2014-10-07T12:22:10Z;foo;1
8
8
// 2014-10-07T12:23:11Z;foo;29
9
9
10
+ import java .util .Properties
11
+ import kafka .producer .{ProducerConfig , KeyedMessage , Producer }
12
+
10
13
import org .apache .spark ._
11
14
import org .apache .spark .SparkContext ._
12
15
import org .apache .spark .streaming ._
@@ -21,6 +24,9 @@ import com.datastax.spark.connector.cql.CassandraConnector
21
24
import com .datastax .driver .core .ConsistencyLevel
22
25
import com .datastax .driver .core .utils .UUIDs
23
26
27
+ // import org.apache.spark.streaming._
28
+ import org .apache .spark .streaming .kafka ._
29
+ // import org.apache.spark.SparkConf
24
30
25
31
object Test {
26
32
@@ -83,11 +89,30 @@ object Test {
83
89
84
90
// for testing purposes you can use the alternative input below
85
91
// val input = sc.parallelize(sampleRecords)
86
- val input = ssc.socketTextStream(" localhost" , 9999 )
92
+ // val input = ssc.socketTextStream("localhost", 9999)
93
+ // val Array(zkQuorum, group, topics, numThreads) = args
94
+
95
+ val zkQuorum = " localhost:2181"
96
+ val inputTopic = " events"
97
+
98
+ val kafkaParams = Map (
99
+ " zk.connect" -> " 127.0.0.1:2181" ,
100
+ " zookeeper.connect" -> " localhost:2181" ,
101
+ " zookeeper.connection.timeout.ms" -> " 1000" ,
102
+ " group.id" -> " spark-streaming-test" ,
103
+ " zookeeper.connection.timeout.ms" -> " 1000" )
104
+
105
+ val input = KafkaUtils .createStream(
106
+ ssc,
107
+ " localhost:2181" ,
108
+ Map (inputTopic -> 1 )).map(_._2)
109
+
110
+ input.print()
111
+
87
112
val parsedRecords = input.map(parseMessage)
88
113
val bucketedRecords = parsedRecords.map(record => ((record.bucket, record.name), record))
89
114
val bucketedCounts = bucketedRecords.combineByKey(
90
- (record) => record.count,
115
+ (record: Record ) => record.count,
91
116
(count: Long , record: Record ) => (count + record.count),
92
117
(c1: Long , c2: Long ) => (c1 + c2),
93
118
new HashPartitioner (1 ))
@@ -102,3 +127,36 @@ object Test {
102
127
ssc.awaitTermination()
103
128
}
104
129
}
130
+
131
+
132
+ object KafkaEventProducer {
133
+
134
+ def main (args : Array [String ]) {
135
+
136
+ if (args.length < 4 ) {
137
+ System .err.println(" Usage: KafkaEventProducer <metadataBrokerList> <topic> <messagesPerSec> <wordsPerMessage>" )
138
+ System .exit(1 )
139
+ }
140
+
141
+ val Array (brokers, topic, messagesPerSec, wordsPerMessage) = args
142
+
143
+ // Zookeeper connection properties
144
+ val props = new Properties ()
145
+ props.put(" metadata.broker.list" , brokers)
146
+ props.put(" serializer.class" , " kafka.serializer.StringEncoder" )
147
+
148
+ val config = new ProducerConfig (props)
149
+ val producer = new Producer [String , String ](config)
150
+
151
+ // "2014-10-07T12:20:08Z;foo;1"
152
+
153
+ // Send some messages
154
+ while (true ) {
155
+ val eventCount = scala.util.Random .nextInt(10 ).toString()
156
+ val eventString = " 2014-10-07T12:20:08Z;foo;" + eventCount
157
+ producer.send(new KeyedMessage [String , String ](" events" , eventString))
158
+ Thread .sleep(100 )
159
+ }
160
+ }
161
+
162
+ }
0 commit comments