Search in sources :

Example 11 with TransportClient

use of org.elasticsearch.client.transport.TransportClient in project spring-boot by spring-projects.

the class ElasticsearchAutoConfiguration method createTransportClient.

private Client createTransportClient() throws Exception {
    TransportClientFactoryBean factory = new TransportClientFactoryBean();
    factory.setClusterNodes(this.properties.getClusterNodes());
    factory.setProperties(createProperties());
    factory.afterPropertiesSet();
    TransportClient client = factory.getObject();
    this.releasable = client;
    return client;
}
Also used : TransportClientFactoryBean(org.springframework.data.elasticsearch.client.TransportClientFactoryBean) TransportClient(org.elasticsearch.client.transport.TransportClient)

Example 12 with TransportClient

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

the class ElasticsearchSinkTestBase method runTransportClientTest.

/**
	 * Tests that the Elasticsearch sink works properly using a {@link TransportClient}.
	 */
public void runTransportClientTest() throws Exception {
    final String index = "transport-client-test-index";
    final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    DataStreamSource<Tuple2<Integer, String>> source = env.addSource(new SourceSinkDataTestKit.TestDataSourceFunction());
    Map<String, String> userConfig = new HashMap<>();
    // This instructs the sink to emit after every element, otherwise they would be buffered
    userConfig.put(ElasticsearchSinkBase.CONFIG_KEY_BULK_FLUSH_MAX_ACTIONS, "1");
    userConfig.put("cluster.name", CLUSTER_NAME);
    source.addSink(createElasticsearchSinkForEmbeddedNode(userConfig, new SourceSinkDataTestKit.TestElasticsearchSinkFunction(index)));
    env.execute("Elasticsearch TransportClient Test");
    // verify the results
    Client client = embeddedNodeEnv.getClient();
    SourceSinkDataTestKit.verifyProducedSinkData(client, index);
    client.close();
}
Also used : HashMap(java.util.HashMap) Tuple2(org.apache.flink.api.java.tuple.Tuple2) SourceSinkDataTestKit(org.apache.flink.streaming.connectors.elasticsearch.testutils.SourceSinkDataTestKit) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) Client(org.elasticsearch.client.Client) TransportClient(org.elasticsearch.client.transport.TransportClient)

Example 13 with TransportClient

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

the class Elasticsearch1ApiCallBridge method createClient.

@Override
public Client createClient(Map<String, String> clientConfig) {
    if (transportAddresses == null) {
        // Make sure that we disable http access to our embedded node
        Settings settings = settingsBuilder().put(clientConfig).put("http.enabled", false).build();
        node = nodeBuilder().settings(settings).client(true).data(false).node();
        Client client = node.client();
        if (LOG.isInfoEnabled()) {
            LOG.info("Created Elasticsearch client from embedded node");
        }
        return client;
    } else {
        Settings settings = settingsBuilder().put(clientConfig).build();
        TransportClient transportClient = new TransportClient(settings);
        for (TransportAddress transport : transportAddresses) {
            transportClient.addTransportAddress(transport);
        }
        // verify that we actually are connected to a cluster
        if (transportClient.connectedNodes().isEmpty()) {
            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;
    }
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) TransportAddress(org.elasticsearch.common.transport.TransportAddress) Client(org.elasticsearch.client.Client) TransportClient(org.elasticsearch.client.transport.TransportClient) Settings(org.elasticsearch.common.settings.Settings)

Example 14 with TransportClient

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

the class Elasticsearch5ApiCallBridge method createClient.

@Override
public Client 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()) {
        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;
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) TransportAddress(org.elasticsearch.common.transport.TransportAddress) Settings(org.elasticsearch.common.settings.Settings)

Example 15 with TransportClient

use of org.elasticsearch.client.transport.TransportClient 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();
    // Check if transport client needs to be used (To connect to multiple
    // elasticsearch nodes)
    remoteMode = Boolean.parseBoolean(props.getProperty("es.remote", "false"));
    final String pathHome = props.getProperty("path.home");
    // when running in embedded mode, require path.home
    if (!remoteMode && (pathHome == null || pathHome.isEmpty())) {
        throw new IllegalArgumentException("path.home must be specified when running in embedded mode");
    }
    this.indexKey = props.getProperty("es.index.key", DEFAULT_INDEX_KEY);
    int numberOfShards = parseIntegerProperty(props, "es.number_of_shards", NUMBER_OF_SHARDS);
    int numberOfReplicas = parseIntegerProperty(props, "es.number_of_replicas", NUMBER_OF_REPLICAS);
    Boolean newdb = Boolean.parseBoolean(props.getProperty("es.newdb", "false"));
    Builder settings = Settings.settingsBuilder().put("cluster.name", DEFAULT_CLUSTER_NAME).put("node.local", Boolean.toString(!remoteMode)).put("path.home", pathHome);
    // if properties file contains elasticsearch user defined properties
    // add it to the settings file (will overwrite the defaults).
    settings.put(props);
    final String clusterName = settings.get("cluster.name");
    System.err.println("Elasticsearch starting node = " + clusterName);
    System.err.println("Elasticsearch node path.home = " + settings.get("path.home"));
    System.err.println("Elasticsearch Remote Mode = " + remoteMode);
    // Remote mode support for connecting to remote elasticsearch cluster
    if (remoteMode) {
        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
        String[] nodeList = props.getProperty("es.hosts.list", DEFAULT_REMOTE_HOST).split(",");
        System.out.println("Elasticsearch Remote Hosts = " + props.getProperty("es.hosts.list", DEFAULT_REMOTE_HOST));
        TransportClient tClient = TransportClient.builder().settings(settings).build();
        for (String h : nodeList) {
            String[] nodes = h.split(":");
            try {
                tClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(nodes[0]), Integer.parseInt(nodes[1])));
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException("Unable to parse port number.", e);
            } catch (UnknownHostException e) {
                throw new IllegalArgumentException("Unable to Identify host.", e);
            }
        }
        client = tClient;
    } else {
        // Start node only if transport client mode is disabled
        node = nodeBuilder().clusterName(clusterName).settings(settings).node();
        node.start();
        client = node.client();
    }
    final boolean exists = client.admin().indices().exists(Requests.indicesExistsRequest(indexKey)).actionGet().isExists();
    if (exists && newdb) {
        client.admin().indices().prepareDelete(indexKey).execute().actionGet();
    }
    if (!exists || newdb) {
        client.admin().indices().create(new CreateIndexRequest(indexKey).settings(Settings.builder().put("index.number_of_shards", numberOfShards).put("index.number_of_replicas", numberOfReplicas).put("index.mapping._id.indexed", true))).actionGet();
    }
    client.admin().cluster().health(new ClusterHealthRequest().waitForGreenStatus()).actionGet();
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) 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) NodeBuilder.nodeBuilder(org.elasticsearch.node.NodeBuilder.nodeBuilder) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) Builder(org.elasticsearch.common.settings.Settings.Builder) Properties(java.util.Properties) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest)

Aggregations

TransportClient (org.elasticsearch.client.transport.TransportClient)31 Settings (org.elasticsearch.common.settings.Settings)17 InetSocketTransportAddress (org.elasticsearch.common.transport.InetSocketTransportAddress)15 TransportAddress (org.elasticsearch.common.transport.TransportAddress)8 ImmutableSettings (org.elasticsearch.common.settings.ImmutableSettings)7 PreBuiltTransportClient (org.elasticsearch.transport.client.PreBuiltTransportClient)7 Client (org.elasticsearch.client.Client)5 IOException (java.io.IOException)3 UnknownHostException (java.net.UnknownHostException)3 Test (org.junit.Test)3 Settings (org.sonar.api.config.Settings)3 File (java.io.File)2 String.format (java.lang.String.format)2 InetAddress (java.net.InetAddress)2 Logger (org.apache.log4j.Logger)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Condition (org.assertj.core.api.Condition)2 ClusterHealthResponse (org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse)2 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)2 Node (org.elasticsearch.node.Node)2