我想知道是否有可能在Sparks流中将滑动窗口串在一起。
例如,每1秒就有计数。我想用5秒、15秒和30秒的时间把这些加起来。我想知道是否可以重复使用15秒的窗口结果,15秒的窗口结果,30秒的窗口结果。
这样做的目的是避免存储所有输入的1秒更新,以及最长窗口的长度(因为粒度在这里并不重要)。相反,我们使用与我们需要的频率相匹配的频率重用Dstream。
下面是一个例子:
JavaPairDStream< String, Double > test = input;
JavaPairDStream< String, Double > test1 = input;
// 5s:
test = test.reduceByKeyAndWindow(new SumReducer(), new Duration(5000), new Duration(1000));
test1 = test1.reduceByKeyAndWindow(new SumReducer(), new Duration(5000), new Duration(5000));
// 15s
test = test1.reduceByKeyAndWindow(new SumReducer(), new Duration(15000), new Duration(5000));
test1 = test1.reduceByKeyAndWindow(new SumReducer(), new Duration(15000), new Duration(15000));
// 30s
test = test1.reduceByKeyAndWindow(new SumReducer(), new Duration(30000), new Duration(15000));
test.print();我试过了,但什么都没有印出来。
发布于 2014-11-25 20:02:27
批处理间隔
窗口长度和滑动间隔必须是批处理间隔的乘积。为了避免竞争条件(例如,在10秒窗口中发出3 5秒和),批处理间隔必须大于计算时间。我假设这里的批处理间隔为1000毫秒。
示例
JavaPairDStream<String, Double> stream = input;
// A: 5s sum every 5s
stream5sCount = stream.reduceByKeyAndWindow(
new SumReducer(), new Duration(5000), new Duration(5000));
// B: 15s sum every 5s
stream15sCount = stream5sCount.reduceByKeyAndWindow(
new SumReducer(), new Duration(15000), new Duration(5000));
// C: 30s sum every 15s
stream30sCount = stream15sCount
.reduceByKeyAndWindow(new SumReducer(), new Duration(30000), new Duration(15000))
.map(new DivideBy(3));
stream30sCount.print();解释
(对于两个动作A和B,其中B减少A: B/ windowLength of A=B的输入元组数)
修正步骤
我想你真正的应用程序并不像单词计数那么简单。您将需要一个反函数来修复之后的复制错误。您还可以尝试在C之前修复这个问题(在单词count示例中,可以在前面进行划分)。另一种解决方案是跟踪已经处理过的元组,并且只跟踪聚合的分离元组。这取决于您的用例。
https://stackoverflow.com/questions/27111304
复制相似问题