java flink使用addSink方法保存流数据到redis

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

文章目录

  • pom配置
  • 主要代码
  • 其他内容:MyData2类,与生成数据源的类MyDataSource2

pom配置

        <dependency>
            <groupId>redis.clientsgroupId>
            <artifactId>jedisartifactId>
            <version>3.0.1version>
        dependency>

主要代码

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");
    }
}

可以看到,使用流.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();
        }
    }
}

只需要覆写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) +
                '}';
    }
}

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;
    }
}

你可能感兴趣的:(java,大数据,flink,mysql,python)