flink如何写入es

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录


前言

Flink sink 流数据写入到es5和es7的简单示例。


一、写入到Elasticsearch5

  • pom maven依赖
xml 复制代码
        <dependency>
            <groupId>org.apache.flink</groupId>
            <artifactId>flink-connector-elasticsearch5_2.11</artifactId>
            <version>${flink.version}</version>
        </dependency>
  • 代码如下(示例):
java 复制代码
public class Es5SinkDemo {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        Row row=Row.of("张三","001",getTimestamp("2016-10-24 21:59:06"));
        Row row2=Row.of("张三","002",getTimestamp("2016-10-24 21:50:06"));
        Row row3=Row.of("张三","002",getTimestamp("2016-10-24 21:51:06"));
        Row row4=Row.of("李四","003",getTimestamp("2016-10-24 21:50:56"));
        Row row5=Row.of("李四","004",getTimestamp("2016-10-24 00:48:36"));
        Row row6=Row.of("王五","005",getTimestamp("2016-10-24 00:48:36"));
        DataStreamSource<Row> source =env.fromElements(row,row2,row3,row4,row5,row6);

        Map<String, String> config = new HashMap<>();
//        config.put("cluster.name", "my-cluster-name");
//        config.put("bulk.flush.max.actions", "1");

        List<InetSocketAddress> transportAddresses = new ArrayList<>();

        transportAddresses.add(new InetSocketAddress(InetAddress.getByName("10.68.8.60"), 9300));
        //Sink操作
        DataStreamSink<Row> rowDataStreamSink = source.addSink(new ElasticsearchSink<>(config, transportAddresses, new ElasticsearchSinkFunction<Row>() {
            public IndexRequest createIndexRequest(Row element) {
                Map<String, Object> json = new HashMap<>();
                json.put("name22", element.getField(0).toString());
                json.put("no22", element.getField(1));
                json.put("age", 34);
                json.put("create_time", element.getField(2));

                return Requests.indexRequest()
                        .index("cc")
                        .type("mtype")
                        .id(element.getField(1).toString())
                        .source(json);
            }

            @Override
            public void process(Row element, RuntimeContext ctx, RequestIndexer indexer) {
                //利用requestIndexer进行发送请求,写入数据
                indexer.add(createIndexRequest(element));
            }
        }));
        env.execute("es demo");
    }
    private static java.sql.Timestamp getTimestamp(String str) throws Exception {
//		String string = "2016-10-24 21:59:06";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        java.util.Date date=sdf.parse(str);
        java.sql.Timestamp s = new java.sql.Timestamp(date.getTime());
        return s;
    }

二、写入到Elasticsearch7

  • pom maven依赖
xml 复制代码
        <dependency>
            <groupId>org.apache.flink</groupId>
            <artifactId>flink-connector-elasticsearch7_2.11</artifactId>
            <version>${flink.version}</version>
            <scope>provided</scope>
        </dependency>
  • 代码如下(示例):
java 复制代码
import org.apache.flink.api.common.functions.RuntimeContext;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.elasticsearch.ElasticsearchSinkFunction;
import org.apache.flink.streaming.connectors.elasticsearch.RequestIndexer;
import org.apache.flink.streaming.connectors.elasticsearch7.ElasticsearchSink;
import org.apache.flink.types.Row;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.Requests;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class EsSinkDemo {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        Row row=Row.of("张三","001",getTimestamp("2016-10-24 21:59:06"));
        Row row2=Row.of("张三","002",getTimestamp("2016-10-24 21:50:06"));
        Row row3=Row.of("张三","002",getTimestamp("2016-10-24 21:51:06"));
        Row row4=Row.of("李四","003",getTimestamp("2016-10-24 21:50:56"));
        Row row5=Row.of("李四","004",getTimestamp("2016-10-24 00:48:36"));
        Row row6=Row.of("王五","005",getTimestamp("2016-10-24 00:48:36"));
        DataStreamSource<Row> source =env.fromElements(row,row2,row3,row4,row5,row6);

        Map<String, String> config = new HashMap<>();
//        config.put("cluster.name", "my-cluster-name");
// This instructs the sink to emit after every element, otherwise they would be buffered
//        config.put("bulk.flush.max.actions", "1");

        List<HttpHost> hosts = new ArrayList<>();
        hosts.add(new HttpHost("10.68.8.69",9200,"http"));

        ElasticsearchSink.Builder<Row> esSinkBuilder = new ElasticsearchSink.Builder<Row>(hosts,new ElasticsearchSinkFunction<Row>() {
            public IndexRequest createIndexRequest(Row element) {
                Map<String, Object> json = new HashMap<>();
                json.put("name22", element.getField(0).toString());
                json.put("no22", element.getField(1));
                json.put("age", 34);
//                json.put("create_time", element.getField(2));

                return Requests.indexRequest()
                        .index("cc")
                        .id(element.getField(1).toString())
                        .source(json);
            }

            @Override
            public void process(Row element, RuntimeContext ctx, RequestIndexer indexer) {
                //利用requestIndexer进行发送请求,写入数据
                indexer.add(createIndexRequest(element));
            }
        });
        esSinkBuilder.setBulkFlushMaxActions(100);
        //Sink操作
        source.addSink(esSinkBuilder.build());

        env.execute("es demo");
    }

    private static java.sql.Timestamp getTimestamp(String str) throws Exception {
//		String string = "2016-10-24 21:59:06";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        java.util.Date date=sdf.parse(str);
        java.sql.Timestamp s = new java.sql.Timestamp(date.getTime());
        return s;
    }
}

总结

flink写入es5和es7 的区别是引入不同的flink-connector-elasticsearch,es7已没有type的概念故无需再设置type。

相关推荐
万米商云16 分钟前
企业物资集采平台解决方案:跨地域、多仓库、百部门——大型企业如何用一套系统管好百万级物资?
大数据·运维·人工智能
BigData共享23 分钟前
极致性能背后的黑科技?这个世上没有“银弹”!(三)
大数据
阿里云大数据AI技术24 分钟前
Flink Forward Asia 2025 主旨演讲精彩回顾
大数据·人工智能·flink
y_y_liang37 分钟前
图生生AI商品换背景,高效商拍!
大数据·人工智能·ai·ai作画
王小王-1231 小时前
基于Hadoop的用户购物行为可视化分析系统设计与实现
大数据·hadoop·分布式·用户购物行为·电商日志分析
沐尘而生1 小时前
【AI智能体】智能音视频-硬件设备基于 WebSocket 实现语音交互
大数据·人工智能·websocket·机器学习·ai作画·音视频·娱乐
贝格前端工场1 小时前
小程序订阅消息设计:用户触达与隐私保护的平衡法则
大数据·小程序
成都极云科技2 小时前
成都算力租赁新趋势:H20 八卡服务器如何重塑 AI 产业格局?
大数据·服务器·人工智能·云计算·gpu算力
典学长编程3 小时前
高效学习之一篇搞定分布式管理系统Git !
大数据·git·搜索引擎
YFJ_mily5 小时前
2025第二届机电一体化、机器人与控制系统国际会议(MRCS2025)即将来袭
大数据·人工智能·机器人·机电一体化