|
27 | 27 |
|
28 | 28 | import junit.framework.Assert;
|
29 | 29 |
|
| 30 | +import kafka.serializer.StringDecoder; |
| 31 | + |
30 | 32 | import org.apache.spark.api.java.JavaPairRDD;
|
31 | 33 | import org.apache.spark.api.java.function.Function;
|
| 34 | +import org.apache.spark.storage.StorageLevel; |
32 | 35 | import org.apache.spark.streaming.Duration;
|
33 | 36 | import org.apache.spark.streaming.LocalJavaStreamingContext;
|
34 | 37 | import org.apache.spark.streaming.api.java.JavaDStream;
|
@@ -65,17 +68,31 @@ public void testKafkaStream() throws InterruptedException {
|
65 | 68 | String topic = "topic1";
|
66 | 69 | HashMap<String, Integer> topics = new HashMap<String, Integer>();
|
67 | 70 | topics.put(topic, 1);
|
68 |
| - testSuite.createTopic(topic); |
69 | 71 |
|
70 | 72 | HashMap<String, Integer> sent = new HashMap<String, Integer>();
|
71 | 73 | sent.put("a", 5);
|
72 | 74 | sent.put("b", 3);
|
73 | 75 | sent.put("c", 10);
|
74 | 76 |
|
| 77 | + testSuite.createTopic(topic); |
| 78 | + HashMap<String, Object> tmp = new HashMap<String, Object>(sent); |
| 79 | + testSuite.produceAndSendMessage(topic, |
| 80 | + JavaConverters.asScalaMapConverter(tmp).asScala().toMap( |
| 81 | + Predef.<Tuple2<String, Object>>conforms())); |
| 82 | + |
| 83 | + HashMap<String, String> kafkaParams = new HashMap<String, String>(); |
| 84 | + kafkaParams.put("zookeeper.connect", testSuite.zkConnect()); |
| 85 | + kafkaParams.put("group.id", "test-consumer-" + testSuite.random().nextInt(10000)); |
| 86 | + kafkaParams.put("auto.offset.reset", "smallest"); |
| 87 | + |
75 | 88 | JavaPairDStream<String, String> stream = KafkaUtils.createStream(ssc,
|
76 |
| - testSuite.zkConnect(), |
77 |
| - "group", |
78 |
| - topics); |
| 89 | + String.class, |
| 90 | + String.class, |
| 91 | + StringDecoder.class, |
| 92 | + StringDecoder.class, |
| 93 | + kafkaParams, |
| 94 | + topics, |
| 95 | + StorageLevel.MEMORY_ONLY_SER()); |
79 | 96 |
|
80 | 97 | final HashMap<String, Long> result = new HashMap<String, Long>();
|
81 | 98 |
|
@@ -107,15 +124,6 @@ public Void call(JavaPairRDD<String, Long> rdd) throws Exception {
|
107 | 124 | );
|
108 | 125 |
|
109 | 126 | ssc.start();
|
110 |
| - |
111 |
| - // Sleep to let Receiver start first |
112 |
| - Thread.sleep(3000); |
113 |
| - |
114 |
| - HashMap<String, Object> tmp = new HashMap<String, Object>(sent); |
115 |
| - testSuite.produceAndSendMessage(topic, |
116 |
| - JavaConverters.asScalaMapConverter(tmp).asScala().toMap( |
117 |
| - Predef.<Tuple2<String, Object>>conforms())); |
118 |
| - |
119 | 127 | ssc.awaitTermination(3000);
|
120 | 128 |
|
121 | 129 | Assert.assertEquals(sent.size(), result.size());
|
|
0 commit comments