Refine search
private TransportClient createTransportClient(Config config) throws UnknownHostException { TransportClient transportClient; // Set TransportClient settings Settings.Builder settingsBuilder = Settings.builder(); if (config.hasPath(ElasticsearchWriterConfigurationKeys.ELASTICSEARCH_WRITER_SETTINGS)) { settingsBuilder.put(ConfigUtils.configToProperties(config, ElasticsearchWriterConfigurationKeys.ELASTICSEARCH_WRITER_SETTINGS)); } settingsBuilder.put("client.transport.ignore_cluster_name",true); settingsBuilder.put("client.transport.sniff", true); transportClient = new PreBuiltTransportClient(settingsBuilder.build()); this.hostAddresses.forEach(transportClient::addTransportAddress); return transportClient; } }
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 */ }
protected void init() { client = new PreBuiltTransportClient(Settings.EMPTY) .addTransportAddress(new InetSocketTransportAddress( InetAddress.getByName("localhost"), 9300)); } catch (UnknownHostException e) {
Settings.Builder settingsBuilder = Settings.builder(); settingsBuilder.put( Settings.Builder.EMPTY_SETTINGS ); tempMeta.getSettingsMap().entrySet().stream().forEach( ( s ) -> settingsBuilder.put( s.getKey(), transMeta .environmentSubstitute( s.getValue() ) ) ); try ( PreBuiltTransportClient client = new PreBuiltTransportClient( settingsBuilder.build() ) ) { client.addTransportAddress( new TransportAddress( InetAddress.getByName( transMeta.environmentSubstitute( server.getAddress() ) ), server.getPort() ) ); AdminClient admin = client.admin();
Settings.Builder settingsBuilder = Settings.builder(); for (Enumeration<?> iter = parameters.propertyNames(); iter.hasMoreElements();) { String propName = (String)iter.nextElement(); if (propName.startsWith(ELASTICSEARCH_KEY_PREFIX)) { String esName = propName.substring(ELASTICSEARCH_KEY_PREFIX.length()); settingsBuilder.put(esName, parameters.getProperty(propName)); client = new PreBuiltTransportClient(settingsBuilder.build()); String transport = parameters.getProperty(TRANSPORT_KEY, DEFAULT_TRANSPORT); for (String addrStr : transport.split(",")) { clusterName = client.settings().get("cluster.name");
private TransportClient internalBuildElasticClient(String url) { String ipUrl = url.substring(ELASTIC_SEARCH_DRIVER_PREFIX.length()); Settings.Builder settingBuilder = Settings.builder(); settingBuilder.put("client.transport.sniff", true); String hostListString = ipUrl; int clusterNamePosIdx = ipUrl.lastIndexOf("/"); if (clusterNamePosIdx >= 0) { hostListString = hostListString.substring(0, clusterNamePosIdx); settingBuilder.put("cluster.name", ipUrl.substring(clusterNamePosIdx + 1)); } else { settingBuilder.put("client.transport.ignore_cluster_name", true); } List<InetSocketTransportAddress> addressList = Lists.newLinkedList(); String[] connStringList = hostListString.split(Constants.COMMA); for (String connStr : connStringList) { String[] connArr = connStr.split(Constants.COLON); if (connArr.length == 1) { addressList.add(new InetSocketTransportAddress(new InetSocketAddress(connArr[0], 9300))); } else { addressList.add(new InetSocketTransportAddress(new InetSocketAddress(connArr[0], Integer.parseInt(connArr[1])))); } } return new PreBuiltTransportClient(settingBuilder.build()) .addTransportAddresses(addressList.toArray(new InetSocketTransportAddress[addressList.size()])); }
headers.put("Authorization", authToken); transportClient = (TransportClient) ctor.newInstance(settingsBuilder.build(), new Class[0]); transportClient = new PreBuiltTransportClient(settingsBuilder.build());
Settings settings = Settings.builder() .put(CLUSTER_NAME_PARAM, esSettings.getClusterName()).build(); List<TransportAddress> addresses = new ArrayList<>(); for(String host: esSettings.getHostToPortMapping().keySet()) { TransportClient tc = new PreBuiltTransportClient(settings); tc.addTransportAddresses(addresses.toArray(socketAddresses)); client = tc; Settings indexSettings = Settings.builder() .put(NUM_OF_SHARDS_PARAM, esSettings.getShards()) .put(NUM_OF_REPLICAS_PARAM, esSettings.getReplicas()) .build(); LOGGER.debug("Index with name " + indexName + " does not exist yet. Creating one with settings: " + indexSettings.toString()); client.admin().indices().prepareCreate(indexName).setSettings(indexSettings).get();
@SuppressWarnings("resource") protected ESClient createRemoteClient(ElasticSearchClientConfig config) { Settings settings = getSetting(config).build(); log.debug("Using settings: " + settings.toDelimitedString(',')); TransportClient client = new PreBuiltTransportClient(settings); // not closed here String[] addresses = config.getOption("addressList", "").split(","); if (addresses.length == 0) { throw new IllegalArgumentException("No addressList option provided cannot connect TransportClient"); } else { for (String item : addresses) { String[] address = item.split(":"); log.debug("Add transport address: " + item); try { InetAddress inet = InetAddress.getByName(address[0]); client.addTransportAddress(new TransportAddress(inet, Integer.parseInt(address[1]))); } catch (UnknownHostException e) { log.error("Unable to resolve host " + address[0], e); } } } return new ESTransportClient(client); }
/** * Creates a new transport client with pre-installed plugins. * * @param settings the settings passed to this transport client * @param plugins a collection of additional plugins to run with this client * @param hostFailureListener a failure listener that is invoked if a node is disconnected; this can be <code>null</code> */ public PreBuiltTransportClient( Settings settings, Collection<Class<? extends Plugin>> plugins, HostFailureListener hostFailureListener) { super(settings, Settings.EMPTY, addPlugins(plugins, PRE_INSTALLED_PLUGINS), hostFailureListener); }
private ElasticClientProxy internalBuildElasticClient(String url) { String ipUrl = url.substring(ELASTIC_SEARCH_DRIVER_PREFIX.length()); Settings.Builder settingBuilder = Settings.builder(); settingBuilder.put("client.transport.sniff", true); String hostListString = ipUrl; int clusterNamePosIdx = ipUrl.lastIndexOf("/"); if (clusterNamePosIdx >= 0) { hostListString = hostListString.substring(0, clusterNamePosIdx); settingBuilder.put("cluster.name", ipUrl.substring(clusterNamePosIdx + 1)); } else { settingBuilder.put("client.transport.ignore_cluster_name", true); } List<InetSocketTransportAddress> addressList = Lists.newLinkedList(); String[] connStringList = hostListString.split(Constants.COMMA); for (String connStr : connStringList) { String[] connArr = connStr.split(Constants.COLON); if (connArr.length == 1) { addressList.add(new InetSocketTransportAddress(new InetSocketAddress(connArr[0], DEFAULT_ES_PORT))); } else { addressList.add(new InetSocketTransportAddress(new InetSocketAddress(connArr[0], Integer.parseInt(connArr[1])))); } } TransportClient transportClient = new PreBuiltTransportClient(settingBuilder.build()) .addTransportAddresses(addressList.toArray(new InetSocketTransportAddress[addressList.size()])); return (ElasticClientProxy) Proxy.newProxyInstance(ElasticClientProxy.class.getClassLoader(), new Class[]{ElasticClientProxy.class}, new CloseClientProxyInvocationHandler(transportClient)); }
/** * A method to retrieve a PreBuiltTransportClient. * * @param settings Elasticsearch settings * * @return a PreBuiltTransportClient */ public PreBuiltTransportClient getPreBuiltTransportClient(Settings settings) { return new PreBuiltTransportClient(settings); }
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(); } }
/** * Creates a new transport client with pre-installed plugins. * * @param settings the settings passed to this transport client * @param plugins a collection of additional plugins to run with this client * @param hostFailureListener a failure listener that is invoked if a node is disconnected; this can be <code>null</code> */ public PreBuiltTransportClient( Settings settings, Collection<Class<? extends Plugin>> plugins, HostFailureListener hostFailureListener) { super(settings, Settings.EMPTY, addPlugins(plugins, PRE_INSTALLED_PLUGINS), hostFailureListener); }
@Override public TransportClient createClient(Map<String, String> clientConfig) { Settings settings = Settings.builder().put(clientConfig) .put(NetworkModule.HTTP_TYPE_KEY, Netty3Plugin.NETTY_HTTP_TRANSPORT_NAME) .put(NetworkModule.TRANSPORT_TYPE_KEY, Netty3Plugin.NETTY_TRANSPORT_NAME) .build(); TransportClient transportClient = new PreBuiltTransportClient(settings); for (TransportAddress transport : ElasticsearchUtils.convertInetSocketAddresses(transportAddresses)) { transportClient.addTransportAddress(transport); } // verify that we actually are connected to a cluster if (transportClient.connectedNodes().isEmpty()) { // close the transportClient here IOUtils.closeQuietly(transportClient); throw new RuntimeException("Elasticsearch client is not connected to any Elasticsearch nodes!"); } if (LOG.isInfoEnabled()) { LOG.info("Created Elasticsearch TransportClient with connected nodes {}", transportClient.connectedNodes()); } return transportClient; }
@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)); }
/** * A method to retrieve a PreBuiltTransportClient with the SearchGuardPlugin. * * @param settings Elasticsearch settings * * @return a PreBuiltTransportClient */ public PreBuiltTransportClient getPreBuiltTransportClientWithSearchGuardPlugin(Settings settings) { return new PreBuiltTransportClient(settings, SearchGuardSSLPlugin.class); } }
/** * 获取连接 * @throws Exception */ @Before public void getClient() throws Exception{ client = new PreBuiltTransportClient(Settings.EMPTY) .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(EsTest.host),EsTest.port)); }
@Override public Client get() { Settings settings = Settings.builder() .put("client.transport.ignore_cluster_name", true) .put("client.transport.sniff", true) .build(); TransportClient tc = new PreBuiltTransportClient(settings); List<URI> clusterAddresses = configuration.getURIs(); if (clusterAddresses.isEmpty()) { logger.warn(ElasticSearchConfiguration.ELASTIC_SEARCH_URL_PROPERTY_NAME + " is not set. Indexing will remain DISABLED."); } for (URI hostAddress : clusterAddresses) { int port = Optional.ofNullable(hostAddress.getPort()).orElse(9200); try { tc.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostAddress.getHost()), port)); } catch (UnknownHostException uhe){ throw new ProvisionException("Invalid host" + hostAddress.getHost(), uhe); } } return tc; } }
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(); }