Flink數據寫入Elastic Search

需要注意的是,flink連接es時,端口號需要使用tcp端口(一般es默認http端口爲9200,tcp端口爲9300)。

代碼如下:

    //es配置
    val config = new java.util.HashMap[String, String]
    config.put("bulk.flush.max.actions", "1")
    config.put("cluster.name", "your_name")
    val transportAddresses = new java.util.ArrayList[InetSocketAddress]
    transportAddresses.add(new InetSocketAddress(InetAddress.getByName("host1"), 9300))
    transportAddresses.add(new InetSocketAddress(InetAddress.getByName("host2"), 9300))
    transportAddresses.add(new InetSocketAddress(InetAddress.getByName("host3"), 9300))
    transportAddresses.add(new InetSocketAddress(InetAddress.getByName("host4"), 9300))
    transportAddresses.add(new InetSocketAddress(InetAddress.getByName("host5"), 9300))

    resultStream.addSink(new ElasticsearchSink(config, transportAddresses, new ElasticsearchSinkFunction[(String, Int, Int, Long, Long)] {
      def createIndexRequest(element: (String, Int, Int, Long, Long)): IndexRequest = {
        val dataMap = new java.util.HashMap[String, Any]
        val keys = element._1.split("_")
        val date = keys(0)
        val productId = keys(1)
        val channelId = keys(2)
        val carriorType = keys(3)
        val provinceName = keys(4)
        dataMap.put("date", date)
        dataMap.put("productId", productId)
        dataMap.put("channelId", channelId)
        dataMap.put("carriorType", carriorType)
        dataMap.put("provinceName", provinceName)
        dataMap.put("pv", element._2)
        dataMap.put("uv", element._3)
        val idCompact: String = date + productId + channelId + carriorType + provinceName
        val id: String = DataUtils.simpleEncode(idCompact)
        return Requests.indexRequest()
          .index("your_index")
          .`type`("your_type")
          .id(id)
          .source(dataMap)
      }
      def process(element: (String, Int, Int, Long, Long), ctx: RuntimeContext, indexer: RequestIndexer): Unit = {
        indexer.add(createIndexRequest(element))
      }
    }))

pom文件:

<dependency>
   <groupId>org.apache.flink</groupId>
   <artifactId>flink-connector-elasticsearch5_2.10</artifactId>
   <version>1.3.3</version>
</dependency>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章