我正在尝试在本地计算机上运行一个简单的Spark-Streaming示例 我有一个线程将As / Bs / Cs写入套接字:
serverSocket = new ServerSocket(Constants.PORT);
s1 = serverSocket.accept();
while(true) {
Thread.sleep(random.nextInt(100));
String character = alphabet.get(random.nextInt(alphabet.size())) ;
PrintWriter out = new PrintWriter(s1.getOutputStream());
out.println(character);
out.flush();
}
我的主程序,我尝试计算As / Bs / Cs的数量如下(没有reduce步骤):
public static void main(String[] args) {
// start socket writer thread
System.setProperty("spark.cleaner.ttl", "10000");
JavaSparkContext sc = new JavaSparkContext(
"local",
"Test",
Constants.SPARK_HOME,
new String[]{"target/spark-standalone-0.0.1-SNAPSHOT.jar"});
Duration batchDuration = new Duration(TIME_WINDOW_MS);
JavaStreamingContext streamingContext = new JavaStreamingContext(sc, batchDuration);
JavaDStream<String> stream = streamingContext.socketTextStream("localhost", Constants.PORT);
stream.print();
JavaPairDStream<String, Long> texts = stream.map(new PairFunction<String, String, Long>() {
@Override
public Tuple2<String, Long> call(String t) throws Exception {
return new Tuple2<String, Long>("batchCount" + t, 1l);
}
});
texts.print();
streamingContext.checkpoint("checkPointDir");
streamingContext.start();
在这种情况下,一切正常(批量的样本输出):
Time: 1372413296000 ms
-------------------------------------------
B
A
B
C
C
C
A
B
C
C
...
-------------------------------------------
Time: 1372413296000 ms
-------------------------------------------
(batchCountB,1)
(batchCountA,1)
(batchCountB,1)
(batchCountC,1)
(batchCountC,1)
(batchCountC,1)
(batchCountA,1)
(batchCountB,1)
(batchCountC,1)
(batchCountC,1)
...
但如果我在地图之后添加减少步骤则不再有效。此代码位于texts.print()
之后JavaPairDStream<String, Long> reduced = texts.reduceByKeyAndWindow(new Function2<Long, Long, Long>() {
@Override
public Long call(Long t1, Long t2) throws Exception {
return t1 + t2;
}
}, new Duration(TIME_WINDOW_MS));
reduced.print();
在这种情况下,我只得到第一个“stream”变量和“texts”变量的输出,而没有用于reduce的输出。第一批处理后也没有任何反应。我还将火花日志级别设置为DEBUG,但没有遇到任何异常或其他奇怪的事情。
这里发生了什么?为什么我会被锁定?
答案 0 :(得分:2)
仅供记录:我在Spark用户组中得到了答案 错误是必须使用
"local[2]"
而不是
"local"
作为实例化Spark上下文的参数,以便启用并发处理。