Java Code Examples for org.apache.flink.streaming.api.environment.CheckpointConfig#setCheckpointingMode()
The following examples show how to use
org.apache.flink.streaming.api.environment.CheckpointConfig#setCheckpointingMode() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HyperLogLogUvExample.java From flink-learning with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws Exception { final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); env.enableCheckpointing(TimeUnit.MINUTES.toMillis(1)); env.setParallelism(2); CheckpointConfig checkpointConf = env.getCheckpointConfig(); checkpointConf.setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE); checkpointConf.enableExternalizedCheckpoints(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION); Properties props = new Properties(); props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, UvExampleUtil.broker_list); props.put(ConsumerConfig.GROUP_ID_CONFIG, "app-uv-stat"); FlinkKafkaConsumerBase<String> kafkaConsumer = new FlinkKafkaConsumer011<>( UvExampleUtil.topic, new SimpleStringSchema(), props) .setStartFromLatest(); FlinkJedisPoolConfig conf = new FlinkJedisPoolConfig .Builder().setHost("192.168.30.244").build(); env.addSource(kafkaConsumer) .map(string -> { // 反序列化 JSON UserVisitWebEvent userVisitWebEvent = GsonUtil.fromJson( string, UserVisitWebEvent.class); // 生成 Redis key,格式为 日期_pageId,如: 20191026_0 String redisKey = userVisitWebEvent.getDate() + "_" + userVisitWebEvent.getPageId(); return Tuple2.of(redisKey, userVisitWebEvent.getUserId()); }) .returns(new TypeHint<Tuple2<String, String>>(){}) .addSink(new RedisSink<>(conf, new RedisPfaddSinkMapper())); env.execute("Redis Set UV Stat"); }
Example 2
Source File: RedisSetUvExample.java From flink-learning with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws Exception { final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); env.enableCheckpointing(TimeUnit.MINUTES.toMillis(1)); env.setParallelism(2); CheckpointConfig checkpointConf = env.getCheckpointConfig(); checkpointConf.setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE); checkpointConf.enableExternalizedCheckpoints(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION); Properties props = new Properties(); props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, UvExampleUtil.broker_list); props.put(ConsumerConfig.GROUP_ID_CONFIG, "app-uv-stat"); FlinkKafkaConsumerBase<String> kafkaConsumer = new FlinkKafkaConsumer011<>( UvExampleUtil.topic, new SimpleStringSchema(), props) .setStartFromGroupOffsets(); FlinkJedisPoolConfig conf = new FlinkJedisPoolConfig .Builder().setHost("192.168.30.244").build(); env.addSource(kafkaConsumer) .map(string -> { // 反序列化 JSON UserVisitWebEvent userVisitWebEvent = GsonUtil.fromJson( string, UserVisitWebEvent.class); // 生成 Redis key,格式为 日期_pageId,如: 20191026_0 String redisKey = userVisitWebEvent.getDate() + "_" + userVisitWebEvent.getPageId(); return Tuple2.of(redisKey, userVisitWebEvent.getUserId()); }) .returns(new TypeHint<Tuple2<String, String>>(){}) .addSink(new RedisSink<>(conf, new RedisSaddSinkMapper())); env.execute("Redis Set UV Stat"); }
Example 3
Source File: TuningKeyedStateDeduplication.java From flink-learning with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws Exception{ final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); env.enableCheckpointing(TimeUnit.MINUTES.toMillis(10)); env.setParallelism(6); RocksDBStateBackend rocksDBStateBackend = new RocksDBStateBackend("hdfs:///flink/checkpoints", enableIncrementalCheckpointing); rocksDBStateBackend.setNumberOfTransferingThreads(numberOfTransferingThreads); rocksDBStateBackend.setPredefinedOptions(PredefinedOptions.SPINNING_DISK_OPTIMIZED_HIGH_MEM); rocksDBStateBackend.enableTtlCompactionFilter(); env.setStateBackend(rocksDBStateBackend); CheckpointConfig checkpointConf = env.getCheckpointConfig(); checkpointConf.setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE); checkpointConf.setMinPauseBetweenCheckpoints(TimeUnit.MINUTES.toMillis(8)); checkpointConf.setCheckpointTimeout(TimeUnit.MINUTES.toMillis(20)); checkpointConf.enableExternalizedCheckpoints(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION); Properties props = new Properties(); props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, DeduplicationExampleUtil.broker_list); props.put(ConsumerConfig.GROUP_ID_CONFIG, "keyed-state-deduplication"); FlinkKafkaConsumerBase<String> kafkaConsumer = new FlinkKafkaConsumer011<>( DeduplicationExampleUtil.topic, new SimpleStringSchema(), props) .setStartFromLatest(); env.addSource(kafkaConsumer) .map(string -> GsonUtil.fromJson(string, UserVisitWebEvent.class)) // 反序列化 JSON // 这里将日志的主键 id 通过 murmur3_128 hash 后,将生成 long 类型数据当做 key .keyBy((KeySelector<UserVisitWebEvent, Long>) log -> Hashing.murmur3_128(5).hashUnencodedChars(log.getId()).asLong()) .addSink(new KeyedStateDeduplication.KeyedStateSink()); env.execute("TuningKeyedStateDeduplication"); }
Example 4
Source File: KeyedStateDeduplication.java From flink-learning with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws Exception{ StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); env.setParallelism(6); // 使用 RocksDBStateBackend 做为状态后端,并开启增量 Checkpoint RocksDBStateBackend rocksDBStateBackend = new RocksDBStateBackend( "hdfs:///flink/checkpoints", true); rocksDBStateBackend.setNumberOfTransferingThreads(3); // 设置为机械硬盘+内存模式,强烈建议为 RocksDB 配备 SSD rocksDBStateBackend.setPredefinedOptions( PredefinedOptions.SPINNING_DISK_OPTIMIZED_HIGH_MEM); rocksDBStateBackend.enableTtlCompactionFilter(); env.setStateBackend(rocksDBStateBackend); // Checkpoint 间隔为 10 分钟 env.enableCheckpointing(TimeUnit.MINUTES.toMillis(10)); // 配置 Checkpoint CheckpointConfig checkpointConf = env.getCheckpointConfig(); checkpointConf.setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE); checkpointConf.setMinPauseBetweenCheckpoints(TimeUnit.MINUTES.toMillis(8)); checkpointConf.setCheckpointTimeout(TimeUnit.MINUTES.toMillis(20)); checkpointConf.enableExternalizedCheckpoints( CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION); // Kafka Consumer 配置 Properties props = new Properties(); props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, DeduplicationExampleUtil.broker_list); props.put(ConsumerConfig.GROUP_ID_CONFIG, "keyed-state-deduplication"); FlinkKafkaConsumerBase<String> kafkaConsumer = new FlinkKafkaConsumer011<>( DeduplicationExampleUtil.topic, new SimpleStringSchema(), props) .setStartFromGroupOffsets(); env.addSource(kafkaConsumer) .map(log -> GsonUtil.fromJson(log, UserVisitWebEvent.class)) // 反序列化 JSON .keyBy((KeySelector<UserVisitWebEvent, String>) UserVisitWebEvent::getId) .addSink(new KeyedStateSink()); env.execute("KeyedStateDeduplication"); }
Example 5
Source File: UnionListStateExample.java From flink-learning with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws Exception { final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); // 1 分钟一次CheckPoint env.enableCheckpointing(TimeUnit.SECONDS.toMillis(15)); env.setParallelism(3); CheckpointConfig checkpointConf = env.getCheckpointConfig(); // CheckPoint 语义 EXACTLY ONCE checkpointConf.setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE); checkpointConf.enableExternalizedCheckpoints(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION); Properties props = new Properties(); props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, UnionListStateUtil.broker_list); props.put(ConsumerConfig.GROUP_ID_CONFIG, "app-pv-stat"); FlinkKafkaConsumer011<String> kafkaConsumer011 = new FlinkKafkaConsumer011<>( // kafka topic, String 序列化 UnionListStateUtil.topic, new SimpleStringSchema(), props); env.addSource(kafkaConsumer011) .uid(UnionListStateUtil.topic) .addSink(new MySink()) .uid("MySink") .name("MySink"); env.execute("Flink unionListState"); }
Example 6
Source File: HyperLogLogUvExample.java From flink-learning with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws Exception { final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); env.enableCheckpointing(TimeUnit.MINUTES.toMillis(1)); env.setParallelism(2); CheckpointConfig checkpointConf = env.getCheckpointConfig(); checkpointConf.setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE); checkpointConf.enableExternalizedCheckpoints(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION); Properties props = new Properties(); props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, UvExampleUtil.broker_list); props.put(ConsumerConfig.GROUP_ID_CONFIG, "app-uv-stat"); FlinkKafkaConsumerBase<String> kafkaConsumer = new FlinkKafkaConsumer011<>( UvExampleUtil.topic, new SimpleStringSchema(), props) .setStartFromLatest(); FlinkJedisPoolConfig conf = new FlinkJedisPoolConfig .Builder().setHost("192.168.30.244").build(); env.addSource(kafkaConsumer) .map(string -> { // 反序列化 JSON UserVisitWebEvent userVisitWebEvent = GsonUtil.fromJson( string, UserVisitWebEvent.class); // 生成 Redis key,格式为 日期_pageId,如: 20191026_0 String redisKey = userVisitWebEvent.getDate() + "_" + userVisitWebEvent.getPageId(); return Tuple2.of(redisKey, userVisitWebEvent.getUserId()); }) .returns(new TypeHint<Tuple2<String, String>>(){}) .addSink(new RedisSink<>(conf, new RedisPfaddSinkMapper())); env.execute("Redis Set UV Stat"); }
Example 7
Source File: MapStateUvExample.java From flink-learning with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws Exception { final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); env.enableCheckpointing(TimeUnit.MINUTES.toMillis(1)); env.setParallelism(2); CheckpointConfig checkpointConf = env.getCheckpointConfig(); checkpointConf.setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE); checkpointConf.enableExternalizedCheckpoints(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION); Properties props = new Properties(); props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, UvExampleUtil.broker_list); props.put(ConsumerConfig.GROUP_ID_CONFIG, "app-uv-stat"); FlinkKafkaConsumerBase<String> kafkaConsumer = new FlinkKafkaConsumer011<>( UvExampleUtil.topic, new SimpleStringSchema(), props) .setStartFromGroupOffsets(); FlinkJedisPoolConfig conf = new FlinkJedisPoolConfig .Builder().setHost("192.168.30.244").build(); env.addSource(kafkaConsumer) .map(string -> GsonUtil.fromJson(string, UserVisitWebEvent.class)) // 反序列化 JSON .keyBy("date","pageId") // 按照 日期和页面 进行 keyBy .map(new RichMapFunction<UserVisitWebEvent, Tuple2<String, Long>>() { // 存储当前 key 对应的 userId 集合 private MapState<String,Boolean> userIdState; // 存储当前 key 对应的 UV 值 private ValueState<Long> uvState; @Override public Tuple2<String, Long> map(UserVisitWebEvent userVisitWebEvent) throws Exception { // 初始化 uvState if(null == uvState.value()){ uvState.update(0L); } // userIdState 中不包含当前访问的 userId,说明该用户今天还未访问过该页面 // 则将该 userId put 到 userIdState 中,并把 UV 值 +1 if(!userIdState.contains(userVisitWebEvent.getUserId())){ userIdState.put(userVisitWebEvent.getUserId(),null); uvState.update(uvState.value() + 1); } // 生成 Redis key,格式为 日期_pageId,如: 20191026_0 String redisKey = userVisitWebEvent.getDate() + "_" + userVisitWebEvent.getPageId(); System.out.println(redisKey + " ::: " + uvState.value()); return Tuple2.of(redisKey, uvState.value()); } @Override public void open(Configuration parameters) throws Exception { super.open(parameters); // 从状态中恢复 userIdState userIdState = getRuntimeContext().getMapState( new MapStateDescriptor<>("userIdState", TypeInformation.of(new TypeHint<String>() {}), TypeInformation.of(new TypeHint<Boolean>() {}))); // 从状态中恢复 uvState uvState = getRuntimeContext().getState( new ValueStateDescriptor<>("uvState", TypeInformation.of(new TypeHint<Long>() {}))); } }) .addSink(new RedisSink<>(conf, new RedisSetSinkMapper())); env.execute("Redis Set UV Stat"); }
Example 8
Source File: RedisSetUvExample.java From flink-learning with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws Exception { final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); env.enableCheckpointing(TimeUnit.MINUTES.toMillis(1)); env.setParallelism(2); CheckpointConfig checkpointConf = env.getCheckpointConfig(); checkpointConf.setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE); checkpointConf.enableExternalizedCheckpoints(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION); Properties props = new Properties(); props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, UvExampleUtil.broker_list); props.put(ConsumerConfig.GROUP_ID_CONFIG, "app-uv-stat"); FlinkKafkaConsumerBase<String> kafkaConsumer = new FlinkKafkaConsumer011<>( UvExampleUtil.topic, new SimpleStringSchema(), props) .setStartFromGroupOffsets(); FlinkJedisPoolConfig conf = new FlinkJedisPoolConfig .Builder().setHost("192.168.30.244").build(); env.addSource(kafkaConsumer) .map(string -> { // 反序列化 JSON UserVisitWebEvent userVisitWebEvent = GsonUtil.fromJson( string, UserVisitWebEvent.class); // 生成 Redis key,格式为 日期_pageId,如: 20191026_0 String redisKey = userVisitWebEvent.getDate() + "_" + userVisitWebEvent.getPageId(); return Tuple2.of(redisKey, userVisitWebEvent.getUserId()); }) .returns(new TypeHint<Tuple2<String, String>>(){}) .addSink(new RedisSink<>(conf, new RedisSaddSinkMapper())); env.execute("Redis Set UV Stat"); }
Example 9
Source File: TuningKeyedStateDeduplication.java From flink-learning with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws Exception{ final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); env.enableCheckpointing(TimeUnit.MINUTES.toMillis(10)); env.setParallelism(6); RocksDBStateBackend rocksDBStateBackend = new RocksDBStateBackend("hdfs:///flink/checkpoints", enableIncrementalCheckpointing); rocksDBStateBackend.setNumberOfTransferingThreads(numberOfTransferingThreads); rocksDBStateBackend.setPredefinedOptions(PredefinedOptions.SPINNING_DISK_OPTIMIZED_HIGH_MEM); rocksDBStateBackend.enableTtlCompactionFilter(); env.setStateBackend(rocksDBStateBackend); CheckpointConfig checkpointConf = env.getCheckpointConfig(); checkpointConf.setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE); checkpointConf.setMinPauseBetweenCheckpoints(TimeUnit.MINUTES.toMillis(8)); checkpointConf.setCheckpointTimeout(TimeUnit.MINUTES.toMillis(20)); checkpointConf.enableExternalizedCheckpoints(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION); Properties props = new Properties(); props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, DeduplicationExampleUtil.broker_list); props.put(ConsumerConfig.GROUP_ID_CONFIG, "keyed-state-deduplication"); FlinkKafkaConsumerBase<String> kafkaConsumer = new FlinkKafkaConsumer011<>( DeduplicationExampleUtil.topic, new SimpleStringSchema(), props) .setStartFromLatest(); env.addSource(kafkaConsumer) .map(string -> GsonUtil.fromJson(string, UserVisitWebEvent.class)) // 反序列化 JSON // 这里将日志的主键 id 通过 murmur3_128 hash 后,将生成 long 类型数据当做 key .keyBy((KeySelector<UserVisitWebEvent, Long>) log -> Hashing.murmur3_128(5).hashUnencodedChars(log.getId()).asLong()) .addSink(new KeyedStateDeduplication.KeyedStateSink()); env.execute("TuningKeyedStateDeduplication"); }
Example 10
Source File: KeyedStateDeduplication.java From flink-learning with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws Exception{ StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); env.setParallelism(6); // 使用 RocksDBStateBackend 做为状态后端,并开启增量 Checkpoint RocksDBStateBackend rocksDBStateBackend = new RocksDBStateBackend( "hdfs:///flink/checkpoints", true); rocksDBStateBackend.setNumberOfTransferingThreads(3); // 设置为机械硬盘+内存模式,强烈建议为 RocksDB 配备 SSD rocksDBStateBackend.setPredefinedOptions( PredefinedOptions.SPINNING_DISK_OPTIMIZED_HIGH_MEM); rocksDBStateBackend.enableTtlCompactionFilter(); env.setStateBackend(rocksDBStateBackend); // Checkpoint 间隔为 10 分钟 env.enableCheckpointing(TimeUnit.MINUTES.toMillis(10)); // 配置 Checkpoint CheckpointConfig checkpointConf = env.getCheckpointConfig(); checkpointConf.setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE); checkpointConf.setMinPauseBetweenCheckpoints(TimeUnit.MINUTES.toMillis(8)); checkpointConf.setCheckpointTimeout(TimeUnit.MINUTES.toMillis(20)); checkpointConf.enableExternalizedCheckpoints( CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION); // Kafka Consumer 配置 Properties props = new Properties(); props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, DeduplicationExampleUtil.broker_list); props.put(ConsumerConfig.GROUP_ID_CONFIG, "keyed-state-deduplication"); FlinkKafkaConsumerBase<String> kafkaConsumer = new FlinkKafkaConsumer011<>( DeduplicationExampleUtil.topic, new SimpleStringSchema(), props) .setStartFromGroupOffsets(); env.addSource(kafkaConsumer) .map(log -> GsonUtil.fromJson(log, UserVisitWebEvent.class)) // 反序列化 JSON .keyBy((KeySelector<UserVisitWebEvent, String>) UserVisitWebEvent::getId) .addSink(new KeyedStateSink()); env.execute("KeyedStateDeduplication"); }
Example 11
Source File: UnionListStateExample.java From flink-learning with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws Exception { final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); // 1 分钟一次CheckPoint env.enableCheckpointing(TimeUnit.SECONDS.toMillis(15)); env.setParallelism(3); CheckpointConfig checkpointConf = env.getCheckpointConfig(); // CheckPoint 语义 EXACTLY ONCE checkpointConf.setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE); checkpointConf.enableExternalizedCheckpoints(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION); Properties props = new Properties(); props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, UnionListStateUtil.broker_list); props.put(ConsumerConfig.GROUP_ID_CONFIG, "app-pv-stat"); FlinkKafkaConsumer011<String> kafkaConsumer011 = new FlinkKafkaConsumer011<>( // kafka topic, String 序列化 UnionListStateUtil.topic, new SimpleStringSchema(), props); env.addSource(kafkaConsumer011) .uid(UnionListStateUtil.topic) .addSink(new MySink()) .uid("MySink") .name("MySink"); env.execute("Flink unionListState"); }
Example 12
Source File: PvStatLocalKeyByExactlyOnce.java From flink-learning with Apache License 2.0 | 4 votes |
public static void main(String[] args) throws Exception { final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); // 1 分钟一次 Checkpoint env.enableCheckpointing(TimeUnit.MINUTES.toMillis(1)); env.setParallelism(2); CheckpointConfig checkpointConf = env.getCheckpointConfig(); // Checkpoint 语义 EXACTLY ONCE checkpointConf.setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE); checkpointConf.enableExternalizedCheckpoints(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION); Properties props = new Properties(); props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, PvStatExactlyOnceKafkaUtil.broker_list); props.put(ConsumerConfig.GROUP_ID_CONFIG, "app-pv-stat"); FlinkKafkaConsumerBase<String> appKafkaConsumer = new FlinkKafkaConsumer011<>( // kafka topic, String 序列化 PvStatExactlyOnceKafkaUtil.topic, new SimpleStringSchema(), props).setStartFromLatest(); env.addSource(appKafkaConsumer) .flatMap(new LocalKeyByFlatMap(10)) // 按照 appId 进行 keyBy .keyBy((KeySelector<Tuple2<String, Long>, String>) appIdPv -> appIdPv.f0) .map(new RichMapFunction<Tuple2<String, Long>, Tuple2<String, Long>>() { private ValueState<Long> pvState; private long pv = 0; @Override public void open(Configuration parameters) throws Exception { super.open(parameters); // 初始化状态 pvState = getRuntimeContext().getState( new ValueStateDescriptor<>("pvStat", TypeInformation.of(new TypeHint<Long>() { }))); } @Override public Tuple2<String, Long> map(Tuple2<String, Long> tuple2) throws Exception { // 从状态中获取该 app 的pv值,加上新收到的 pv 值以后后,update 到状态中 if (null == pvState.value()) { log.info("{} is new, PV is {}", tuple2.f0, tuple2.f1); pv = tuple2.f1; } else { pv = pvState.value(); pv += tuple2.f1; log.info("{} is old, PV is {}", tuple2.f0, pv); } pvState.update(pv); tuple2.setField(pv, 1); return tuple2; } }) .print(); env.execute("Flink pv stat LocalKeyBy"); }
Example 13
Source File: MapStateUvExample.java From flink-learning with Apache License 2.0 | 4 votes |
public static void main(String[] args) throws Exception { final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); env.enableCheckpointing(TimeUnit.MINUTES.toMillis(1)); env.setParallelism(2); CheckpointConfig checkpointConf = env.getCheckpointConfig(); checkpointConf.setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE); checkpointConf.enableExternalizedCheckpoints(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION); Properties props = new Properties(); props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, UvExampleUtil.broker_list); props.put(ConsumerConfig.GROUP_ID_CONFIG, "app-uv-stat"); FlinkKafkaConsumerBase<String> kafkaConsumer = new FlinkKafkaConsumer011<>( UvExampleUtil.topic, new SimpleStringSchema(), props) .setStartFromGroupOffsets(); FlinkJedisPoolConfig conf = new FlinkJedisPoolConfig .Builder().setHost("192.168.30.244").build(); env.addSource(kafkaConsumer) .map(string -> GsonUtil.fromJson(string, UserVisitWebEvent.class)) // 反序列化 JSON .keyBy("date","pageId") // 按照 日期和页面 进行 keyBy .map(new RichMapFunction<UserVisitWebEvent, Tuple2<String, Long>>() { // 存储当前 key 对应的 userId 集合 private MapState<String,Boolean> userIdState; // 存储当前 key 对应的 UV 值 private ValueState<Long> uvState; @Override public Tuple2<String, Long> map(UserVisitWebEvent userVisitWebEvent) throws Exception { // 初始化 uvState if(null == uvState.value()){ uvState.update(0L); } // userIdState 中不包含当前访问的 userId,说明该用户今天还未访问过该页面 // 则将该 userId put 到 userIdState 中,并把 UV 值 +1 if(!userIdState.contains(userVisitWebEvent.getUserId())){ userIdState.put(userVisitWebEvent.getUserId(),null); uvState.update(uvState.value() + 1); } // 生成 Redis key,格式为 日期_pageId,如: 20191026_0 String redisKey = userVisitWebEvent.getDate() + "_" + userVisitWebEvent.getPageId(); System.out.println(redisKey + " ::: " + uvState.value()); return Tuple2.of(redisKey, uvState.value()); } @Override public void open(Configuration parameters) throws Exception { super.open(parameters); // 从状态中恢复 userIdState userIdState = getRuntimeContext().getMapState( new MapStateDescriptor<>("userIdState", TypeInformation.of(new TypeHint<String>() {}), TypeInformation.of(new TypeHint<Boolean>() {}))); // 从状态中恢复 uvState uvState = getRuntimeContext().getState( new ValueStateDescriptor<>("uvState", TypeInformation.of(new TypeHint<Long>() {}))); } }) .addSink(new RedisSink<>(conf, new RedisSetSinkMapper())); env.execute("Redis Set UV Stat"); }
Example 14
Source File: PvStatLocalKeyByExactlyOnce.java From flink-learning with Apache License 2.0 | 4 votes |
public static void main(String[] args) throws Exception { final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); // 1 分钟一次 Checkpoint env.enableCheckpointing(TimeUnit.MINUTES.toMillis(1)); env.setParallelism(2); CheckpointConfig checkpointConf = env.getCheckpointConfig(); // Checkpoint 语义 EXACTLY ONCE checkpointConf.setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE); checkpointConf.enableExternalizedCheckpoints(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION); Properties props = new Properties(); props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, PvStatExactlyOnceKafkaUtil.broker_list); props.put(ConsumerConfig.GROUP_ID_CONFIG, "app-pv-stat"); FlinkKafkaConsumerBase<String> appKafkaConsumer = new FlinkKafkaConsumer011<>( // kafka topic, String 序列化 PvStatExactlyOnceKafkaUtil.topic, new SimpleStringSchema(), props).setStartFromLatest(); env.addSource(appKafkaConsumer) .flatMap(new LocalKeyByFlatMap(10)) // 按照 appId 进行 keyBy .keyBy((KeySelector<Tuple2<String, Long>, String>) appIdPv -> appIdPv.f0) .map(new RichMapFunction<Tuple2<String, Long>, Tuple2<String, Long>>() { private ValueState<Long> pvState; private long pv = 0; @Override public void open(Configuration parameters) throws Exception { super.open(parameters); // 初始化状态 pvState = getRuntimeContext().getState( new ValueStateDescriptor<>("pvStat", TypeInformation.of(new TypeHint<Long>() { }))); } @Override public Tuple2<String, Long> map(Tuple2<String, Long> tuple2) throws Exception { // 从状态中获取该 app 的pv值,加上新收到的 pv 值以后后,update 到状态中 if (null == pvState.value()) { log.info("{} is new, PV is {}", tuple2.f0, tuple2.f1); pv = tuple2.f1; } else { pv = pvState.value(); pv += tuple2.f1; log.info("{} is old, PV is {}", tuple2.f0, pv); } pvState.update(pv); tuple2.setField(pv, 1); return tuple2; } }) .print(); env.execute("Flink pv stat LocalKeyBy"); }