でDataStream演算子
地図
要素を取得し、要素を生成し、
//新的一年给每个员工的工资加 5000。
SingleOutputStreamOperator<Employee> map = employeeStream.map(new MapFunction<Employee, Employee>() {
@Override
public Employee map(Employee employee) throws Exception {
employee.salary = employee.salary + 5000;
return employee;
}
});
map.print();
FlatMap
要素を取得し、ゼロ、一つ以上の要素を生成します
//将工资大于 40000 的找出来。
SingleOutputStreamOperator<Employee> flatMap = employeeStream.flatMap(new FlatMapFunction<Employee, Employee>() {
@Override
public void flatMap(Employee employee, Collector<Employee> out) throws Exception {
if (employee.salary >= 40000) { out.collect(employee);}
}
});
flatMap.print();
フィルタ
各要素の決意は、要素がtrueを返します
//将工资大于 40000 的找出来。
SingleOutputStreamOperator<Employee> filter = employeeStream.filter(new FilterFunction<Employee>() {
@Override
public boolean filter(Employee employee) throws Exception {
if (employee.salary >= 40000) { return true; }
return false;
}
});
filter.print();
KeyBy
対流に基づくKeyBy論理パーティションキー、キーが同じパーティション(パーティションは、本明細書に一方の平行ノード下流オペレータの複数を意味する)に割り当てられます。内部的には、ハッシュ関数のパーティションストリームを使用しています。それはKeyedDataStreamデータストリームを返します。
//根据商品的店铺 id 来进行分区。
KeyedStream<ProductEvent, Integer> keyBy = productStream.keyBy(new KeySelector<ProductEvent, Integer>() {
@Override
public Integer getKey(ProductEvent product) throws Exception {
return product.shopId;
}
});
keyBy.print();
減らします
単一の結果値を返すように削減し、常に新しい価値を創造し、各要素の処理動作を減らします。一般に使用される方法は削減方法を実現することができる使用して、平均値、合計値、MIN、MAX、カウントされています。
//上面先将数据流进行 keyby 操作,因为执行 Reduce 操作只能是 KeyedStream,然后将员工的工资做 了一个求平均值的操作。
SingleOutputStreamOperator<Employee> reduce = employeeStream
.keyBy(new KeySelector<Employee, Integer>() {
@Override
public Integer getKey(Employee employee) throws Exception {
return employee.shopId;
}
})
.reduce(new ReduceFunction<Employee>() {
@Override
public Employee reduce(Employee employee1, Employee employee2) throws Exception {
employee1.salary = (employee1.salary + employee2.salary) / 2;
return employee1;
}
});
reduce.print();
集計
KeyedStream.sum(0)
KeyedStream.sum("key")
KeyedStream.min(0)
KeyedStream.min("key")
KeyedStream.max(0)
KeyedStream.max("key")
KeyedStream.minBy(0)
KeyedStream.minBy("key")
KeyedStream.maxBy(0)
KeyedStream.maxBy("key")
窓
許可時間やその他の条件既存のパケットKeyedStream
inputStream.keyBy(0).window(Time.seconds(10));//以 10 秒的时间窗口聚合
WindowAll
凝集体は一緒にいくつかの特徴要素によれば、並列動作をサポートしていない機能は、デフォルトの並列度は1であります
inputStream.keyBy(0).windowAll(TumblingProcessingTimeWindows.of(Time.seconds(10) ));
連合
2つ以上のデータストリームを組み合わせます
inputStream.union(inputStream1, inputStream2, ...);
ウィンドウが参加します
2つのデータストリームにより、いくつかのキーは、のウィンドウと一緒に参加します
//在 5 秒的窗口中连接两个流,其中第一个流的第一个属性的连接条件等于另一个流的第二个 属性
inputStream.join(inputStream1).where(0).equalTo(1) .window(Time.seconds(5)) .apply (new JoinFunction () {...});
スプリット
流れは、二つ以上の流れに分割されています
SplitStream<Integer> split = inputStream.split(new OutputSelector<Integer>() {
@Override
public Iterable<String> select(Integer value) {
List<String> output = new ArrayList<String>();
if (value % 2 == 0) { output.add("even"); }
else { output.add("odd"); }
return output;
}
});
選択する
スプリットストリームから特定のストリームを選択します
SplitStream<Integer> split;
DataStream<Integer> even = split.select("even");
DataStream<Integer> odd = split.select("odd");
DataStream<Integer> all = split.select("even","odd");
DataSetの演算子
地図、FlatMap、その上のフィルタと...
最初のN
DataSet<Tuple2<String, Integer>> in =
// 返回 DataSet 中前 5 的元素
DataSet<Tuple2<String, Integer>> out1 = in.first(5);
// 返回分组后每个组的前 2 元素
DataSet<Tuple2<String, Integer>> out2 = in.groupBy(0) .first(2);
// 返回分组后每个组的前 3 元素(按照上升排序)
DataSet<Tuple2<String, Integer>> out3 = in.groupBy(0).sortGroup(1, Order.ASCENDING) .first(3);