Java Code Examples for org.elasticsearch.common.transport.InetSocketTransportAddress

The following are top voted examples for showing how to use org.elasticsearch.common.transport.InetSocketTransportAddress. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: es-crud   File: ElasticSearchClient.java   Source Code and License 9 votes vote down vote up
private TransportClient getElasticClient() {
        try {
            // un-command this, if you have multiple node
//            TransportClient client1 = new PreBuiltTransportClient(Settings.EMPTY)
//                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host1"), 9300))
//                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host1"), 9300));

            Settings setting = Settings.builder()
                    .put("cluster.name", elasticPro.getProperty("cluster"))
                    .put("client.transport.sniff", Boolean.valueOf(elasticPro.getProperty("transport.sniff"))).build();

            client = new PreBuiltTransportClient(setting)
                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(elasticPro.getProperty("host")), Integer.valueOf(elasticPro.getProperty("port"))));
        } catch (UnknownHostException ex) {
            log.error("Exception occurred while getting Client : " + ex, ex);
        }
        return client;
    }
 
Example 2
Project: Stargraph   File: ElasticClient.java   Source Code and License 9 votes vote down vote up
private TransportClient createClient() {
    Config cfg = getTypeCfg();
    Settings settings = Settings.builder().put("cluster.name", cfg.getString("elastic.cluster-name")).build();
    TransportClient client = new PreBuiltTransportClient(settings);

    List<String> servers = cfg.getStringList("elastic.servers");
    logger.debug(marker, "Elastic Servers: {}", servers);
    for (String addr : servers) {
        try {
            String[] a = addr.split(":");
            String host = a[0];
            int port = Integer.parseInt(a[1]);
            client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), port));
        } catch (Exception e) {
            logger.error(marker, "Transport client creation failed for '{}'", addr, e);
        }
    }

    return client;
}
 
Example 3
Project: hsweb-learning   File: EsClient.java   Source Code and License 9 votes vote down vote up
public static TransportClient getClient() {
    try {
        if (tclient == null) {
            String EsHosts = "192.168.1.41:9300,192.168.1.42:9300,192.168.1.43:9300";
            Settings settings = Settings.settingsBuilder()
                    .put("cluster.name", "dkes")//设置集群名称
                    .put("tclient.transport.sniff", true).build();//自动嗅探整个集群的状态,把集群中其它机器的ip地址加到客户端中

            tclient = TransportClient.builder().settings(settings).build();
            String[] nodes = EsHosts.split(",");
            for (String node : nodes) {
                if (node.length() > 0) {//跳过为空的node(当开头、结尾有逗号或多个连续逗号时会出现空node)
                    String[] hostPort = node.split(":");
                    tclient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostPort[0]), Integer.parseInt(hostPort[1])));

                }
            }
        }//if
    } catch (Exception e) {
        e.printStackTrace();
    }
    return tclient;
}
 
Example 4
Project: Transwarp-Sample-Code   File: SearchES.java   Source Code and License 8 votes vote down vote up
/**
 * wildcard查询/or条件/and条件
 */
public static void wildcardQuery() {
    try {
        Settings settings = Settings.settingsBuilder().put("cluster.name", "elasticsearch1").build();
        TransportClient transportClient = TransportClient.builder().
                settings(settings).build().addTransportAddress(
                new InetSocketTransportAddress(InetAddress.getByName("172.16.2.94"), 9300));
        SearchRequestBuilder searchRequestBuilder = transportClient.prepareSearch("sqd.es_start");

        // {"query": {"bool": {"must": [{"or": [{"wildcard": {"content": "*oracle*"}},{"wildcard": {"content": "*mysql*"}}]}],"must_not": [],"should": []}},"from": 0, "size": 10, "sort": [],"aggs": {}}
        SearchResponse searchResponse = searchRequestBuilder.
                setQuery(QueryBuilders.boolQuery()
                .must(QueryBuilders.orQuery(QueryBuilders.wildcardQuery("content","*mysql*"),
                        QueryBuilders.wildcardQuery("content","*oracle*")))
                .must(QueryBuilders.termQuery("tbool","false")))
                .setFrom(0).setSize(100).setExplain(true).execute().actionGet();
        SearchHits searchHits = searchResponse.getHits();
        System.out.println();
        System.out.println("Total Hits is " + searchHits.totalHits());
        System.out.println();
        for (int i = 0; i < searchHits.getHits().length; ++i) {
            System.out.println("content is "
                    + searchHits.getHits()[i].getSource().get("content"));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 5
Project: Transwarp-Sample-Code   File: SearchES.java   Source Code and License 8 votes vote down vote up
/**
 * 多字段查询
 */
public static void multisearch() {
    try {
        Settings settings = Settings.settingsBuilder().put("cluster.name", "elasticsearch1").build();
        TransportClient transportClient = TransportClient.builder().
                settings(settings).build().addTransportAddress(
                new InetSocketTransportAddress(InetAddress.getByName("172.16.2.93"), 9300));
        SearchRequestBuilder searchRequestBuilder = transportClient.prepareSearch("service2","clients");
        SearchResponse searchResponse = searchRequestBuilder.
                setQuery(QueryBuilders.boolQuery()
                        .should(QueryBuilders.termQuery("id","5"))
                        .should(QueryBuilders.prefixQuery("content","oracle")))
                .setFrom(0).setSize(100).setExplain(true).execute().actionGet();
        SearchHits searchHits = searchResponse.getHits();
        System.out.println();
        System.out.println("Total Hits is " + searchHits.totalHits());
        System.out.println();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 6
Project: asura   File: EshClientFactory.java   Source Code and License 8 votes vote down vote up
public TransportClient getClient()   {
    if (esClient == null) {
        synchronized (this) {
            if (esClient == null) {
                try {
                    //判断配置
                    Preconditions.checkNotNull(clusterName, "es 服务clusterName未配置");
                    Preconditions.checkNotNull(addresses, "es 服务ip未配置");
                    //Preconditions.checkArgument(esPort > 0, "es 服务服务port未配置");
                    //设置集群的名字
                    Settings settings = Settings.settingsBuilder().put("client.node", true).put("cluster.name", clusterName).put("client.transport.sniff", sniff).build();
                    //Settings settings = Settings.settingsBuilder().put("client.transport.sniff", sniff).build();
                    //创建集群client并添加集群节点地址
                    esClient = TransportClient.builder().settings(settings).build();
                    for (String address : addresses) {
                        String[] inetAddress = address.split(":");
                        esClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(inetAddress[0]), new Integer(inetAddress[1])));
                    }
                  }catch (Exception e){
                     LOGGER.error("客户端连接初始化异常",e);
                }
            }
        }
    }
    return esClient;
}
 
Example 7
Project: yacy_grid_mcp   File: ElasticsearchClient.java   Source Code and License 7 votes vote down vote up
/**
 * create a elasticsearch transport client (remote elasticsearch)
 * @param addresses an array of host:port addresses
 * @param clusterName
 */
public ElasticsearchClient(final String[] addresses, final String clusterName) {
    // create default settings and add cluster name
    Settings.Builder settings = Settings.builder()
            .put("cluster.routing.allocation.enable", "all")
            .put("cluster.routing.allocation.allow_rebalance", "always");
    if (clusterName != null) settings.put("cluster.name", clusterName);
    
    // create a client
    TransportClient tc = new PreBuiltTransportClient(settings.build());

    for (String address: addresses) {
        String a = address.trim();
        int p = a.indexOf(':');
        if (p >= 0) try {
            InetAddress i = InetAddress.getByName(a.substring(0, p));
            int port = Integer.parseInt(a.substring(p + 1));
            tc.addTransportAddress(new InetSocketTransportAddress(i, port));
        } catch (UnknownHostException e) {
            Data.logger.warn("", e);
        }
    }
    this.elasticsearchClient = tc;
}
 
Example 8
Project: grpc-mate   File: TransportClientProvider.java   Source Code and License 7 votes vote down vote up
/**
 * TransportClient provider.
 * @return TransportClient
 */
public TransportClient get() {
  final String hostCsv = configuration.getString(CONFIG_ES_CLUSTER_HOST);
  final List<String> hosts = Splitter.on(",").splitToList(hostCsv);
  Preconditions.checkState(!hosts.isEmpty());

  final TransportClient transportClient = new PreBuiltTransportClient(esSettings());
  final Integer esTransportPort = configuration.getInteger(CONFIG_ES_CLUSTER_PORT, 9300);
  log.info("connect to elastic search {} on port {} ", hostCsv, esTransportPort);

  hosts.forEach(
      host -> transportClient.addTransportAddress(
          new InetSocketTransportAddress(new InetSocketAddress(host, esTransportPort))
      )
  );

  return transportClient;
}
 
Example 9
Project: uDetective   File: ElasticSearchDataSource.java   Source Code and License 7 votes vote down vote up
@Override
public TransportClient connect() {
    Settings settings = Settings.builder()
        .put("cluster.name", "elasticsearch")
        .put("client.transport.sniff", true).build();       
    
    try {
        client = new PreBuiltTransportClient(settings)
            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("event-apptst01.as.it.ubc.ca"), 9300))
            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("event-apptst02.as.it.ubc.ca"), 9300));        
    } catch (UnknownHostException uhe) {
        logger.error(uhe.toString());
    }
    
    return client;
}
 
Example 10
Project: Transwarp-Sample-Code   File: SearchES.java   Source Code and License 7 votes vote down vote up
/**
 * json查询
 */
public static void jsonquery() {
    try {
        Settings settings = Settings.settingsBuilder().put("cluster.name", "elasticsearch1").build();
        TransportClient transportClient = TransportClient.builder().
                settings(settings).build().addTransportAddress(
                new InetSocketTransportAddress(InetAddress.getByName("172.16.2.93"), 9300));
        SearchRequestBuilder searchRequestBuilder = transportClient.prepareSearch("service2");
        SearchResponse searchResponse = searchRequestBuilder.setSource("{\n" +
                "\"query\": {\n" +
                "\"bool\": {\n" +
                "\"must\": [\n" +
                "{\n" +
                "\"prefix\": {\n" +
                "\"content\": \"oracle\"\n" +
                "}\n" +
                "}\n" +
                "],\n" +
                "\"must_not\": [ ],\n" +
                "\"should\": [ ]\n" +
                "}\n" +
                "},\n" +
                "\"from\": 0,\n" +
                "\"size\": 10,\n" +
                "\"sort\": [ ],\n" +
                "\"aggs\": { }\n" +
                "}")
                .get();
        SearchHits searchHits = searchResponse.getHits();
        System.out.println();
        System.out.println("Total Hits is " + searchHits.totalHits());
        System.out.println();
        for (int i = 0; i < searchHits.getHits().length; ++i) {
            System.out.println("content is "
                    + searchHits.getHits()[i].getSource().get("content"));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 11
Project: elasticsearch   File: App.java   Source Code and License 7 votes vote down vote up
public static void main(String[] args) throws IOException {
    Log4jESLoggerFactory.getRootLogger().setLevel("ERROR");
    Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "ciphergateway").build();
    //Use one and only one Client in your JVM. It's threadsafe.
    Client client = new TransportClient(settings)
        .addTransportAddress(new InetSocketTransportAddress("localhost", 9300));

    Long start = System.currentTimeMillis();
//    exportES(client);
//    deleteES(client);
//    importES(client);
//    searchES(client);
    exportAndDeleteES(client);
    client.close();
    Long end = System.currentTimeMillis();
    System.out.println("用时: " + (end - start) + " ms");
  }
 
Example 12
Project: es-sql   File: ElasticSearchConnection.java   Source Code and License 7 votes vote down vote up
public ElasticSearchConnection(String jdbcUrl) {


        Settings settings = Settings.builder().put("client.transport.ignore_cluster_name", true).build();
        try {
            TransportClient transportClient = new PreBuiltTransportClient(settings);

            String hostAndPortArrayStr = jdbcUrl.split("/")[2];
            String[] hostAndPortArray = hostAndPortArrayStr.split(",");

            for (String hostAndPort : hostAndPortArray) {
                String host = hostAndPort.split(":")[0];
                String port = hostAndPort.split(":")[1];
                transportClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), Integer.parseInt(port)));
            }
            client = transportClient;
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
 
Example 13
Project: mapr-music   File: MaprElasticSearchServiceBuilder.java   Source Code and License 6 votes vote down vote up
/**
 * Builds the {@link MaprElasticSearchService} according to the specified properties.
 *
 * @return instence of {@link MaprElasticSearchService}, which can be started via {@link MaprElasticSearchService#start()}.
 * @throws IllegalStateException in case when some of the required properties are missed.
 */
public MaprElasticSearchService build() {

    ensureFieldNonNull("port", this.port);
    ensureFieldNonNull("hostname", this.inetAddress);
    ensureFieldNonNull("indexName", this.indexName);
    ensureFieldNonNull("typeName", this.typeName);
    ensureFieldNonNull("changelog", this.changelog);
    ensureFieldNonNull("fields", this.fields);

    return () -> {

        // Create ES Client
        TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
                .addTransportAddress(new InetSocketTransportAddress(inetAddress, port));

        // Create CDC Listener
        ChangelogListener listener = ChangelogListenerImpl.forChangelog(changelog);

        // Set 'onInsert' callback
        listener.onInsert(new SaveIndexCDCCallback(client));

        // Set 'onUpdate' callback
        listener.onUpdate(new SaveIndexCDCCallback(client));

        // Define and set 'onDelete' callback
        listener.onDelete((id) -> client.prepareDelete(indexName, typeName, id).get());

        listener.listen();

    };
}
 
Example 14
Project: memory-graph   File: Elasticsearch5SearchIndex.java   Source Code and License 6 votes vote down vote up
private static TransportClient createTransportClient(ElasticsearchSearchIndexConfiguration config) {
    Settings settings = tryReadSettingsFromFile(config);
    if (settings == null) {
        Settings.Builder settingsBuilder = Settings.builder();
        if (config.getClusterName() != null) {
            settingsBuilder.put("cluster.name", config.getClusterName());
        }
        for (Map.Entry<String, String> esSetting : config.getEsSettings().entrySet()) {
            settingsBuilder.put(esSetting.getKey(), esSetting.getValue());
        }
        settings = settingsBuilder.build();
    }
    TransportClient transportClient = new PreBuiltTransportClient(settings);
    for (String esLocation : config.getEsLocations()) {
        String[] locationSocket = esLocation.split(":");
        String hostname;
        int port;
        if (locationSocket.length == 2) {
            hostname = locationSocket[0];
            port = Integer.parseInt(locationSocket[1]);
        } else if (locationSocket.length == 1) {
            hostname = locationSocket[0];
            port = config.getPort();
        } else {
            throw new MemgraphException("Invalid elastic search location: " + esLocation);
        }
        InetAddress host;
        try {
            host = InetAddress.getByName(hostname);
        } catch (UnknownHostException ex) {
            throw new MemgraphException("Could not resolve host: " + hostname, ex);
        }
        transportClient.addTransportAddress(new InetSocketTransportAddress(host, port));
    }
    return transportClient;
}
 
Example 15
Project: springboot_op   File: ElasticsearchConfiguration.java   Source Code and License 6 votes vote down vote up
@Bean
public TransportClient initESClient() throws NumberFormatException, UnknownHostException{
	String ip = env.getProperty("spring.es.ip");
	String port = env.getProperty("spring.es.port");
	String clusterName = env.getProperty("spring.es.cluster_name");
	
	Builder builder = Settings.builder().put("cluster.name", clusterName).put("client.transport.sniff", true);
	Settings esSettings = builder.build();
	TransportClient client = new PreBuiltTransportClient(esSettings);
	client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ip), Integer.parseInt(port)));
	logger.info("ES Client 初始化成功, ip : {}, port : {}, cluster_name : {}", ip, port, clusterName);
	return client;
}
 
Example 16
Project: connect-sample-apps   File: ElasticsearchClientConfiguration.java   Source Code and License 6 votes vote down vote up
@Bean
TransportClient transportClient() throws UnknownHostException {
    return new PreBuiltTransportClient(
            Settings.builder()
            .put(ClusterName.CLUSTER_NAME_SETTING.getKey(), "slacklistener")
            .build()
        )
        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(System.getProperty("elasticsearch.host")), 9300));
}
 
Example 17
Project: canal-elasticsearch   File: ElasticsearchServiceImpl.java   Source Code and License 6 votes vote down vote up
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");
    }
 
Example 18
Project: flume-release-1.7.0   File: ElasticSearchTransportClient.java   Source Code and License 6 votes vote down vote up
/**
 * Open client to elaticsearch cluster
 * 
 * @param clusterName
 */
private void openClient(String clusterName) {
  logger.info("Using ElasticSearch hostnames: {} ",
      Arrays.toString(serverAddresses));
  Settings settings = Settings.builder().put("cluster.name", clusterName).build();;

  TransportClient transportClient = TransportClient.builder().settings(settings).build();
  for (InetSocketTransportAddress host : serverAddresses) {
    transportClient.addTransportAddress(host);
  }
  if (client != null) {
    client.close();
  }
  client = transportClient;
}
 
Example 19
Project: Gather-Platform   File: ESClient.java   Source Code and License 6 votes vote down vote up
public Client getClient() {
    if (!staticValue.isNeedEs()) {
        LOG.info("已在配置文件中声明不需要ES,如需要ES,请在配置文件中进行配置");
        return null;
    }
    if (client != null) return client;
    try {
        LOG.info("正在初始化ElasticSearch客户端," + staticValue.getEsHost());
        Settings settings = Settings.builder()
                .put("cluster.name", staticValue.getEsClusterName()).build();
        client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(staticValue.getEsHost()), 9300));
        final ClusterHealthResponse healthResponse = client.admin().cluster().prepareHealth()
                .setTimeout(TimeValue.timeValueMinutes(1)).execute().actionGet();
        if (healthResponse.isTimedOut()) {
            LOG.error("ES客户端初始化失败");
        } else {
            LOG.info("ES客户端初始化成功");
        }
    } catch (IOException e) {
        LOG.fatal("构建ElasticSearch客户端失败!");
    }
    return client;
}
 
Example 20
Project: soundwave   File: EsStore.java   Source Code and License 6 votes vote down vote up
public EsStore(String host, int port) {
  String clusterName = Configuration.getProperties().getString("es_cluster_name", "soundwave");
  Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", clusterName)
      .put("client.transport.sniff", false).build();
  esClient = new TransportClient(settings)
      .addTransportAddress(
          new InetSocketTransportAddress(host, port));
}
 
Example 21
Project: Transwarp-Sample-Code   File: SearchES.java   Source Code and License 6 votes vote down vote up
/**
 * 通过json创建index
 */
public static void createByJson() {
    try {
        Settings settings = Settings.settingsBuilder().put("cluster.name", "elasticsearch1").build();
        TransportClient client = TransportClient.builder().
                settings(settings).build().addTransportAddress(
                new InetSocketTransportAddress(InetAddress.getByName("172.16.2.93"), 9300));
        XContentBuilder mapping = jsonBuilder()
                .startObject()
                .startObject("general")
                .startObject("properties")
                .startObject("message")
                .field("type", "string")
                .field("index", "not_analyzed")
                .endObject()
                .startObject("source")
                .field("type","string")
                .endObject()
                .endObject()
                .endObject()
                .endObject();
        client.admin().indices().prepareCreate("t2")
                .setSettings(Settings.builder()
                        .put("index.number_of_shards", 3)
                        .put("index.number_of_replicas", 2)
                )
                .get();
        client.admin().indices().preparePutMapping("t2")
                .setType("general")
                .setSource(mapping)
                .execute().actionGet();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 22
Project: Elasticsearch-Tutorial-zh-CN   File: ElasticsearchGis.java   Source Code and License 6 votes vote down vote up
@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();
}
 
Example 23
Project: Elasticsearch-Tutorial-zh-CN   File: BaseDemo.java   Source Code and License 6 votes vote down vote up
@SuppressWarnings({"unchecked", "resource"})
public static void main(String[] args) throws IOException {
	// 先构建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));

	//======================================================

	create(transportClient);
	batchCreate(transportClient);
	batchUpdate(transportClient);
	batchDelete(transportClient);
	update(transportClient);
	query(transportClient);
	queryByMatchOneParam(transportClient);
	queryByMatchMoreParam(transportClient);
	queryByTerm(transportClient);
	queryByPrefix(transportClient);
	queryByBool(transportClient);
	queryMore(transportClient);
	queryByMultiGet(transportClient);
	queryByScroll(transportClient);
	queryByTemplate(transportClient);
	delete(transportClient);
	aggregate(transportClient);

	//======================================================

	transportClient.close();
}
 
Example 24
Project: nest-spider   File: ESClient.java   Source Code and License 6 votes vote down vote up
public Client getClient() {
	if(!sValue.isNeedES()) {
		LOG.info("配置文件已声明不需要ES, 如需要ES, 请更改配置文件");
		return null;
	}
	if(client!=null)
		return client;
	LOG.info("正在初始化客户端, Host:{},Port:{}", sValue.getEsHost(), sValue.getEsPort());
	Settings settings = Settings.builder()
	        .put("cluster.name", sValue.getEsClusterName()).build();
	try {
		client = new PreBuiltTransportClient(settings)
		        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(sValue.getEsHost()), sValue.getEsPort()));
	    final ClusterHealthResponse healthResponse = client.admin().cluster()
	    		.prepareHealth().setTimeout(TimeValue.timeValueMinutes(1)).get();
	    if(healthResponse.isTimedOut()) 
	    	LOG.info("ES客户端初始化失败,响应超时");
	    else
	    	LOG.info("ES客户端初始化成功");
	} catch(IOException e) {
		LOG.fatal("ES客户端构建失败,由于 " + e.getLocalizedMessage());
	}
	return client;
}
 
Example 25
Project: servicebuilder   File: ElasticsearchAddonImpl.java   Source Code and License 6 votes vote down vote up
@Override
public ElasticsearchAddonImpl initialize(ServiceConfig serviceConfig) {

    Settings settings = Settings.builder()
            .put("cluster.name", clustername)
            .put("node.name", clientname)
            .build();

    InetAddress address = null;
    try {
        address = InetAddress.getByName(coordinatorUrl);
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }

    return this.withClient(new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(address, coordinatorPort)));
}
 
Example 26
Project: es-sql   File: MainTestSuite.java   Source Code and License 6 votes vote down vote up
protected static InetSocketTransportAddress getTransportAddress() throws UnknownHostException {
	String host = System.getenv("ES_TEST_HOST");
	String port = System.getenv("ES_TEST_PORT");

	if(host == null) {
		host = "localhost";
		System.out.println("ES_TEST_HOST enviroment variable does not exist. choose default 'localhost'");
	}

	if(port == null) {
		port = "9300";
		System.out.println("ES_TEST_PORT enviroment variable does not exist. choose default '9300'");
	}

	System.out.println(String.format("Connection details: host: %s. port:%s.", host, port));
	return new InetSocketTransportAddress(InetAddress.getByName(host), Integer.parseInt(port));
}
 
Example 27
Project: uavstack   File: ESClient.java   Source Code and License 6 votes vote down vote up
/**
 * init
 * 
 * @param esAddrs
 * @param clusterName
 */
private void init(String[] esAddrs, String clusterName) {

    Settings settings = Settings.EMPTY;

    if (!StringHelper.isEmpty(clusterName)) {
        settings = Settings.builder().put("cluster.name", clusterName).build();
    }

    client = new PreBuiltTransportClient(settings);

    for (String esAddr : esAddrs) {
        String[] ipport = esAddr.split(":");
        client.addTransportAddress(new InetSocketTransportAddress(
                new InetSocketAddress(ipport[0], DataConvertHelper.toInt(ipport[1], 9300))));
    }
}
 
Example 28
Project: elasticsearch-full   File: XPackBaseDemo.java   Source Code and License 6 votes vote down vote up
@Before
public void setUp() throws Exception {
    /**
     * 如果es集群安装了x-pack插件则以此种方式连接集群
     * 1. java客户端的方式是以tcp协议在9300端口上进行通信
     * 2. http客户端的方式是以http协议在9200端口上进行通信
     */
    Settings settings = Settings.builder(). put("xpack.security.user", "elastic:changeme").build();
    client = new PreBuiltXPackTransportClient(settings)
            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
    elasticsearchTemplate = new ElasticsearchTemplate(client);
    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials("elastic", "changeme"));
    restClient = RestClient.builder(new HttpHost("localhost",9200,"http"))
            .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                @Override
                public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                    return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                }
            }).build();
}
 
Example 29
Project: dremio-oss   File: ElasticsearchCluster.java   Source Code and License 6 votes vote down vote up
private void initClient() throws IOException {

    final String hosts;
    if(remote && nodes != null && nodes.length > 0 && nodes[0] != null){
      NodesInfoResponse response =  nodes[0].client().admin().cluster().prepareNodesInfo().execute().actionGet();
      TransportInfo info = response.getNodes()[0].getTransport();
      InetSocketTransportAddress inet = (InetSocketTransportAddress) info.address().publishAddress();
      hosts = inet.address().getAddress().getHostAddress() + ":" + inet.address().getPort();
    } else {
      int port = sslEnabled ? sslPort : ELASTICSEARCH_PORT;
      hosts = "127.0.0.1:" + port;
    }

    this.pool = new ElasticConnectionPool(hosts, sslEnabled, null, null, 10000, false);
    pool.connect();
    connection = pool.getRandomConnection();
    webTarget = connection.getTarget();
  }
 
Example 30
Project: jaf-examples   File: AggsQueryTest.java   Source Code and License 6 votes vote down vote up
public static void main(String[] args) throws UnknownHostException {
	// TODO Auto-generated method stub

	
	Client client = TransportClient.builder().build()
	        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("169.254.135.217"), 9300));
	//avgQuery(client);
	//minQuery(client);
	//maxQuery(client);
	//valueCountQuery(client);
	//extendedStatsQuery(client);
	//percentileQuery(client);
	//percentileRankQuery(client);
	//rangeQuery(client);
	//histogramQuery(client);
	//dateHistogramQuery(client);
	client.close();
}
 
Example 31
Project: jaf-examples   File: QueryTest.java   Source Code and License 6 votes vote down vote up
public static void main(String[] args) throws UnknownHostException {
		// TODO Auto-generated method stub
//		Client client = TransportClient.builder().build()
//		        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.1.110"), 9300));
		
		Client client = TransportClient.builder().build()
		        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("169.254.135.217"), 9300));
		//matchAllQuery(client);
		/* full text queries */
		//matchQuery(client);
		//multiMatchQuery(client);
		//commonTermQuery(client);
		/* end of full text queries */
		/* term query */
		//rangeQuery(client);
		//termQuery(client);
		othersQuery(client);
		/* end of term query*/
		client.close();
	}
 
Example 32
Project: conductor   File: ElasticsearchModule.java   Source Code and License 6 votes vote down vote up
@Provides
@Singleton
public Client getClient(Configuration config) throws Exception {

	String clusterAddress = config.getProperty("workflow.elasticsearch.url", "");
	if(clusterAddress.equals("")) {
		log.warn("workflow.elasticsearch.url is not set.  Indexing will remain DISABLED.");
	}

       Settings settings = Settings.builder().put("client.transport.ignore_cluster_name",true).put("client.transport.sniff", true).build();

       TransportClient tc = new PreBuiltTransportClient(settings);
       String[] hosts = clusterAddress.split(",");
       for (String host : hosts) {
           String[] hostparts = host.split(":");
           String hostname = hostparts[0];
           int hostport = 9200;
           if (hostparts.length == 2) hostport = Integer.parseInt(hostparts[1]);
           tc.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostname), hostport));
       }
       return tc;
   
   }
 
Example 33
Project: conductor   File: ElasticsearchModule.java   Source Code and License 6 votes vote down vote up
@Provides
@Singleton
public Client getClient(Configuration config) throws Exception {

	String clusterAddress = config.getProperty("workflow.elasticsearch.url", "");
	if(clusterAddress.equals("")) {
		log.warn("workflow.elasticsearch.url is not set.  Indexing will remain DISABLED.");
	}
	
   	Settings.Builder settings = Settings.settingsBuilder();
       settings.put("client.transport.ignore_cluster_name", true);
       settings.put("client.transport.sniff", true);
       
       TransportClient tc = TransportClient.builder().settings(settings).build();
       String[] hosts = clusterAddress.split(",");
       for (String host : hosts) {
           String[] hostparts = host.split(":");
           String hostname = hostparts[0];
           int hostport = 9200;
           if (hostparts.length == 2) hostport = Integer.parseInt(hostparts[1]);
           tc.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostname), hostport));
       }
       return tc;
   
   }
 
Example 34
Project: chili-core   File: ElasticMap.java   Source Code and License 6 votes vote down vote up
public ElasticMap(Future<AsyncStorage<Value>> future, StorageContext<Value> context) throws IOException {
    this.context = context;
    this.logger = context.logger(getClass());
    try {
        this.client = new PreBuiltTransportClient(Settings.builder()
                .put("client.transport.sniff", true)
                .build())
                .addTransportAddress(
                        new InetSocketTransportAddress(InetAddress.getByName(context.host()), context.port()));

        client.admin().indices().create(new CreateIndexRequest(context.database())).get();
    } catch (UnknownHostException | InterruptedException | ExecutionException e) {
        logger.onError(e);
    }
    future.complete(this);
}
 
Example 35
Project: flink-operator-library   File: ElasticsearchSink.java   Source Code and License 6 votes vote down vote up
/**
 * @see org.apache.flink.api.common.functions.AbstractRichFunction#open(org.apache.flink.configuration.Configuration)
 */
public void open(Configuration parameters) throws Exception {
	////////////////////////////////////////////////////////////////////////////////////////////////
	// prepare settings and initialize client
	final Settings settings = Settings.builder().put(ES_SETTING_CLUSTER_NAME, clusterName).build();		
	this.client = TransportClient.builder().settings(settings).build();
	for(final ElasticsearchNodeAddress ta : transportAddresses) {
		this.client.addTransportAddress(
				new InetSocketTransportAddress(
						new InetSocketAddress(ta.getHost(), ta.getPort())));
		LOG.info("elasticsearch connection [host="+ta.getHost()+", port="+ta.getPort()+"]");
	}
	////////////////////////////////////////////////////////////////////////////////////////////////
	
	////////////////////////////////////////////////////////////////////////////////////////////////
	// if provided initialize the statsd client
	if(StringUtils.isNotBlank(this.statsdHost) || this.statsdPort > 0)
		this.statsdClient = new NonBlockingStatsDClient(this.statsdPrefix, this.statsdHost, this.statsdPort);
	////////////////////////////////////////////////////////////////////////////////////////////////
}
 
Example 36
Project: ecs-dashboard   File: ElasticNamespaceDAO.java   Source Code and License 6 votes vote down vote up
public ElasticNamespaceDAO(ElasticDAOConfig config) {
	try {
		Builder builder = Settings.builder();
		// Check for new hosts within the cluster
		builder.put(CLIENT_SNIFFING_CONFIG, true);
		// specify cluster name
		if (config.getClusterName() != null) {
			builder.put(CLIENT_CLUSTER_NAME_CONFIG, config.getClusterName());
		}
		Settings settings = builder.build();
		// create client
		elasticClient = new PreBuiltTransportClient(settings);
		// add hosts
		for (String elasticHost : config.getHosts()) {
			elasticClient.addTransportAddress(
					new InetSocketTransportAddress(InetAddress.getByName(elasticHost), config.getPort()));
		}
	} catch (UnknownHostException e) {
		throw new RuntimeException("Unable to initialize Eleasticsearch client " + e.getLocalizedMessage());
	}
}
 
Example 37
Project: ecs-dashboard   File: ElasticVdcDAO.java   Source Code and License 6 votes vote down vote up
public ElasticVdcDAO(ElasticDAOConfig config) {
	try {
		Builder builder = Settings.builder();
		// Check for new hosts within the cluster
		builder.put(CLIENT_SNIFFING_CONFIG, true);
		// specify cluster name
		if (config.getClusterName() != null) {
			builder.put(CLIENT_CLUSTER_NAME_CONFIG, config.getClusterName());
		}
		Settings settings = builder.build();
		// create client
		elasticClient = new PreBuiltTransportClient(settings);
		// add hosts
		for (String elasticHost : config.getHosts()) {
			elasticClient.addTransportAddress(
					new InetSocketTransportAddress(InetAddress.getByName(elasticHost), config.getPort()));
		}
	} catch (UnknownHostException e) {
		throw new RuntimeException("Unable to initialize Eleasticsearch client " + e.getLocalizedMessage());
	}
}
 
Example 38
Project: bigDataRocks   File: AElasticsearchDao.java   Source Code and License 6 votes vote down vote up
public void init(Class<T> tClass) {
    try {
        objectMapper = new ObjectMapper();
        if (client == null) {
            Settings settings =
                    Settings.builder().put("cluster.name", clusterName).build();
            TransportClient transClient = TransportClient.builder().settings(settings).build();
            String[] nodeList = nodes.split(",");
            for (String node : nodeList) {
                transClient.addTransportAddress(
                        new InetSocketTransportAddress(new InetSocketAddress(node, Integer.parseInt(port))));
                LOGGER.info("Added Elasticsearch node : " + node + ":" + port);
            }
            client = transClient;
        }

    } catch (Exception e) {
        LOGGER.error("Error initializing ElasticsearchDao");
        throw e;
    }
}
 
Example 39
Project: spider   File: ESClient.java   Source Code and License 6 votes vote down vote up
public Client getClient() {
    if (!staticValue.isNeedEs()) {
        LOG.info("已在配置文件中声明不需要ES,如需要ES,请在配置文件中进行配置");
        return null;
    }
    if (client != null) return client;
    LOG.info("正在初始化ElasticSearch客户端," + staticValue.getEsHost());
    
    Settings settings = Settings.builder()
    		.put("cluster.name", staticValue.getEsClusterName()).build();
    try {
    	client = new PreBuiltTransportClient(settings)
    			.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(staticValue.getEsHost()), staticValue.getEsPort()));
        final ClusterHealthResponse healthResponse = client.admin().cluster().prepareHealth()
                .setTimeout(TimeValue.timeValueMinutes(1)).execute().actionGet();
        if (healthResponse.isTimedOut()) {
            LOG.error("ES客户端初始化失败");
        } else {
            LOG.info("ES客户端初始化成功");
        }
    } catch (IOException e) {
        LOG.fatal("构建ElasticSearch客户端失败!");
    }
    return client;
}
 
Example 40
Project: stellar-core-es-indexer   File: ElasticsearchUtils.java   Source Code and License 6 votes vote down vote up
public static Client getElasticCloudClient(SCEIApplicationConfig config) {
    final boolean enableSsl = true;
    Settings settings = Settings.settingsBuilder()
        .put("transport.ping_schedule", "5s")
        .put("cluster.name", config.getElasticsearchClusterId())
        .put("action.bulk.compress", false)
        .put("shield.transport.ssl", enableSsl)
        .put("request.headers.X-Found-Cluster", config.getElasticsearchClusterId())
        .put("shield.user", config.getElasticsearchUserPassword())
        .build();

    final String hostname = config.getElasticsearchClusterId() + "." + config.getElasticsearchRegion() + ".aws.found.io";
    final int port = 9343;

    Client client = null;
    try {
        client = TransportClient.builder()
            .addPlugin(ShieldPlugin.class)
            .settings(settings)
            .build()
            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostname), port));
    } catch (UnknownHostException e) {
        logger.error("Unknown host:" + hostname, e);
    }
    return client;
}
 
Example 41
Project: zipkin   File: NativeClient.java   Source Code and License 6 votes vote down vote up
@Override public InternalElasticsearchClient create(String allIndices) {
  Settings settings = Settings.builder()
      .put("cluster.name", cluster)
      .put("lazyClient.transport.sniff", true)
      .build();

  TransportClient client = TransportClient.builder()
      .settings(settings)
      .build();
  for (String host : hosts.get()) {
    HostAndPort hostAndPort = HostAndPort.fromString(host).withDefaultPort(9300);
    try {
      client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(
          hostAndPort.getHostText()), hostAndPort.getPort()));
    } catch (UnknownHostException e) {
      // Hosts may be down transiently, we should still try to connect. If all of them happen
      // to be down we will fail later when trying to use the client when checking the index
      // template.
      continue;
    }
  }
  return new NativeClient(client, flushOnWrites);
}
 
Example 42
Project: ElasticsearchClassLoader   File: Indexer101.java   Source Code and License 6 votes vote down vote up
public static void main(String[] args) throws Exception{	
	Settings settings = Settings.settingsBuilder().put("cluster.name", "atul-es").build();
	Client client = TransportClient.builder().settings(settings).build().addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(Configuration.IP), Configuration.PORT));
	System.out.println("Connected to elasticsearch");
			
	for(int i=0;i<NAMES.length;i++){
		String json = getData(NAMES[i],i);			
				
		//index Data
		IndexResponse response = client.prepareIndex(Configuration.INDEX, Configuration.TYPE)
		        .setSource(json)
		        .execute()
		        .actionGet();
	}
	
	// on shutdown
	client.close();
}
 
Example 43
Project: ElasticsearchClassLoader   File: ElasticSearchV1.java   Source Code and License 6 votes vote down vote up
public Object search(JSONObject query) {
	
	TransportClient transportClient = new TransportClient();
	transportClient = transportClient.addTransportAddress(new InetSocketTransportAddress(Configuration.IP_V1, Configuration.PORT));			
	Client client = (Client) transportClient;
	System.out.println("Connected to elasticsearch v1");
			
	//Get all Data
	SearchResponse response = client.prepareSearch(Configuration.INDEX).setQuery(QueryBuilders.matchAllQuery()).execute().actionGet();        
	
	//Query by term
	//SearchResponse response = client.prepareSearch(Configuration.INDEX).setQuery(QueryBuilders.termQuery("pos", "1")).execute().actionGet();  
	
	SearchHit[] results = response.getHits().getHits();
       System.out.println("Got " + results.length + " hits") ;
       for (SearchHit hit : results) {
           Map<String,Object> result = hit.getSource();   
           System.out.println(result);
       }

	// on shutdown
	client.close();
	transportClient.close();
	
	return null;
}
 
Example 44
Project: tc   File: TcTransportClientFactoryBean.java   Source Code and License 6 votes vote down vote up
@Override
public TransportClient getObject() throws Exception {
    checkNotNull(settings);
    checkArgument(MapUtils.isNotEmpty(inetAddresses));
    PreBuiltTransportClient client = new PreBuiltTransportClient(Settings.builder().put(settings).build());
    inetAddresses.forEach((key, value) -> {
        try {
            client.addTransportAddress(
                    new InetSocketTransportAddress(InetAddress.getByName(key), value));
            log.info("add socket transport address [{}:{}] to client", key, value);
        } catch (UnknownHostException e) {
            throw new IllegalStateException(e);
        }
    });
    this.client = client;
    log.info("successful create transport client instance ...");
    return client;
}
 
Example 45
Project: linkbinder   File: ElasticsearchConfiguration.java   Source Code and License 6 votes vote down vote up
public ElasticsearchConfiguration build() {
    validate();

    ElasticsearchConfiguration result = new ElasticsearchConfiguration();
    result.addresses = new HashSet<>();
    try {
        for (Entry<String, Integer> a : addresses.entrySet()) {
            result.getAddresses().add(new InetSocketTransportAddress(InetAddress.getByName(a
                    .getKey()), a.getValue()));
        }
    } catch (UnknownHostException e) {
        throw new ElasticsearchException("指定されたホストを解決できません", e);
    }
    result.index = index;

    return result;
}
 
Example 46
Project: c2mon   File: ElasticsearchClientImpl.java   Source Code and License 6 votes vote down vote up
/**
 * Creates a {@link Client} to communicate with the Elasticsearch cluster.
 *
 * @return the {@link Client} instance
 */
private Client createClient() {
  final Settings.Builder settingsBuilder = Settings.builder();

  settingsBuilder.put("node.name", properties.getNodeName())
      .put("cluster.name", properties.getClusterName())
      .put("http.enabled", properties.isHttpEnabled());

  TransportClient client = new PreBuiltTransportClient(settingsBuilder.build());
  try {
    client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(properties.getHost()), properties.getPort()));
  } catch (UnknownHostException e) {
    log.error("Error connecting to the Elasticsearch cluster at {}:{}", properties.getHost(), properties.getPort(), e);
    return null;
  }

  return client;
}
 
Example 47
Project: newsleak-frontend   File: ElasticsearchResource.java   Source Code and License 6 votes vote down vote up
@Override
public boolean initialize(ResourceSpecifier aSpecifier, Map<String, Object> aAdditionalParams)
		throws ResourceInitializationException {
	if (!super.initialize(aSpecifier, aAdditionalParams)) {
		return false;
	}
	this.logger = this.getLogger();
	Settings settings = Settings.builder().put("cluster.name", mClustername).build();
	try {
		//			client = new PreBuiltTransportClient(settings)
		//			        .addTransportAddress(new TransportAddress(InetAddress.getLocalHost(), mPort));
		client = TransportClient.builder().settings(settings).build()
				.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(mHost), mPort));
		if (createIndex) {
			createIndex();
		}
	} catch (Exception e) {
		e.printStackTrace();
		System.exit(0);
	}
	return true;
}
 
Example 48
Project: newsleak-frontend   File: Postgres2ElasticsearchIndexer.java   Source Code and License 6 votes vote down vote up
public static void main(String[] args) throws Exception {

		Postgres2ElasticsearchIndexer indexer = new Postgres2ElasticsearchIndexer();
		indexer.getConfiguration(args);

		indexer.initDb(indexer.dbName, indexer.dbUrl, indexer.dbUser, indexer.dbPass);

		TransportClient client;
		Settings settings = Settings.builder().put("cluster.name", indexer.esClustername).build();
		st.setFetchSize(BATCH_SIZE);
		try {
			client = TransportClient.builder().settings(settings).build()
					.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(indexer.esHost), Integer.parseInt(indexer.esPort)));
			// remove existing index
			client.admin().indices().delete(new DeleteIndexRequest(indexer.esIndex)).actionGet();
			// create index with all extracted data
			indexer.documenIndexer(client, indexer.esIndex, "document");
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(0);
		}

		conn.close();
	}
 
Example 49
Project: samantha   File: ElasticSearchService.java   Source Code and License 6 votes vote down vote up
protected void startUp() {
    Logger.info("Starting SearchService");
    {
        Logger.debug("ElasticSearch settings:");
        Logger.debug("* cluster.name={}", cfgClusterName);
        Logger.debug("* host={}", cfgHost);
        Logger.debug("* port={}", cfgPort);
    }

    final Settings settings = Settings.settingsBuilder()
            .put("cluster.name", cfgClusterName).build();
    try {
        client = TransportClient.builder().settings(settings).build().addTransportAddress(
                new InetSocketTransportAddress(InetAddress.getByName(cfgHost), cfgPort));
    } catch (UnknownHostException e) {
        throw new ConfigurationException(e);
    }
    Logger.info("Connected to an elasticsearch client");
}
 
Example 50
Project: flume-ng-audit-db   File: ElasticSearchTransportClient.java   Source Code and License 6 votes vote down vote up
/**
 * Open client to elaticsearch cluster
 *
 * @param clusterName
 */
private void openClient(String clusterName) {
    logger.info("Using ElasticSearch hostnames: {} ",
            Arrays.toString(serverAddresses));
    Settings settings = Settings.settingsBuilder()
            .put("cluster.name", clusterName).build();

    TransportClient transportClient = TransportClient.builder().settings(settings).build();
    for (InetSocketTransportAddress host : serverAddresses) {
        transportClient.addTransportAddress(host);
    }
    if (client != null) {
        client.close();
    }
    client = transportClient;
}
 
Example 51
Project: elasticsql   File: StatementExecutor.java   Source Code and License 6 votes vote down vote up
public void connect(String httpAddress, String transportAddress) {
	String cluster_name = getClusterName(httpAddress.split(","));
	
	Settings settings = ImmutableSettings.settingsBuilder()
			.put("client.transport.sniff", true)
			.put("cluster.name", cluster_name).build();
	
	TransportClient client = new TransportClient(settings);
	for (String host : transportAddress.split(",")) {
		if (StringUtils.isBlank(host)) {
			continue;
		}
		
		String ip = StringUtils.substringBefore(host, ":");
		String port = StringUtils.substringAfter(host, ":");
		client.addTransportAddress(new InetSocketTransportAddress(ip, Integer.valueOf(port)));
	}
	
	searchDao = new SearchDao(client);
}
 
Example 52
Project: PurchaseNear   File: ElasticSearchClient.java   Source Code and License 6 votes vote down vote up
/**
 * 
 * @Description 初始化客户端
 * @param clusterNodes 多个集群节点用逗号分隔:"ip:9300,ip:9300"
 * @param clusterName 如:elasticsearch
 */
public static void initClient(String clusterNodes, String clusterName) {
	if (client == null) {
		lock.lock();
		try {
			if (client == null) {
				Settings settings = ImmutableSettings.settingsBuilder()
						.put("client.transport.sniff", true)
						.put("client", true).put("data", false)
						.put("clusterName", clusterName).build();

				client = new TransportClient(settings);
				for (String clusterNode : clusterNodes.split(",")) {
					String ip = clusterNode.split(":")[0];
					String port = clusterNode.split(":")[1];
					client.addTransportAddress(new InetSocketTransportAddress(
							ip, Integer.parseInt(port)));
				}
			}
		} finally {
			lock.unlock();
		}
	}
}
 
Example 53
Project: ElasticsearchSink2   File: ElasticSearchTransportClient.java   Source Code and License 6 votes vote down vote up
/**
 * Open client to elaticsearch cluster
 *
 * @param clusterName
 */
private void openClient(String clusterName) {
    logger.info("Using ElasticSearch hostnames: {} ",
            Arrays.toString(serverAddresses));
    Settings settings = Settings.settingsBuilder()
            .put("cluster.name", clusterName).build();

    TransportClient transportClient = TransportClient.builder().settings(settings).build();
    for (InetSocketTransportAddress host : serverAddresses) {
        transportClient.addTransportAddress(host);
    }
    if (client != null) {
        client.close();
    }
    client = transportClient;
}
 
Example 54
Project: bts   File: ElasticTest.java   Source Code and License 6 votes vote down vote up
public static void main(String[] args)
{
	Node node = nodeBuilder().node();
	Client client = node.client();

	AdminClient ac = client.admin();

	client = new TransportClient().addTransportAddress(new InetSocketTransportAddress("localhost", 9300));
	ElasticTest t = new ElasticTest();
	t.initRiver(client);
	// t.initRiver(client);
	// t.getContributionsByCandName(client, "aaew_corpus_aaew_full", 0);
	t.search(client);
	// // on shutdown
	// // Configuration
	// ClientConfig clientConfig = new
	// ClientConfig.Builder("http://localhost:9200").multiThreaded(true).build();
	//
	// // Construct a new Jest client according to configuration via factory
	// JestClientFactory factory = new JestClientFactory();
	// factory.setClientConfig(clientConfig);
	// JestClient clientj = factory.getObject();
	// clientj.
	node.close();

}
 
Example 55
Project: utils-elasticsearch   File: JMElasticsearchClient.java   Source Code and License 6 votes vote down vote up
/**
 * Instantiates a new JM elasticsearch client.
 *
 * @param elasticsearchConnect
 *            the elasticsearch connect
 * @param settings
 *            the settings
 */
public JMElasticsearchClient(String elasticsearchConnect,
		Settings settings) {
	super(settings);
	try {
		for (String ipPort : elasticsearchConnect.split(",")) {
			String[] seperatedIpPort = ipPort.split(":");
			addTransportAddress(new InetSocketTransportAddress(
					InetAddress.getByName(seperatedIpPort[0]),
					Integer.parseInt(seperatedIpPort[1])));
		}
	} catch (Exception e) {
		JMExceptionManager.handleExceptionAndThrowRuntimeEx(log, e,
				"JMElasticsearchClient", elasticsearchConnect, settings);
	}
	JMLog.info(log, "initElasticsearchClient", elasticsearchConnect,
			settings);
	this.jmESBulk = new JMElasticsearchBulk(this);
	this.jmESIndex = new JMElasticsearchIndex(this);
	this.jmESSearchAndCount = new JMElasticsearchSearchAndCount(this);
	this.jmESDelete = new JMElasticsearchDelete(this);
}
 
Example 56
Project: log4j-s3-search   File: ElasticsearchPublishHelper.java   Source Code and License 6 votes vote down vote up
private TransportClient getTransportClient(ElasticsearchConfiguration config) {
    Settings settings = Settings.builder()
        .put("cluster.name", config.getClusterName())
        .build();
    final PreBuiltTransportClient client = new PreBuiltTransportClient(settings);
    config.iterateHosts((host, port) -> {
        try {
            client.addTransportAddress(
                new InetSocketTransportAddress(InetAddress.getByName(host), port)
            );
        } catch (Exception e) {
            System.err.println(String.format(
                "Cannot add Elasticsearch host %s(%d): %s",
                host, port, e.getMessage()));
        }
    });
    return client;
}
 
Example 57
Project: test-data-generator   File: AbstractEsSink.java   Source Code and License 6 votes vote down vote up
@Override
public void init(Map<String, String> props) {
    String host = props.get(PropConst.HOST);
    if (host == null) {
        throw new IllegalArgumentException("Host does not specified");
    }
    String port = props.get(PropConst.PORT);
    if (port == null) {
        throw new IllegalArgumentException("Port does not specified");
    }
    String clusterName = props.get(PropConst.CLUSTER_NAME);
    if (clusterName == null) {
        throw new IllegalArgumentException("Cluster name does not specified");
    }
    client = (new TransportClient(ImmutableSettings.settingsBuilder().put("cluster.name", clusterName).build()))
            .addTransportAddress(new InetSocketTransportAddress(host, Integer.valueOf(port)));
}
 
Example 58
Project: zeppelin   File: TransportBasedClient.java   Source Code and License 6 votes vote down vote up
public TransportBasedClient(Properties props) throws UnknownHostException {
  final String host =
      props.getProperty(ElasticsearchInterpreter.ELASTICSEARCH_HOST);
  final int port = Integer.parseInt(
      props.getProperty(ElasticsearchInterpreter.ELASTICSEARCH_PORT));
  final String clusterName =
      props.getProperty(ElasticsearchInterpreter.ELASTICSEARCH_CLUSTER_NAME);

  final Settings settings = Settings.settingsBuilder()
      .put("cluster.name", clusterName)
      .put(props)
      .build();

  client = TransportClient.builder().settings(settings).build()
      .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), port));
}
 
Example 59
Project: alfresco-apache-storm-demo   File: ElasticSearchConnection.java   Source Code and License 6 votes vote down vote up
public static Client getClient(Map stormConf, String boltType) {
    String host = ConfUtils.getString(stormConf, "es." + boltType
            + ".hostname", "localhost");

    Client client;

    // connection to ES
    if (host.equalsIgnoreCase("localhost")) {
        Node node = org.elasticsearch.node.NodeBuilder.nodeBuilder()
                .clusterName("elasticsearch").client(true).node();
        client = node.client();
    } else {
        Settings settings = ImmutableSettings.settingsBuilder()
                .put("cluster.name", "elasticsearch").build();
        client = new TransportClient(settings)
                .addTransportAddress(new InetSocketTransportAddress(host,
                        9300));
    }
    return client;
}
 
Example 60
Project: elasticsearch-beyonder   File: BeyonderTransportIT.java   Source Code and License 6 votes vote down vote up
@BeforeClass
public static void startElasticsearch() throws IOException {
    // This is going to initialize our Rest Client if not initialized yet and will check
    // if security is activated on this cluster
    restClient();
    if (securityInstalled) {
        client = new PreBuiltXPackTransportClient(
                Settings.builder()
                        .put("client.transport.ignore_cluster_name", true)
                        .put("xpack.security.user", testClusterUser + ":" + testClusterPass)
                        .build()
        ).addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress(testClusterHost, testClusterTransportPort)));
    } else {
        client = new PreBuiltTransportClient(Settings.builder().put("client.transport.ignore_cluster_name", true).build())
                .addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress(testClusterHost, testClusterTransportPort)));
    }
}