Skip to content

Commit

Permalink
reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
agapple committed May 22, 2024
1 parent 5ff6449 commit aa05d8e
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ public void insert(ESMapping mapping, Object pkVal, Map<String, Object> esFieldD
if (mapping.getId() != null) {
String parentVal = (String) esFieldData.remove("$parent_routing");
if (mapping.isUpsert()) {
ESUpdateRequest updateRequest = esConnection.new ES8xUpdateRequest(mapping.getIndex(),
pkVal.toString()).setDoc(esFieldData).setDocAsUpsert(true);
ESUpdateRequest updateRequest = esConnection.new ES8xUpdateRequest(mapping.getIndex(), pkVal.toString())
.setDoc(esFieldData)
.setDocAsUpsert(true);
if (StringUtils.isNotEmpty(parentVal)) {
updateRequest.setRouting(parentVal);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
package com.alibaba.otter.canal.client.adapter.es8x.support;

import com.alibaba.otter.canal.client.adapter.es.core.support.ESBulkRequest;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyStore;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import java.util.Arrays;
import java.util.Map;

import javax.net.ssl.SSLContext;

import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
Expand Down Expand Up @@ -32,20 +47,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyStore;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import java.util.Arrays;
import java.util.Map;
import com.alibaba.otter.canal.client.adapter.es.core.support.ESBulkRequest;

/**
* ES 连接器, 只支持 Rest 方式
Expand All @@ -59,14 +61,15 @@ public class ESConnection {

private RestHighLevelClient restHighLevelClient;

public ESConnection(String[] hosts, Map<String, String> properties) throws UnknownHostException {
public ESConnection(String[] hosts, Map<String, String> properties) throws UnknownHostException{
String caPath = properties.get("security.ca.path");
if (StringUtils.isNotEmpty(caPath)) {
connectEsWithCa(hosts, properties, caPath);
} else {
connectEsWithoutCa(hosts, properties);
}
}

private void connectEsWithCa(String[] hosts, Map<String, String> properties, String caPath) {
Path caCertificatePath = Paths.get(caPath);
try (InputStream is = Files.newInputStream(caCertificatePath)) {
Expand All @@ -75,8 +78,7 @@ private void connectEsWithCa(String[] hosts, Map<String, String> properties, Str
KeyStore trustStore = KeyStore.getInstance("pkcs12");
trustStore.load(null, null);
trustStore.setCertificateEntry("ca", trustedCa);
SSLContextBuilder sslContextBuilder = SSLContexts.custom()
.loadTrustMaterial(trustStore, null);
SSLContextBuilder sslContextBuilder = SSLContexts.custom().loadTrustMaterial(trustStore, null);
final SSLContext sslContext = sslContextBuilder.build();

HttpHost[] httpHosts = Arrays.stream(hosts).map(this::createHttpHost).toArray(HttpHost[]::new);
Expand All @@ -86,13 +88,15 @@ private void connectEsWithCa(String[] hosts, Map<String, String> properties, Str
String[] nameAndPwdArr = nameAndPwd.split(":");
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(nameAndPwdArr[0], nameAndPwdArr[1]));
new UsernamePasswordCredentials(nameAndPwdArr[0], nameAndPwdArr[1]));
restClientBuilder.setHttpClientConfigCallback(httpClientBuilder -> {
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
return httpClientBuilder.setSSLContext(sslContext);
});
}
restHighLevelClient = new RestHighLevelClientBuilder(restClientBuilder.build()).setApiCompatibilityMode(true).build();
restHighLevelClient = new RestHighLevelClientBuilder(restClientBuilder.build())
.setApiCompatibilityMode(true)
.build();
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand All @@ -106,12 +110,12 @@ private void connectEsWithoutCa(String[] hosts, Map<String, String> properties)
String[] nameAndPwdArr = nameAndPwd.split(":");
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(nameAndPwdArr[0], nameAndPwdArr[1]));
new UsernamePasswordCredentials(nameAndPwdArr[0], nameAndPwdArr[1]));
restClientBuilder.setHttpClientConfigCallback(
httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
}
restHighLevelClient = new RestHighLevelClientBuilder(restClientBuilder.build()).setApiCompatibilityMode(true)
.build();
.build();
}

public void close() {
Expand Down Expand Up @@ -147,9 +151,9 @@ public class ES8xIndexRequest implements ESBulkRequest.ESIndexRequest {

private IndexRequestBuilder indexRequestBuilder;

private IndexRequest indexRequest;
private IndexRequest indexRequest;

public ES8xIndexRequest(String index, String id) {
public ES8xIndexRequest(String index, String id){
indexRequest = new IndexRequest(index);
indexRequest.id(id);

Expand Down Expand Up @@ -190,9 +194,9 @@ public class ES8xUpdateRequest implements ESBulkRequest.ESUpdateRequest {

private UpdateRequestBuilder updateRequestBuilder;

private UpdateRequest updateRequest;
private UpdateRequest updateRequest;

public ES8xUpdateRequest(String index, String id) {
public ES8xUpdateRequest(String index, String id){

updateRequest = new UpdateRequest(index, id);
}
Expand Down Expand Up @@ -239,9 +243,9 @@ public class ES8xDeleteRequest implements ESBulkRequest.ESDeleteRequest {

private DeleteRequestBuilder deleteRequestBuilder;

private DeleteRequest deleteRequest;
private DeleteRequest deleteRequest;

public ES8xDeleteRequest(String index, String id) {
public ES8xDeleteRequest(String index, String id){

deleteRequest = new DeleteRequest(index, id);

Expand All @@ -268,11 +272,11 @@ public class ESSearchRequest {

private SearchRequestBuilder searchRequestBuilder;

private SearchRequest searchRequest;
private SearchRequest searchRequest;

private SearchSourceBuilder sourceBuilder;
private SearchSourceBuilder sourceBuilder;

public ESSearchRequest(String index) {
public ESSearchRequest(String index){

searchRequest = new SearchRequest(index);
sourceBuilder = new SearchSourceBuilder();
Expand Down Expand Up @@ -325,12 +329,10 @@ public class ES8xBulkRequest implements ESBulkRequest {

private BulkRequestBuilder bulkRequestBuilder;

private BulkRequest bulkRequest;

public ES8xBulkRequest() {
private BulkRequest bulkRequest;

public ES8xBulkRequest(){
bulkRequest = new BulkRequest();

}

public void resetBulk() {
Expand Down Expand Up @@ -398,7 +400,7 @@ public static class ES8xBulkResponse implements ESBulkRequest.ESBulkResponse {

private BulkResponse bulkResponse;

public ES8xBulkResponse(BulkResponse bulkResponse) {
public ES8xBulkResponse(BulkResponse bulkResponse){
this.bulkResponse = bulkResponse;
}

Expand Down Expand Up @@ -438,7 +440,7 @@ private HttpHost createHttpHost(String uriStr) {
}
try {
return HttpHost.create(new URI(uri
.getScheme(), null, uri.getHost(), uri.getPort(), uri.getPath(), uri.getQuery(), uri.getFragment())
.getScheme(), null, uri.getHost(), uri.getPort(), uri.getPath(), uri.getQuery(), uri.getFragment())
.toString());
} catch (URISyntaxException ex) {
throw new IllegalStateException(ex);
Expand Down
Loading

0 comments on commit aa05d8e

Please sign in to comment.