当前位置:   article > 正文

java flink使用addSink方法保存流数据到redis_addsink()

addsink()

博主把核心的内容写在最前面,其他内容和完整的代码放在最后面哈:

pom配置

        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>3.0.1</version>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

主要代码

create_data.MyData2create_data.MyDataSource2;在后面哈

package write_to_redis;

import create_data.MyData2;
import create_data.MyDataSource2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class RedisSinkMainTest {
    public static void main(String[] args) throws Exception {
        final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        DataStreamSource<MyData2> sourceStream = env.addSource(new MyDataSource2()); // 得到数据源
        sourceStream.addSink(new RedisSink()); // 核心!保存到redis
        sourceStream.print();
        env.execute("Flink_to_mysql demo");
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

可以看到,使用流.addSink()就可以保存流的数据了,java使用redis,会依赖Jedis这个包,保存的内容自定义,代码如下:

package write_to_redis;

import create_data.MyData2;
import org.apache.flink.streaming.api.functions.sink.RichSinkFunction;
import redis.clients.jedis.Jedis;
import org.apache.flink.configuration.Configuration;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;

public class RedisSink extends RichSinkFunction<MyData2> {
    private Jedis jedis = null;

    @Override
    public void open(Configuration parameters) throws Exception {
        super.open(parameters);
        String host = "127.0.0.1";
        int port = 6379;
        JedisPool pool = new JedisPool(host, port);
        jedis = pool.getResource();
    }

    @Override
    public void invoke(MyData2 value, Context context) {
        HashMap<String, String> map = new HashMap<>();
        map.put("timestamp", String.valueOf(value.getTimestamp()));
        map.put("num", String.valueOf(value.getNum()));
        map.put("value_list", String.valueOf(value.getValueList()[0]));
        String key = String.valueOf(value.getTimestamp());
        jedis.hmset(key, map); // 保存的格式是key:时间戳,value: 全部的内容
        jedis.expire(key, 5); // 生存周期 5s (5s后在redis数据库中删除)
    }

    @Override
    public void close() throws Exception {
        super.close();
        jedis.close();
        if (jedis != null) {
            jedis.close();
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

只需要覆写openinvokeclose三个函数即可,一个用于打开连接,一个用于执行操作,一个用于关闭连接。

其他内容:MyData2类,与生成数据源的类MyDataSource2

数据类与生成数据的类请参考:https://blog.csdn.net/weixin_35757704/article/details/120626180

MyData2.java

package create_data;

import java.util.Arrays;

public class MyData2 {
    public int keyId;
    public long timestamp;
    public int num;
    public double[] valueList;

    public MyData2() {
    }

    public MyData2(int accountId, long timestamp, int num, double[] valueList) {
        this.keyId = accountId;
        this.timestamp = timestamp;
        this.num = num;
        this.valueList = valueList;
    }

    public long getKeyId() {
        return keyId;
    }

    public void setKeyId(int keyId) {
        this.keyId = keyId;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public double[] getValueList() {
        return valueList;
    }

    public void setValueList(double[] valueList) {
        this.valueList = valueList;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    @Override
    public String toString() {
        return "MyData{" +
                "keyId=" + keyId +
                ", timestamp=" + timestamp +
                ", num=" + num +
                ", valueList= " + Arrays.toString(valueList) +
                '}';
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62

MyDataSource2.java

package create_data;

import org.apache.flink.streaming.api.functions.source.SourceFunction;

import java.util.Random;

public class MyDataSource2 implements SourceFunction<MyData2> {
    // 定义标志位,用来控制数据的产生
    private boolean isRunning = true;
    private final Random random = new Random(0);

    @Override
    public void run(SourceContext ctx) throws Exception {
        while (isRunning) {
            ctx.collect(new MyData2(random.nextInt(3), System.currentTimeMillis(), 1, new double[]{random.nextDouble()}));
            Thread.sleep(1000L); // 1s生成1个数据
        }
    }

    @Override
    public void cancel() {
        isRunning = false;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/615098
推荐阅读
相关标签
  

闽ICP备14008679号