Flink导入StarRocks

1、pom依赖

powershell 复制代码
  <properties>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
    <flink.version>1.13.6</flink.version>
    <scala.binary.version>2.12</scala.binary.version>
  </properties>

  <dependencies>
    <!-- Apache Flink 的依赖, 这些依赖项,生产环境可以不打包到JAR文件中. -->
    <dependency>
      <groupId>org.apache.flink</groupId>
      <artifactId>flink-java</artifactId>
      <version>${flink.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.flink</groupId>
      <artifactId>flink-streaming-java_${scala.binary.version}</artifactId>
      <version>${flink.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.flink</groupId>
      <artifactId>flink-runtime-web_${scala.binary.version}</artifactId>
      <version>${flink.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.flink</groupId>
      <artifactId>flink-table-planner_${scala.binary.version}</artifactId>
      <version>${flink.version}</version>
    </dependency>
    <!-- flink-connector-starrocks -->
    <dependency>
      <groupId>com.starrocks</groupId>
      <artifactId>flink-connector-starrocks</artifactId>
      <version>1.2.5_flink-1.13_2.12</version>
    </dependency>
  </dependencies>

2、代码编写

java 复制代码
public class LoadJsonRecords {
    public static void main(String[] args) throws Exception {
        // To run the example, you should prepare in the following steps
        // 1. create a primary key table in your StarRocks cluster. The DDL is
        //  CREATE DATABASE `test`;
        //    CREATE TABLE `test`.`score_board`
        //    (
        //        `id` int(11) NOT NULL COMMENT "",
        //        `name` varchar(65533) NULL DEFAULT "" COMMENT "",
        //        `score` int(11) NOT NULL DEFAULT "0" COMMENT ""
        //    )
        //    ENGINE=OLAP
        //    PRIMARY KEY(`id`)
        //    COMMENT "OLAP"
        //    DISTRIBUTED BY HASH(`id`)
        //    PROPERTIES(
        //        "replication_num" = "1"
        //    );
        //
        // 2. replace the connector options "jdbc-url" and "load-url" with your cluster configurations
        MultipleParameterTool params = MultipleParameterTool.fromArgs(args);
        String jdbcUrl = params.get("jdbcUrl", "jdbc:mysql://fe-ip:9030");
        String loadUrl = params.get("loadUrl", "be-ip:8040;be-ip:8040;be-ip:8040");

        //String jdbcUrl = params.get("jdbcUrl", "jdbc:mysql://fe-ip:9030");
        //String loadUrl = params.get("loadUrl", "be-ip:8040;be-ip:8040;be-ip:8040");

        //String jdbcUrl = params.get("jdbcUrl", "jdbc:mysql://fe-ip:9030");
        //String loadUrl = params.get("loadUrl", "be-ip:8040,be-ip:8040,be-ip:8040");

        final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        // Generate json-format records. Each record has three fields correspond to
        // the columns `id`, `name`, and `score` in StarRocks table.
        String[] records = new String[]{
                "{\"id\":1111, \"name\":\"starrocks-json\", \"score\":100}",
                "{\"id\":2222, \"name\":\"flink-json\", \"score\":100}",
        };
        DataStream<String> source = env.fromElements(records);

        // Configure the connector with the required properties, and you also need to add properties
        // "sink.properties.format" and "sink.properties.strip_outer_array" to tell the connector the
        // input records are json-format.
        StarRocksSinkOptions options = StarRocksSinkOptions.builder()
                .withProperty("jdbc-url", jdbcUrl)
                .withProperty("load-url", loadUrl)
                .withProperty("database-name", "tmp")
                .withProperty("table-name", "score_board")
                .withProperty("username", "")
                .withProperty("password", "")
                .withProperty("sink.properties.format", "json")
                .withProperty("sink.properties.strip_outer_array", "true")
                .withProperty("sink.parallelism","1")
                //.withProperty("sink.version","V1")
                .build();
        // Create the sink with the options
        SinkFunction<String> starRockSink = StarRocksSink.sink(options);
        source.addSink(starRockSink);

        env.execute("LoadJsonRecords");
    }
}
相关推荐
StarRocks_labs5 天前
StarRocks 如何查询 Paimon 半结构化数据?Variant、Shredding 与 SQL 实践
starrocks·sql·paimon·variant·shredding
StarRocks_labs11 天前
StarRocks × Fluss × Paimon:流湖仓分析与 ETL 闭环
starrocks·flink·jni·native·paimon·湖仓·fluss
StarRocks_labs11 天前
StarRocks 存算分离架构下的大规模实时导入优化实践
starrocks·flink·sstable·compaction·tablet·主键索引·存算分离架构
StarRocks_labs12 天前
基于 Fluss、Paimon 与 StarRocks 构建淘天集团湖流一体数据链路
starrocks·olap·schema·paimon·fluss·湖流一体
StarRocks_labs13 天前
从 6000+ Commit 中识别升级风险:一个 StarRocks AI 升级扫描工具的实现
starrocks·ai·commit·分析·claude code
StarRocks_labs13 天前
StarRocks 4.1:聚焦生产实践,持续降低运维复杂度
运维·starrocks·iceberg·schema·物化视图·tablet·存算分离架构
StarRocks_labs13 天前
StarRocks x Fluss x Paimon 湖流一体方案:构建秒级响应、湖流一体的实时数据引擎
starrocks·kafka·lambda·查询·paimon·fluss·湖流一体
StarRocks_labs14 天前
StarRocks 全模态能力导读:连接内容理解、混合检索与业务分析
starrocks·paimon·业务分析·多模态处理·混合检索·全模态能力·内容处理
大大大大晴天️1 个月前
StarRocks 的查询性能为什么快?
大数据·starrocks
大大大大晴天️1 个月前
浅析StarRocks 表设计:表类型、分布策略与索引
大数据·starrocks