private void initClient() throws UnknownHostException { Settings.Builder settingsBuilder = Settings.builder(); settingsBuilder.put( Settings.Builder.EMPTY_SETTINGS ); meta.getSettingsMap().entrySet().stream().forEach( ( s ) -> settingsBuilder.put( s.getKey(), environmentSubstitute( s.getValue() ) ) ); PreBuiltTransportClient tClient = new PreBuiltTransportClient( settingsBuilder.build() ); for ( Server server : meta.getServers() ) { tClient.addTransportAddress( new TransportAddress( InetAddress.getByName( environmentSubstitute( server.getAddress() ) ), server.getPort() ) ); } client = tClient; /** With the upgrade to elasticsearch 6.3.0, removed the NodeBuilder, * which was removed from the elasticsearch 5.0 API, see: * https://www.elastic.co/guide/en/elasticsearch/reference/5.0/breaking_50_java_api_changes * .html#_nodebuilder_removed */ }
@Inject public ElasticsearchClient(ElasticsearchTableDescriptionProvider descriptions, ElasticsearchConnectorConfig config) throws IOException { tableDescriptions = requireNonNull(descriptions, "description is null"); ElasticsearchConnectorConfig configuration = requireNonNull(config, "config is null"); requestTimeout = configuration.getRequestTimeout(); maxAttempts = configuration.getMaxRequestRetries(); maxRetryTime = configuration.getMaxRetryTime(); for (ElasticsearchTableDescription tableDescription : tableDescriptions.getAllTableDescriptions()) { if (!clients.containsKey(tableDescription.getClusterName())) { Settings settings = Settings.builder().put("cluster.name", tableDescription.getClusterName()).build(); TransportAddress address = new TransportAddress(InetAddress.getByName(tableDescription.getHost()), tableDescription.getPort()); TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(address); clients.put(tableDescription.getClusterName(), client); } } this.columnMetadataCache = CacheBuilder.newBuilder() .expireAfterWrite(30, MINUTES) .refreshAfterWrite(15, MINUTES) .maximumSize(500) .build(asyncReloading(CacheLoader.from(this::loadColumns), executor)); }
public ElasticsearchQueryBuilder(List<ElasticsearchColumnHandle> columnHandles, ElasticsearchConnectorConfig config, ElasticsearchSplit split) { requireNonNull(columnHandles, "columnHandles is null"); requireNonNull(config, "config is null"); requireNonNull(split, "split is null"); columns = columnHandles; tupleDomain = split.getTupleDomain(); index = split.getIndex(); shard = split.getShard(); type = split.getType(); Settings settings = Settings.builder() .put("client.transport.ignore_cluster_name", true) .build(); try { InetAddress address = InetAddress.getByName(split.getSearchNode()); client = new PreBuiltTransportClient(settings) .addTransportAddress(new TransportAddress(address, split.getPort())); } catch (UnknownHostException e) { throw new PrestoException(ELASTICSEARCH_CONNECTION_ERROR, format("Error connecting to search node (%s:%d)", split.getSearchNode(), split.getPort()), e); } scrollTimeout = config.getScrollTimeout(); scrollSize = config.getScrollSize(); }
.addTransportAddress(new InetSocketTransportAddress( InetAddress.getByName("localhost"), 9300)); } catch (UnknownHostException e) {
client.addTransportAddress( new TransportAddress( InetAddress.getByName( transMeta.environmentSubstitute( server.getAddress() ) ), server.getPort() ) );
@Test public void transportClientClusterHealth() { try (ElasticsearchContainer container = new ElasticsearchContainer()) { container.start(); TransportAddress transportAddress = new TransportAddress(container.getTcpHost()); String expectedClusterName = "docker-cluster"; Settings settings = Settings.builder().put("cluster.name", expectedClusterName).build(); try (TransportClient transportClient = new PreBuiltTransportClient(settings) .addTransportAddress(transportAddress)) { ClusterHealthResponse healths = transportClient.admin().cluster().prepareHealth().get(); String clusterName = healths.getClusterName(); assertThat(clusterName, is(expectedClusterName)); } } }
@Bean public TransportClient getTransportClient() throws Exception { Settings settings = Settings.builder().put("cluster.name", clusterName) .put("client.transport.sniff", true) .build(); transportClient = new PreBuiltTransportClient(settings) .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), Integer.valueOf(port))); logger.info("elasticsearch transportClient 连接成功"); return transportClient; }
public static void main(String[] args) throws Exception{ //这里有个Settings 等后面讲到集群再详解 TransportClient client = new PreBuiltTransportClient(Settings.EMPTY) .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ConnectTest.host),ConnectTest.port)); System.out.println(client); client.close(); } }
private static Client getClient(String hostname, int transportPort) { Settings settings = Settings.builder() .put("client.transport.ignore_cluster_name", true) .build(); try { return new PreBuiltTransportClient(settings).addTransportAddress( new InetSocketTransportAddress(InetAddress.getByName(hostname), transportPort)); } catch (UnknownHostException e) { throw new RuntimeException(e); } }
public ElasticsearchServiceImpl(EsConf esConf) throws UnknownHostException { String clusterName = esConf.getClusterName(); String address = esConf.getAddress(); String[] hostPort = address.split(":"); logger.info("Connect to elasticsearch {}:{}", clusterName, address); Settings settings = Settings.builder().put("cluster.name", clusterName) .put("client.transport.sniff", true) .build(); transportClient = new PreBuiltTransportClient(settings) .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostPort[0]), Integer.valueOf(hostPort[1]))); logger.info("Complete the connection to elasticsearch"); }
@Bean public TransportClient elasticSearchClient() throws Exception { return new PreBuiltTransportClient(Settings.builder().put("client.transport.ignore_cluster_name", true).build()) .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(elasticSearchHost), elasticSearchPort)); } }
@Bean public TransportClient elasticSearchClient() throws Exception { return new PreBuiltTransportClient(Settings.builder().put("client.transport.ignore_cluster_name", true).build()) .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(elasticSearchHost), elasticSearchPort)); } }
/** * 获取连接 * @throws Exception */ @Before public void getClient() throws Exception{ client = new PreBuiltTransportClient(Settings.EMPTY) .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(EsTest.host),EsTest.port)); }
public ESHelper(String clusterName, String host, int port) throws UnknownHostException { Settings settings = Settings.builder().put("cluster.name", clusterName) .build(); this.client = new PreBuiltTransportClient(settings) .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), port)); }
public ESHelper(String clusterName, String host) throws UnknownHostException { Settings settings = Settings.builder().put("cluster.name", clusterName) .build(); this.client = new PreBuiltTransportClient(settings) .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), 9300)); }
@Bean public TransportClient transportClient(){ Settings settings = Settings.EMPTY; if(!EmptyUtils.isEmpty(clusterName)){ settings = Settings.builder() .put("cluster.name", clusterName) .build(); } try { transportClient = new PreBuiltTransportClient(settings) .addTransportAddress(new TransportAddress(InetAddress.getByName(host), port)); } catch (UnknownHostException e) { logger.error("创建elasticsearch客户端失败"); e.printStackTrace(); } logger.info("创建elasticsearch客户端成功"); return transportClient; }
@Profile("production") @Bean(destroyMethod = "close") @SuppressWarnings("all") public TransportClient getTransportClient() throws UnknownHostException { return new PreBuiltTransportClient(Settings.EMPTY) .addTransportAddress(new TransportAddress(InetAddress.getByName(props.getClients().getHostname()), props.getClients().getTransportPort())); }
@SuppressWarnings({"unchecked", "resource"}) public static void main(String[] args) throws Exception { // 先构建client,两个参数分别是:cluster.name 固定参数代表后面参数的含义,集群名称 // client.transport.sniff 表示设置自动探查集群的集群节点 Settings settings = Settings.builder() .put("cluster.name", "youmeek-cluster") .put("client.transport.sniff", true) .build(); //单个节点的写法 TransportClient transportClient = new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.1.127"), 9300)); //batchCreate(transportClient); query(transportClient); transportClient.close(); }
private void prepareClient() { Builder settingsBuilder = applySettings(); try { TransportAddress ista = new TransportAddress(InetAddress.getByName(config.getElastic().getHost()), config.getElastic().getPort()); client = new PreBuiltTransportClient(settingsBuilder.build()) .addTransportAddress(ista); } catch (UnknownHostException ex) { logger.error(ex.getMessage(), ex); System.exit(-1); } }
@Override public void before() throws Throwable { embeddedElastic.start(); client = new PreBuiltTransportClient(Settings.builder().put(CLUSTER_NAME, CLUSTER_NAME_VALUE).build()) .addTransportAddress( new TransportAddress(InetAddress.getByName("localhost"), embeddedElastic.getTransportTcpPort())); }