Search in sources :

Example 1 with Elasticsearch7SinkBuilder

use of org.apache.flink.connector.elasticsearch.sink.Elasticsearch7SinkBuilder in project flink by apache.

the class Elasticsearch7SinkExample method main.

public static void main(String[] args) throws Exception {
    final ParameterTool parameterTool = ParameterTool.fromArgs(args);
    if (parameterTool.getNumberOfParameters() < 3) {
        System.out.println("Missing parameters!\n" + "Usage: --numRecords <numRecords> --index <index> --type <type>");
        return;
    }
    final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    env.enableCheckpointing(5000);
    DataStream<String> source = env.fromSequence(0, parameterTool.getInt("numRecords") - 1).map((MapFunction<Long, String>) value -> "message #" + value);
    source.sinkTo(new Elasticsearch7SinkBuilder<String>().setBulkFlushMaxActions(1).setHosts(new HttpHost("127.0.0.1", 9200, "http")).setEmitter((element, context, indexer) -> indexer.add(createIndexRequest(element, parameterTool))).build());
    env.execute("Elasticsearch7.x end to end sink test example");
}
Also used : ParameterTool(org.apache.flink.api.java.utils.ParameterTool) DataStream(org.apache.flink.streaming.api.datastream.DataStream) ParameterTool(org.apache.flink.api.java.utils.ParameterTool) IndexRequest(org.elasticsearch.action.index.IndexRequest) Map(java.util.Map) HashMap(java.util.HashMap) Elasticsearch7SinkBuilder(org.apache.flink.connector.elasticsearch.sink.Elasticsearch7SinkBuilder) Requests(org.elasticsearch.client.Requests) HttpHost(org.apache.http.HttpHost) MapFunction(org.apache.flink.api.common.functions.MapFunction) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) HttpHost(org.apache.http.HttpHost) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)

Example 2 with Elasticsearch7SinkBuilder

use of org.apache.flink.connector.elasticsearch.sink.Elasticsearch7SinkBuilder in project flink by apache.

the class Elasticsearch7SinkExample method main.

public static void main(String[] args) throws Exception {
    final ParameterTool parameterTool = ParameterTool.fromArgs(args);
    if (parameterTool.getNumberOfParameters() < 2) {
        System.out.println("Missing parameters!\n" + "Usage: --numRecords <numRecords> --index <index>");
        return;
    }
    final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    env.enableCheckpointing(5000);
    DataStream<Tuple2<String, String>> source = env.fromSequence(0, parameterTool.getInt("numRecords") - 1).flatMap(new FlatMapFunction<Long, Tuple2<String, String>>() {

        @Override
        public void flatMap(Long value, Collector<Tuple2<String, String>> out) {
            final String key = String.valueOf(value);
            final String message = "message #" + value;
            out.collect(Tuple2.of(key, message + "update #1"));
            out.collect(Tuple2.of(key, message + "update #2"));
        }
    });
    ElasticsearchSink<Tuple2<String, String>> sink = new Elasticsearch7SinkBuilder<Tuple2<String, String>>().setHosts(new HttpHost("127.0.0.1", 9200, "http")).setEmitter((element, ctx, indexer) -> {
        indexer.add(createIndexRequest(element.f1, parameterTool));
        indexer.add(createUpdateRequest(element, parameterTool));
    }).setBulkFlushMaxActions(// emit after every element, don't buffer
    1).build();
    source.sinkTo(sink);
    env.execute("Elasticsearch 7.x end to end sink test example");
}
Also used : ParameterTool(org.apache.flink.api.java.utils.ParameterTool) Tuple2(org.apache.flink.api.java.tuple.Tuple2) HttpHost(org.apache.http.HttpHost) Elasticsearch7SinkBuilder(org.apache.flink.connector.elasticsearch.sink.Elasticsearch7SinkBuilder) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)

Aggregations

ParameterTool (org.apache.flink.api.java.utils.ParameterTool)2 Elasticsearch7SinkBuilder (org.apache.flink.connector.elasticsearch.sink.Elasticsearch7SinkBuilder)2 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)2 HttpHost (org.apache.http.HttpHost)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 MapFunction (org.apache.flink.api.common.functions.MapFunction)1 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)1 DataStream (org.apache.flink.streaming.api.datastream.DataStream)1 IndexRequest (org.elasticsearch.action.index.IndexRequest)1 Requests (org.elasticsearch.client.Requests)1