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