Search in sources :

Example 11 with PreBuiltTransportClient

use of org.elasticsearch.transport.client.PreBuiltTransportClient in project cheetah by togethwy.

the class TestElasticsearch method test.

@Test
public void test() {
    try {
        Settings settings = Settings.builder().put("cluster.name", "elasticsearch").build();
        TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
        // IndexResponse response = client.prepareIndex("twitter", "tweet", "1")
        // .setSource(jsonBuilder()
        // .startObject()
        // .field("user", "kimchy")
        // .field("postDate", new Date())
        // .field("message", "trying out Elasticsearch")
        // .endObject()
        // )
        // .get();
        // System.out.println(response);
        Map<String, Object> result = new HashMap<>();
        result.put("user", "Mary6");
        result.put("postDate", new Date());
        result.put("massage", "this is Mary6");
        IndexResponse response2 = client.prepareIndex("twitter", "tweet").setSource(result).get();
        System.out.println(response2.status().name().equals("CREATED"));
        client.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) HashMap(java.util.HashMap) IndexResponse(org.elasticsearch.action.index.IndexResponse) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) Settings(org.elasticsearch.common.settings.Settings) Date(java.util.Date) Test(org.junit.Test)

Example 12 with PreBuiltTransportClient

use of org.elasticsearch.transport.client.PreBuiltTransportClient in project api-core by ca-cwds.

the class ElasticSearchLiveTestRunner method elasticsearchClient.

private static Client elasticsearchClient(ElasticsearchConfiguration config) throws Exception {
    TransportClient ret = new PreBuiltTransportClient(Settings.builder().put("cluster.name", config.getElasticsearchCluster()).build());
    ret.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(config.getElasticsearchHost()), Integer.parseInt(config.getElasticsearchPort())));
    return ret;
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress)

Example 13 with PreBuiltTransportClient

use of org.elasticsearch.transport.client.PreBuiltTransportClient in project api-core by ca-cwds.

the class ElasticUtils method makeESTransportClient.

private static TransportClient makeESTransportClient(final ElasticsearchConfiguration config) {
    TransportClient client;
    final String cluster = config.getElasticsearchCluster();
    final String user = config.getUser();
    final String password = config.getPassword();
    final boolean secureClient = StringUtils.isNotBlank(user) && StringUtils.isNotBlank(password);
    final Settings.Builder settings = Settings.builder().put("cluster.name", cluster);
    settings.put("client.transport.sniff", true);
    if (secureClient) {
        LOGGER.info("ENABLE X-PACK - cluster: {}", cluster);
        settings.put("xpack.security.user", user + ":" + password);
        client = new PreBuiltXPackTransportClient(settings.build());
    } else {
        LOGGER.info("DISABLE X-PACK - cluster: {}", cluster);
        client = new PreBuiltTransportClient(settings.build());
    }
    return client;
}
Also used : PreBuiltXPackTransportClient(org.elasticsearch.xpack.client.PreBuiltXPackTransportClient) PreBuiltXPackTransportClient(org.elasticsearch.xpack.client.PreBuiltXPackTransportClient) TransportClient(org.elasticsearch.client.transport.TransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) Settings(org.elasticsearch.common.settings.Settings)

Example 14 with PreBuiltTransportClient

use of org.elasticsearch.transport.client.PreBuiltTransportClient in project flink by apache.

the class ElasticsearchSinkITCase method getClient.

@Override
@SuppressWarnings("deprecation")
protected final Client getClient() {
    TransportAddress transportAddress = new TransportAddress(elasticsearchContainer.getTcpHost());
    String expectedClusterName = getClusterName();
    Settings settings = Settings.builder().put("cluster.name", expectedClusterName).build();
    return new PreBuiltTransportClient(settings).addTransportAddress(transportAddress);
}
Also used : PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) TransportAddress(org.elasticsearch.common.transport.TransportAddress) Settings(org.elasticsearch.common.settings.Settings)

Example 15 with PreBuiltTransportClient

use of org.elasticsearch.transport.client.PreBuiltTransportClient in project YCSB by brianfrankcooper.

the class ElasticsearchClient method init.

/**
 * Initialize any state for this DB. Called once per DB instance; there is one
 * DB instance per client thread.
 */
@Override
public void init() throws DBException {
    final Properties props = getProperties();
    this.indexKey = props.getProperty("es.index.key", DEFAULT_INDEX_KEY);
    final int numberOfShards = parseIntegerProperty(props, "es.number_of_shards", NUMBER_OF_SHARDS);
    final int numberOfReplicas = parseIntegerProperty(props, "es.number_of_replicas", NUMBER_OF_REPLICAS);
    final Boolean newIndex = Boolean.parseBoolean(props.getProperty("es.new_index", "false"));
    final Builder settings = Settings.builder().put("cluster.name", DEFAULT_CLUSTER_NAME);
    // add it to the settings file (will overwrite the defaults).
    for (final Entry<Object, Object> e : props.entrySet()) {
        if (e.getKey() instanceof String) {
            final String key = (String) e.getKey();
            if (key.startsWith("es.setting.")) {
                settings.put(key.substring("es.setting.".length()), e.getValue());
            }
        }
    }
    settings.put("client.transport.sniff", true).put("client.transport.ignore_cluster_name", false).put("client.transport.ping_timeout", "30s").put("client.transport.nodes_sampler_interval", "30s");
    // Default it to localhost:9300
    final String[] nodeList = props.getProperty("es.hosts.list", DEFAULT_REMOTE_HOST).split(",");
    client = new PreBuiltTransportClient(settings.build());
    for (String h : nodeList) {
        String[] nodes = h.split(":");
        final InetAddress address;
        try {
            address = InetAddress.getByName(nodes[0]);
        } catch (UnknownHostException e) {
            throw new IllegalArgumentException("unable to identity host [" + nodes[0] + "]", e);
        }
        final int port;
        try {
            port = Integer.parseInt(nodes[1]);
        } catch (final NumberFormatException e) {
            throw new IllegalArgumentException("unable to parse port [" + nodes[1] + "]", e);
        }
        client.addTransportAddress(new InetSocketTransportAddress(address, port));
    }
    final boolean exists = client.admin().indices().exists(Requests.indicesExistsRequest(indexKey)).actionGet().isExists();
    if (exists && newIndex) {
        client.admin().indices().prepareDelete(indexKey).get();
    }
    if (!exists || newIndex) {
        client.admin().indices().create(new CreateIndexRequest(indexKey).settings(Settings.builder().put("index.number_of_shards", numberOfShards).put("index.number_of_replicas", numberOfReplicas))).actionGet();
    }
    client.admin().cluster().health(new ClusterHealthRequest().waitForGreenStatus()).actionGet();
}
Also used : UnknownHostException(java.net.UnknownHostException) ClusterHealthRequest(org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentFactory.jsonBuilder(org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) Builder(org.elasticsearch.common.settings.Settings.Builder) Properties(java.util.Properties) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) InetAddress(java.net.InetAddress)

Aggregations

PreBuiltTransportClient (org.elasticsearch.transport.client.PreBuiltTransportClient)41 Settings (org.elasticsearch.common.settings.Settings)33 TransportClient (org.elasticsearch.client.transport.TransportClient)25 InetSocketTransportAddress (org.elasticsearch.common.transport.InetSocketTransportAddress)23 TransportAddress (org.elasticsearch.common.transport.TransportAddress)17 UnknownHostException (java.net.UnknownHostException)10 InetSocketAddress (java.net.InetSocketAddress)5 InetAddress (java.net.InetAddress)4 HashMap (java.util.HashMap)4 PostConstruct (javax.annotation.PostConstruct)4 SearchItem (cn.exrick.manager.dto.front.SearchItem)3 IOException (java.io.IOException)3 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)3 IndexResponse (org.elasticsearch.action.index.IndexResponse)3 Builder (org.elasticsearch.common.settings.Settings.Builder)3 XmallException (cn.exrick.common.exception.XmallException)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Logger (org.apache.log4j.Logger)2 ElasticsearchClusterRunner (org.codelibs.elasticsearch.runner.ElasticsearchClusterRunner)2