@Override
public void flatMap(String value, Collector<Tuple2<String, Long>> out) throws Exception {
// this.counter.inc();
result2LineCounter++;
System.out.println(“计数器行数:” + result2LineCounter);
String[] arr = value.split(“,”);
for (String word : arr) {
out.collect(Tuple2.of(word, result2LineCounter));
}
}
}).map(new MapFunction<Tuple2<String, Long>, Tuple3<String, Long, Integer>>() {
@Override
public Tuple3<String, Long, Integer> map(Tuple2<String, Long> value) throws Exception {
// Tuple3<String, Long, Integer> t = Tuple3.of(value.f0, value.f1, 1);
return Tuple3.of(value.f0, value.f1, 1);
}
}).keyBy(t -> t.f0).sum(2);
// sink
result.print("result:");
result2.print("result2:");
env.execute();
}
public static void main(String[] args) throws Exception {
test1();
// StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
// env.setParallelism(1);
// DataStream input = env.fromElements(“a”, “b”, “c”, “a”, “b”, “c”);
//
// input.keyBy(value -> value).map(new RichMapFunction<String, String>() {
// private long count = 0;
//
// @Override
// public void open(Configuration parameters) throws Exception {
super.open(parameters);
// count = getRuntimeContext().getMetricGroup().counter(“myCounter”).getCount();
// }
//
// @Override
// public String map(String value) throws Exception {
// count++;
// return value + ": " + count;
// }
// }).print();
//
// env.execute(“Flink Count Counter Example”);
}
}
///验证数据///
// 输入数据
[alanchan@server2 bin]$ nc -lk 9999
hello,123
alan,flink,good
alan_chan,hi,flink
//控制台输出:
计数器行数:1
result:> (hello,1)
result2:> (hello,1,1)
result:> (123,1)
result2:> (123,1,1)
计数器行数:2
result2:> (alan,2,1)
result:> (alan,1)
result2:> (flink,2,1)
result:> (flink,1)
result2:> (good,2,1)
result:> (good,1)
计数器行数:3
result:> (alan_chan,1)
result2:> (alan_chan,3,1)
result:> (hi,1)
result2:> (hi,3,1)
result:> (flink,2)
result2:> (flink,2,2)
或者,您也可以使用自己的Counter实现:
import org.apache.flink.api.common.RuntimeExecutionMode;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.RichFlatMapFunction;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.metrics.Counter;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;
/**
* @author alanchan
*
*/
public class TestMetricsDemo {
public static void test2() throws Exception {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.setRuntimeMode(RuntimeExecutionMode.AUTOMATIC);
env.setParallelism(1);
// source
DataStream<String> lines = env.socketTextStream("192.168.10.42", 9999);
// transformation
// Tuple3<String, Long, Integer> 输入的字符串,行数,统计单词的总数
DataStream<Tuple3<String, Long, Integer>> result = lines.flatMap(new RichFlatMapFunction<String, Tuple2<String, Long>>() {
private transient Counter counter;
@Override
public void open(Configuration config) {
this.counter = getRuntimeContext().getMetricGroup().counter("result2LineCounter", new AlanCustomCounter());
}
@Override
public void flatMap(String value, Collector<Tuple2<String, Long>> out) throws Exception {
this.counter.inc();
// result2LineCounter++;
System.out.println(“计数器行数:” + this.counter.getCount());
String[] arr = value.split(“,”);
for (String word : arr) {
out.collect(Tuple2.of(word, this.counter.getCount()));
}
}
}).map(new MapFunction<Tuple2<String, Long>, Tuple3<String, Long, Integer>>() {
@Override
public Tuple3<String, Long, Integer> map(Tuple2<String, Long> value) throws Exception {
return Tuple3.of(value.f0, value.f1, 1);
}
}).keyBy(t -> t.f0).sum(2);
// sink
result.print("result:");
env.execute();
}
public static class AlanCustomCounter implements Counter {
private long count;
@Override
public void inc() {
count += 2;
}
@Override
public void inc(long n) {
count += n;
}
@Override
public void dec() {
count -= 2;
}
@Override
public void dec(long n) {
count -= n;
}
@Override
public long getCount() {
return count;
}
}
public static void main(String[] args) throws Exception {
test2();
}
}
///验证数据///
// 输入数据
[alanchan@server2 bin]$ nc -lk 9999
hello,123
alan,flink,good
alan_chan,hi,flink
//控制台输出:
计数器行数:2
result:> (hello,2,1)
result:> (123,2,1)
计数器行数:4
result:> (alan,4,1)
result:> (flink,4,1)
result:> (good,4