flink写入starrocks案例

StarRocks Connector

		<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.20</version>
        </dependency>
        <dependency>
            <groupId>org.apache.flink</groupId>
            <artifactId>flink-connector-jdbc_2.12</artifactId>
            <version>1.13.5</version>
        </dependency>

        <dependency>
            <groupId>com.starrocks</groupId>
            <artifactId>flink-connector-starrocks</artifactId>
            <version>1.1.16_flink-1.13_2.12</version>
        </dependency>

StarRocksSinkExample

import com.starrocks.connector.flink.StarRocksSink;
import com.starrocks.connector.flink.table.StarRocksSinkOptions;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.DataTypes;
import org.apache.flink.table.api.TableSchema;

public class StarRocksSinkExample {

    static class Book {
        public Book(Long id, String title, String authors, Integer year) {
            this.id = id;
            this.title = title;
            this.authors = authors;
            this.year = year;
        }
        final Long id;
        final String title;
        final String authors;
        final Integer year;
    }

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.fromElements(
                new Book(101L, "Stream Processing with Apache Flink", "Fabian Hueske, Vasiliki Kalavri", 2019),
                new Book(102L, "Streaming Systems", "Tyler Akidau, Slava Chernyak, Reuven Lax", 2018),
                new Book(103L, "Designing Data-Intensive Applications", "Martin Kleppmann", 2017),
                new Book(104L, "Kafka: The Definitive Guide", "Gwen Shapira, Neha Narkhede, Todd Palino", 2017)
        ).addSink(
                StarRocksSink.sink(
                        TableSchema.builder()
                                .field("id", DataTypes.BIGINT())
                                .field("title", DataTypes.VARCHAR(100))
                                .field("authors", DataTypes.VARCHAR(100))
                                .field("year", DataTypes.INT())
                                .build(),
                        StarRocksSinkOptions.builder()
                                .withProperty("connector","starrocks")
                                .withProperty("jdbc-url","jdbc:mysql://10.x.x.x:9030?characterEncoding=utf-8&useSSL=false")
                                .withProperty("load-url","10.x.x.x:8030;10.x.x.x:8030;10.x.x.x:8030")
                                .withProperty("username","root")
                                .withProperty("password","pass")
                                .withProperty("table-name","book")
                                .withProperty("database-name","database_name")
                                .withProperty("sink.properties.column_separator", "\\x01")
                                .withProperty("sink.properties.row_delimiter", "\\x02")
                                .withProperty("sink.buffer-flush.interval-ms", "10000")
                                .build(),
                        (slot,book)->{
                            slot[0] = book.id;
                            slot[1] = book.title;
                            slot[2] = book.authors;
                            slot[3] = book.year;
                        }
                )
        );

        env.execute();
    }
}
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Java中使用Flink写入Elasticsearch(Es)可以通过以下几个步骤实现: 1. 配置Elasticsearch的连接信息:首先,需要在Java应用程序中配置Elasticsearch的连接信息,包括Elasticsearch的主机名、端口号和集群名称。 2. 创建Elasticsearch连接:Flink提供了ElasticsearchSink类,用于与Elasticsearch建立连接。通过创建ElasticsearchSink类的实例,并使用之前配置的连接信息初始化它,即可建立与Elasticsearch的连接。 3. 定义Elasticsearch的索引和类型:在将数据写入Elasticsearch之前,需要定义要写入的索引名称和类型名称。可以通过调用ElasticsearchSink类的withBulkFlushMaxActions()方法指定每个批次写入的最大记录数。 4. 将数据写入Elasticsearch:在Flink应用程序的数据流中,可以使用addSink()方法将ElasticsearchSink实例添加为数据流的下游。然后,可以使用ElasticsearchSink类的invoke()方法将数据写入Elasticsearch。 5. 启动Flink应用程序:在配置好Elasticsearch连接和数据写入逻辑后,可以使用Flink的ExecutionEnvironment或StreamExecutionEnvironment启动Flink应用程序。 6. 监控写入情况:在Flink应用程序运行期间,可以通过ElasticsearchSink类提供的方法来监控数据写入情况,包括写入成功和失败的记录数。 以上就是在Java中使用Flink写入Elasticsearch的步骤。通过配置Elasticsearch连接信息、创建连接、定义索引和类型、添加ElasticsearchSink实例以及监控写入情况,可以实现将Flink流处理的数据写入Elasticsearch的功能。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值