flink可以实现从支持的数据库或者文件中采集数据到redis中
public RetDTO pushToRedis(List<RtNode> rtNodesList) throws Exception {
//开始执行
String rediskey = "flink:" + UUID.randomUUID();
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.setRestartStrategy(RestartStrategies.noRestart());
// 本地环境提交
env.setParallelism(1);
StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);
//创建表环境
Table table = null;
// 根据自己项目的逻辑编写可执行的Flink语句
// todo
// 执行flink语句
//解析table
ResolvedSchema resolvedSchema = table.getResolvedSchema();
List<String> columnNames = resolvedSchema.getColumnNames();
DataStream<Row> rowDataStream = tableEnv.toChangelogStream(table);
SingleOutputStreamOperator<Tuple2<String, String>> test = rowDataStream
.flatMap((FlatMapFunction<Row, Tuple2<String, String>>) (row, collector) -> {
JSONObject jsonObject = new JSONObject(true);
columnNames.forEach(field -> jsonObject.put(field, null == row.getField(field) ? "" : row.getField(field)));
Tuple2<String, String> t = new Tuple2<>(rediskey, jsonObject.toJSONString());
collector.collect(t);
})
.returns(Types.TUPLE(Types.STRING, Types.STRING));
//定义redis服务器信息
FlinkJedisPoolConfig conf = new FlinkJedisPoolConfig.Builder()
.setHost("127.0.0.1")
.setPort(6479)
.setPassword("redis_pwd")
.setDatabase(0)
.build();
test.addSink(new RedisSink<>(conf, new SinkRedisMapper()));
env.execute();
return RetDTO.ok().data(rediskey);
}