Created
November 27, 2018 11:24
-
-
Save michaelkeevildown/15f392941f3cfd78139a7886ce926a21 to your computer and use it in GitHub Desktop.
Flink (v1.6) Elasticsearch (v6.x) Sink -- with auth
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// -- ** ELASTICSEARCH | |
List<HttpHost> httpHost = new ArrayList<>(); | |
httpHost.add(new HttpHost("127.0.0.1", 9200, "http")); | |
ElasticsearchSink.Builder<String> esSinkBuilder = new ElasticsearchSink.Builder<>( | |
httpHost, | |
new ElasticsearchSinkFunction<String>() { | |
public IndexRequest createIndexRequest(String element) { | |
// Map<String, String> json = new HashMap<>(); | |
// json.put("data", element); | |
// System.out.println(json); | |
return Requests.indexRequest() | |
.index("tweets") | |
.type("tweet") | |
.source(element, XContentType.JSON); | |
} | |
@Override | |
public void process(String element, RuntimeContext ctx, RequestIndexer indexer) { | |
indexer.add(createIndexRequest(element)); | |
} | |
} | |
); | |
// configuration for the bulk requests; this instructs the sink to emit after every element, otherwise they would be buffered | |
esSinkBuilder.setBulkFlushMaxActions(1); | |
// provide a RestClientFactory for custom configuration on the internally created REST client | |
esSinkBuilder.setRestClientFactory( | |
restClientBuilder -> { | |
restClientBuilder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { | |
@Override | |
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { | |
// elasticsearch username and password | |
CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); | |
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("$USERNAME", "$PASSWORD")); | |
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); | |
} | |
}); | |
} | |
); | |
// finally, build and add the sink to the job's pipeline | |
jsonTweets.addSink(esSinkBuilder.build()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Additional info can be found on StackOverflow here