flink-1.12.0消费kafka写入clickhouse-20.8.20.1

clickhouse集群搭建 请观看我上一篇文章

DataStream方式写入clickhouse - java

方法主入口

public class DataClean {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment bsEnv = StreamExecutionEnvironment.getExecutionEnvironment();
        Properties properties = new Properties();
        properties.setProperty("bootstrap.servers", "172.17.2.73:9092,172.17.2.74:9092,172.17.2.75:9092");
        properties.setProperty("group.id", "flink_20210902");
        DataStream<String> stream = bsEnv.addSource(new FlinkKafkaConsumer<>("test_topic", new SimpleStringSchema(), properties));

        DataStream<NewsPo> stream2 = stream.map(line -> {
            JSONObject jb = new JSONObject(line);
            NewsPo news = null;
            try {
                news = new NewsPo(jb.getString("key"), jb.getString("title"), jb.getString("file1"), jb.getString("field2").substring(0,10));
            } catch (Exception e) {
                news = new NewsPo();
            }
            return news;
        });
        MyClickhouseSink myClickhouseSink = new MyClickhouseSink();
        myClickhouseSink.setSql("insert into clj_test.news2 values(?,?,?,?)");
        stream2.addSink(myClickhouseSink);

        bsEnv.execute("flink clickhouse sink");


    }
}

自定义clickhouse sink

public class MyClickhouseSink extends RichSinkFunction<NewsPo> implements Serializable {
    Connection conn = null;
    String sql = null;

    public void setSql(String sql) {
        this.sql = sql;
    }

    @Override
    public void open(Configuration parameters) throws Exception {
        super.open(parameters);
        conn = ClickHouseUtil.getConn();
    }

    @Override
    public void close() throws Exception {
        super.close();
        if (conn != null) {
            conn.close();
        }
    }

    @Override
    public void invoke(NewsPo news) throws SQLException {
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        preparedStatement.setString(1, news.getKey());
        preparedStatement.setString(2, news.getTitle());
        preparedStatement.setString(3, news.getPublish_time());
        preparedStatement.setString(4, news.getPublish_date());
        preparedStatement.addBatch();
        preparedStatement.executeBatch();
    }
}
public class ClickHouseUtil {

    private static Connection connection;

    public static Connection getConn() throws SQLException, ClassNotFoundException {
        Class.forName("ru.yandex.clickhouse.ClickHouseDriver");
        String address = "jdbc:clickhouse://172.17.2.144:8123/clj_test" ;
        connection = DriverManager.getConnection(address);
        return connection;
    }

    public void close() throws SQLException {
        connection.close();
    }

}

结果展示

插入的是分布式表 4个节点的数据量是一致的
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值