如果我们去阅读一下Flink是如何将DataStream中的数据写入ElasticSearch,你会发现其就是在使用bulk API。我顺便将这个类改写成支持将DataSet写入到ElasticSearch中了,也就是实现了ElasticSearchOutputFormat
,目前代码我开源到GitHub:https://github.com/397090770/flink-elasticsearch2-connector。编译的包已经上传到Maven中央仓库(可以参见《如何发布Jar包到Maven中央仓库》),这也就意味着你可以直接在pom.xml
文件中使用我那个ElasticSearchOutputFormat
:
< dependency > < groupId >com.iteblog</ groupId > < artifactId >flink-elasticsearch2-connector</ artifactId > < version >1.0.2</ version > </ dependency > |
在Scala中使用
import scala.collection.JavaConversions. _ val config = Map( "bulk.flush.max.actions" -> "1000" , "cluster.name" -> "elasticsearch" ) val hosts = "www.iteblog.com" val transports = hosts.split( "," ).map(host = > new InetSocketAddress(InetAddress.getByName(host), 9300 )).toList val data : DataSet[String] = .... data.output( new ElasticSearchOutputFormat(config, transports, new ElasticsearchSinkFunction[String] { def createIndexRequest(element : String) : IndexRequest = { Requests.indexRequest.index( "iteblog" ).` type `( "info" ).source(element) } override def process(element : String, ctx : RuntimeContext, indexer : RequestIndexer) { indexer.add(createIndexRequest(element)) } })) |
在Java中使用
Map<String, String> config = new HashMap<>(); config.put( "bulk.flush.max.actions" , "1000" ); config.put( "cluster.name" , "elasticsearch" ); String hosts = "www.iteblog.com" ; List<InetSocketAddress> list = Lists.newArrayList(); for (String host : hosts.split( "," )) { list.add( new InetSocketAddress(InetAddress.getByName(host), 9300 )); } DataSet<String> data = ....; data.output( new ElasticSearchOutputFormat<>(config, list, new ElasticsearchSinkFunction<String>() { @Override public void process(String element, RuntimeContext ctx, RequestIndexer indexer) { indexer.add(createIndexRequest(element)); } private IndexRequest createIndexRequest(String element) { return Requests.indexRequest().index( "iteblog" ).type( "info" ).source(element); } })); |
我在同样的环境下测试了写入1,172,235条数据到ElasticSearch中,这次我只使用了20s不到!可见效率提高不少啊。
这个ElasticSearchOutputFormat
目前只支持写入到Elasticsearch: 2.x.x,后面我会再写个支持ElasticSearch 1.x.x的版本。
本文来自博客园,作者:大码王,转载请注明原文链接:https://www.cnblogs.com/huanghanyu/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具