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;
}
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.");
}
}
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());
}
}
}
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;
}
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));
}
Aggregations