Search in sources :

Example 51 with InetSocketTransportAddress

use of org.elasticsearch.common.transport.InetSocketTransportAddress in project kylo by Teradata.

the class IndexElasticSearch method sendToElasticSearch.

private boolean sendToElasticSearch(String json, String hostName, String index, String type, String clusterName, String idField, String categoryName, String feedName) throws Exception {
    final ComponentLog logger = getLog();
    Settings settings = Settings.settingsBuilder().put("cluster.name", clusterName).build();
    Client client = TransportClient.builder().settings(settings).build().addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostName), 9300));
    JSONArray array = new JSONArray(json);
    BulkRequestBuilder bulkRequest = client.prepareBulk();
    for (int i = 0; i < array.length(); i++) {
        JSONObject jsonObj = array.getJSONObject(i);
        String id = null;
        if (idField != null && idField.length() > 0) {
            id = jsonObj.getString(idField);
            logger.debug("Document index id using field " + idField + ": " + id);
        } else if (StringUtils.isNotEmpty(categoryName) && (StringUtils.isNotEmpty(feedName))) {
            String hash = InsecureHashingMessageUtil.getHashMD5(jsonObj.toString());
            if (StringUtils.isNotEmpty(hash)) {
                id = categoryName + "::" + feedName + "::" + hash;
                logger.debug("Document index id using hash: " + id);
            }
        }
        if (StringUtils.isEmpty(id)) {
            id = UUID.randomUUID().toString();
            logger.debug("Document index id auto-generated + " + id);
        }
        jsonObj.put("post_date", String.valueOf(System.currentTimeMillis()));
        bulkRequest.add(client.prepareIndex(index, type, id).setSource(jsonObj.toString()));
    }
    BulkResponse bulkResponse = bulkRequest.get();
    if (bulkResponse.hasFailures()) {
        logger.error("Error occurred while batch updating" + bulkResponse.buildFailureMessage());
        return false;
    }
    return true;
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) JSONArray(org.codehaus.jettison.json.JSONArray) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) TransportClient(org.elasticsearch.client.transport.TransportClient) Client(org.elasticsearch.client.Client) ComponentLog(org.apache.nifi.logging.ComponentLog) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) Settings(org.elasticsearch.common.settings.Settings)

Example 52 with InetSocketTransportAddress

use of org.elasticsearch.common.transport.InetSocketTransportAddress in project play2-elasticsearch by cleverage.

the class IndexClient method start.

public void start() throws Exception {
    // Load Elasticsearch Settings
    Settings.Builder settings = loadSettings();
    // Check Model
    if (this.isLocalMode()) {
        Logger.info("ElasticSearch : Starting in Local Mode");
        NodeBuilder nb = nodeBuilder().settings(settings).local(true).client(false).data(true);
        node = nb.node();
        client = node.client();
        Logger.info("ElasticSearch : Started in Local Mode");
    } else {
        Logger.info("ElasticSearch : Starting in Client Mode");
        TransportClient c = TransportClient.builder().settings(settings).build();
        if (config.client == null) {
            throw new Exception("Configuration required - elasticsearch.client when local model is disabled!");
        }
        String[] hosts = config.client.trim().split(",");
        boolean done = false;
        for (String host : hosts) {
            String[] parts = host.split(":");
            if (parts.length != 2) {
                throw new Exception("Invalid Host: " + host);
            }
            Logger.info("ElasticSearch : Client - Host: " + parts[0] + " Port: " + parts[1]);
            c.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(parts[0]), Integer.valueOf(parts[1])));
            done = true;
        }
        if (!done) {
            throw new Exception("No Hosts Provided for ElasticSearch!");
        }
        client = c;
        Logger.info("ElasticSearch : Started in Client Mode");
    }
    // Check Client
    if (client == null) {
        throw new Exception("ElasticSearch Client cannot be null - please check the configuration provided and the health of your ElasticSearch instances.");
    }
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) NodeBuilder(org.elasticsearch.node.NodeBuilder) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) Settings(org.elasticsearch.common.settings.Settings) SettingsException(org.elasticsearch.common.settings.SettingsException)

Example 53 with InetSocketTransportAddress

use of org.elasticsearch.common.transport.InetSocketTransportAddress in project elasticsearch-jdbc by jprante.

the class NodeTestUtils method findNodeAddresses.

protected void findNodeAddresses() {
    NodesInfoRequest nodesInfoRequest = new NodesInfoRequest().transport(true);
    NodesInfoResponse response = client("1").admin().cluster().nodesInfo(nodesInfoRequest).actionGet();
    Iterator<NodeInfo> it = response.iterator();
    hosts = new LinkedList<>();
    hosts = new LinkedList<>();
    while (it.hasNext()) {
        NodeInfo nodeInfo = it.next();
        TransportInfo transportInfo = nodeInfo.getTransport();
        TransportAddress address = transportInfo.getAddress().publishAddress();
        if (address instanceof InetSocketTransportAddress) {
            InetSocketTransportAddress inetSocketTransportAddress = (InetSocketTransportAddress) address;
            hosts.add(inetSocketTransportAddress.address().getHostName() + ":" + inetSocketTransportAddress.address().getPort());
        }
    }
}
Also used : NodesInfoResponse(org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse) TransportInfo(org.elasticsearch.transport.TransportInfo) NodeInfo(org.elasticsearch.action.admin.cluster.node.info.NodeInfo) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) TransportAddress(org.elasticsearch.common.transport.TransportAddress) NodesInfoRequest(org.elasticsearch.action.admin.cluster.node.info.NodesInfoRequest) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress)

Example 54 with InetSocketTransportAddress

use of org.elasticsearch.common.transport.InetSocketTransportAddress in project metacat by Netflix.

the class ElasticSearchConfig method elasticSearchClient.

/**
 * The ElasticSearch client.
 *
 * @param config System config
 * @return Configured client or error
 */
@Bean
@ConditionalOnMissingBean(Client.class)
public Client elasticSearchClient(final Config config) {
    final String clusterName = config.getElasticSearchClusterName();
    if (StringUtils.isBlank(clusterName)) {
        throw new IllegalStateException("No cluster name set. Unable to continue");
    }
    final Settings settings = Settings.builder().put("cluster.name", clusterName).put("client.transport.sniff", // to dynamically add new hosts and remove old ones
    true).put("transport.tcp.connect_timeout", "60s").build();
    final TransportClient client = new PreBuiltTransportClient(settings);
    // Add the transport address if exists
    final String clusterNodesStr = config.getElasticSearchClusterNodes();
    if (StringUtils.isNotBlank(clusterNodesStr)) {
        final int port = config.getElasticSearchClusterPort();
        final Iterable<String> clusterNodes = Splitter.on(',').split(clusterNodesStr);
        clusterNodes.forEach(clusterNode -> {
            try {
                client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(clusterNode), port));
            } catch (UnknownHostException exception) {
                log.error("Skipping unknown host {}", clusterNode);
            }
        });
    }
    if (client.transportAddresses().isEmpty()) {
        throw new IllegalStateException("No Elasticsearch cluster nodes added. Unable to create client.");
    }
    return client;
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) UnknownHostException(java.net.UnknownHostException) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) Settings(org.elasticsearch.common.settings.Settings) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 55 with InetSocketTransportAddress

use of org.elasticsearch.common.transport.InetSocketTransportAddress in project wonderdog by infochimps-labs.

the class ElasticSearchStreamingRecordWriter method buildTransportClient.

/**
 *       Build a transport client that will connect to some
 *       Elasticsearch node.
 */
private Client buildTransportClient() {
    LOG.info("Connecting transport client to " + transportHost + ":" + Integer.toString(transportPort));
    Settings settings = ImmutableSettings.settingsBuilder().put("client.transport.ignore_cluster_name", "true").build();
    return new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress(transportHost, transportPort));
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) ImmutableSettings(org.elasticsearch.common.settings.ImmutableSettings) Settings(org.elasticsearch.common.settings.Settings)

Aggregations

InetSocketTransportAddress (org.elasticsearch.common.transport.InetSocketTransportAddress)69 TransportClient (org.elasticsearch.client.transport.TransportClient)37 Settings (org.elasticsearch.common.settings.Settings)35 PreBuiltTransportClient (org.elasticsearch.transport.client.PreBuiltTransportClient)25 UnknownHostException (java.net.UnknownHostException)17 InetSocketAddress (java.net.InetSocketAddress)11 TransportAddress (org.elasticsearch.common.transport.TransportAddress)10 HashMap (java.util.HashMap)8 ImmutableSettings (org.elasticsearch.common.settings.ImmutableSettings)7 Builder (org.elasticsearch.common.settings.Settings.Builder)7 ArrayList (java.util.ArrayList)6 Client (org.elasticsearch.client.Client)6 Bean (org.springframework.context.annotation.Bean)5 InetAddress (java.net.InetAddress)4 CreateIndexRequest (org.elasticsearch.action.admin.indices.create.CreateIndexRequest)4 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)4 Node (org.elasticsearch.node.Node)4 File (java.io.File)3 IOException (java.io.IOException)3 Properties (java.util.Properties)3