文章18 | 阅读 10838 | 点赞0
Sink | 语义保证 | 备注 |
---|---|---|
hdfs | exactly once | |
elasticsearch | at least once | |
kafka produce | at least once/exactly once | Kafka 0.9 and 0.10提供at least once Kafka 0.11提供exactly once |
file | at least once | |
redis | at least once |
需要添加依赖
Maven仓库
搜索 flink-connector-redis 依赖:
<dependency>
<groupId>org.apache.bahir</groupId>
<artifactId>flink-connector-redis_${scala.version}</artifactId>
<version>1.0</version>
</dependency>
完整程序:
package com.Streaming.custormSink;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.redis.RedisSink;
import org.apache.flink.streaming.connectors.redis.common.config.FlinkJedisPoolConfig;
import org.apache.flink.streaming.connectors.redis.common.mapper.RedisCommand;
import org.apache.flink.streaming.connectors.redis.common.mapper.RedisCommandDescription;
import org.apache.flink.streaming.connectors.redis.common.mapper.RedisMapper;
/**
* @Author: Henry
* @Description: 接收socket数据,把数据保存到redis中(list格式)
* 保存到Redis中数据一般采用两种格式:list或hashmap
*
* lpush list_key value
* @Date: Create in 2019/5/12 22:29
**/
public class StreamingDemoToRedis {
public static void main(String[] args) throws Exception{
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
DataStreamSource<String> text = env.socketTextStream(
"master", 9000, "\n");
//input: l_words word , 其中 l_words 代表 list 类型
//对数据进行组装,把string转化为tuple2<String,String>
DataStream<Tuple2<String, String>> l_wordsData = text.map(
new MapFunction<String, Tuple2<String, String>>() {
@Override
public Tuple2<String, String> map(String value) throws Exception {
return new Tuple2<>("l_words", value);
}
});
//创建redis的配置
FlinkJedisPoolConfig conf = new FlinkJedisPoolConfig.Builder()
.setHost("master").setPort(6379).build();
//创建redissink
RedisSink<Tuple2<String, String>> redisSink = new RedisSink<>(
conf, new MyRedisMapper());
l_wordsData.addSink(redisSink);
env.execute("StreamingDemoToRedis");
}
public static class MyRedisMapper implements RedisMapper<Tuple2<String, String>> {
//表示从接收的数据中获取需要操作的redis key
@Override
public String getKeyFromData(Tuple2<String, String> data) {
return data.f0;
}
//表示从接收的数据中获取需要操作的redis value
@Override
public String getValueFromData(Tuple2<String, String> data) {
return data.f1;
}
@Override
public RedisCommandDescription getCommandDescription() {
return new RedisCommandDescription(RedisCommand.LPUSH);
}
}
}
完整代码如下:
package cn.Streaming.custormSink
import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment
import org.apache.flink.streaming.connectors.redis.RedisSink
import org.apache.flink.streaming.connectors.redis.common.config.FlinkJedisPoolConfig
import org.apache.flink.streaming.connectors.redis.common.mapper.{RedisCommand, RedisCommandDescription, RedisMapper}
/**
* @Author: Henry
* @Description:
* @Date: Create in 2019/5/14 22:37
**/
object StreamingDataToRedisScala {
def main(args: Array[String]): Unit = {
//获取socket端口号
val port = 9000
//获取运行环境
val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment
//链接socket获取输入数据
val text = env.socketTextStream("master",port,'\n')
//注意:必须要添加这一行隐式转行,否则下面的flatmap方法执行会报错
import org.apache.flink.api.scala._
val l_wordsData = text.map(line=>
("l_words_scala",line))
val conf = new FlinkJedisPoolConfig.Builder()
.setHost("master")
.setPort(6379)
.build()
val redisSink = new RedisSink[Tuple2[String,String]](conf,new MyRedisMapper)
l_wordsData.addSink(redisSink)
//执行任务
env.execute("Socket window count")
}
class MyRedisMapper extends RedisMapper[Tuple2[String,String]]{
override def getKeyFromData(data: (String, String)) = {
data._1
}
override def getValueFromData(data: (String, String)) = {
data._2
}
override def getCommandDescription = {
new RedisCommandDescription(RedisCommand.LPUSH) // 具体操作命令
}
}
}
先在一个终端启动redis server服务:
./src/redis-server
再在另一个终端连接服务:
./src/redis-cli
开启socket终端:
nc -l 9000
在IDEA中点击“Run”运行代码:
通过 nc 终端输入数据,查询 redis 数据库:
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/hongzhen91/article/details/90146906
内容来源于网络,如有侵权,请联系作者删除!