flink 例子(scala)

import org.apache.flink.api.common.functions.RuntimeContext

import org.apache.flink.api.common.serialization.SimpleStringSchema

import org.apache.flink.api.java.utils.ParameterTool

import org.apache.flink.api.scala._

import org.apache.flink.runtime.state.filesystem.FsStateBackend

import org.apache.flink.streaming.api.TimeCharacteristic

import org.apache.flink.streaming.api.scala.DataStream

import org.apache.flink.streaming.connectors.elasticsearch.{ElasticsearchSinkFunction, RequestIndexer}

import org.apache.flink.streaming.connectors.elasticsearch7.ElasticsearchSink

import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer

import org.elasticsearch.action.DocWriteRequest

import org.elasticsearch.action.index.IndexRequest

import org.elasticsearch.client.Requests

object demo{

def main(args: Array[String]): Unit = {

val env = StreamExecutionEnvironment.getExecutionEnvironment

//需要状态开启下面的配置

//env.setStateBackend(new RocksDBStateBackend(s"hdfs://${namenodeID}", true))//hdfs 作为状态后端

//env.enableCheckpointing(10 * 60 * 1000L)

//env.getCheckpointConfig.setCheckpointTimeout(10 * 60 * 1000L)

env.setStreamTimeCharacteristic(TimeCharacteristic.ProcessingTime) //处理时间

val props = new Properties

props.setProperty("bootstrap.servers", "host:6667")//有些是9092端口

props.setProperty("group.id", "groupId")

props.setProperty("retries", "10")

props.setProperty("retries.backoff.ms", "100")

props.put(ConsumerConfig.REQUEST_TIMEOUT_MS_CONFIG, "60000")

//是否配置了权限,有的话加上下面的配置

// props.setProperty("sasl.jaas.config","org.apache.kafka.common.security.plain.PlainLoginModule required username='' password='';")

//props.setProperty("security.protocol", "SASL_PLAINTEXT");

// props.setProperty("sasl.mechanism", "PLAIN")

val myConsumer = new FlinkKafkaConsumer[String]("topicName", new SimpleStringSchema(), props)

.setStartFromEarliest()//从什么时间开始读

val stream = env.addSource(myConsumer)

.map(m => {

val list= m.split("\t")

val id = list(1)

val ts = list(2)

Demo(id,ts)

})

val httpHosts = CP.getESConf

val esSinkBuilder = new ElasticsearchSink.Builder[Demo](

httpHosts,

new ElasticsearchSinkFunction[Demo] {

def process(element: Demo, ctx: RuntimeContext, indexer: RequestIndexer) {

val json = new java.util.HashMap[String, String]

json.put("@timestamp", element.ts)

json.put("id", element.id)

val rqst: IndexRequest = Requests.indexRequest

//.id("自定义id,不加会自动生成")

.id(element.id)

.index("indexName")

.source(json)

.opType(DocWriteRequest.OpType.INDEX)

indexer.add(rqst)

}

}

)

setESConf(esSinkBuilder, 50000)

stream.addSink(esSinkBuilder.build())

.uid("write-to-es")

.name("write-to-es")

env.execute(s"demo")

}

def setESConf[T](esSinkBuilder: ElasticsearchSink.Builder[T], numMaxActions: Int) {

esSinkBuilder.setBulkFlushMaxActions(numMaxActions)

esSinkBuilder.setBulkFlushMaxSizeMb(10)

esSinkBuilder.setBulkFlushInterval(10000)

esSinkBuilder.setBulkFlushBackoff(true)

esSinkBuilder.setBulkFlushBackoffDelay(2)

esSinkBuilder.setBulkFlushBackoffRetries(3)

esSinkBuilder.setRestClientFactory(new RestClientFactory {

override def configureRestClientBuilder(restClientBuilder: RestClientBuilder): Unit = {

restClientBuilder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {

override def customizeRequestConfig(requestConfigBuilder: RequestConfig.Builder): RequestConfig.Builder = {

requestConfigBuilder.setConnectTimeout(12000)

requestConfigBuilder.setSocketTimeout(90000)

}

})

}

})

}

}

case class Demo(id: String, ts: String)

相关推荐
Data跳动5 小时前
Spark内存都消耗在哪里了?
大数据·分布式·spark
woshiabc1116 小时前
windows安装Elasticsearch及增删改查操作
大数据·elasticsearch·搜索引擎
lucky_syq6 小时前
Saprk和Flink的区别
大数据·flink
lucky_syq6 小时前
流式处理,为什么Flink比Spark Streaming好?
大数据·flink·spark
袋鼠云数栈6 小时前
深入浅出Flink CEP丨如何通过Flink SQL作业动态更新Flink CEP作业
大数据
小白学大数据7 小时前
如何使用Selenium处理JavaScript动态加载的内容?
大数据·javascript·爬虫·selenium·测试工具
15年网络推广青哥8 小时前
国际抖音TikTok矩阵运营的关键要素有哪些?
大数据·人工智能·矩阵
节点。csn8 小时前
Hadoop yarn安装
大数据·hadoop·分布式
arnold669 小时前
探索 ElasticSearch:性能优化之道
大数据·elasticsearch·性能优化
NiNg_1_23410 小时前
基于Hadoop的数据清洗
大数据·hadoop·分布式