本文整理汇总了Java中org.elasticsearch.client.transport.TransportClient类的典型用法代码示例。如果您正苦于以下问题:Java TransportClient类的具体用法?Java TransportClient怎么用?Java TransportClient使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
TransportClient类属于org.elasticsearch.client.transport包,在下文中一共展示了TransportClient类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: indexJSONTableDocuments
import org.elasticsearch.client.transport.TransportClient; //导入依赖的package包/类
private void indexJSONTableDocuments(TransportClient client, String indexName, String typeName, String tablePath, String... fields) {
loginTestUser(TEST_USER_NAME, TEST_USER_GROUP);
// Create an OJAI connection to MapR cluster
Connection connection = DriverManager.getConnection(CONNECTION_URL);
// Get an instance of OJAI DocumentStore
final DocumentStore store = connection.getStore(tablePath);
DocumentStream documentStream = store.find(fields);
for (Document document : documentStream) {
IndexResponse response = client.prepareIndex(indexName, typeName, document.getId().getString())
.setSource(document.asJsonString(), XContentType.JSON)
.get();
log.info("Elasticsearch Index Response: '{}'", response);
}
// Close this instance of OJAI DocumentStore
store.close();
// Close the OJAI connection and release any resources held by the connection
connection.close();
}
示例2: getElasticClient
import org.elasticsearch.client.transport.TransportClient; //导入依赖的package包/类
private TransportClient getElasticClient() {
try {
// un-command this, if you have multiple node
// TransportClient client1 = new PreBuiltTransportClient(Settings.EMPTY)
// .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host1"), 9300))
// .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host1"), 9300));
Settings setting = Settings.builder()
.put("cluster.name", elasticPro.getProperty("cluster"))
.put("client.transport.sniff", Boolean.valueOf(elasticPro.getProperty("transport.sniff"))).build();
client = new PreBuiltTransportClient(setting)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(elasticPro.getProperty("host")), Integer.valueOf(elasticPro.getProperty("port"))));
} catch (UnknownHostException ex) {
log.error("Exception occurred while getting Client : " + ex, ex);
}
return client;
}
示例3: createClient
import org.elasticsearch.client.transport.TransportClient; //导入依赖的package包/类
private TransportClient createClient() {
Config cfg = getTypeCfg();
Settings settings = Settings.builder().put("cluster.name", cfg.getString("elastic.cluster-name")).build();
TransportClient client = new PreBuiltTransportClient(settings);
List<String> servers = cfg.getStringList("elastic.servers");
logger.debug(marker, "Elastic Servers: {}", servers);
for (String addr : servers) {
try {
String[] a = addr.split(":");
String host = a[0];
int port = Integer.parseInt(a[1]);
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), port));
} catch (Exception e) {
logger.error(marker, "Transport client creation failed for '{}'", addr, e);
}
}
return client;
}
示例4: getClient
import org.elasticsearch.client.transport.TransportClient; //导入依赖的package包/类
public static TransportClient getClient() {
try {
if (tclient == null) {
String EsHosts = "192.168.1.41:9300,192.168.1.42:9300,192.168.1.43:9300";
Settings settings = Settings.settingsBuilder()
.put("cluster.name", "dkes")//设置集群名称
.put("tclient.transport.sniff", true).build();//自动嗅探整个集群的状态,把集群中其它机器的ip地址加到客户端中
tclient = TransportClient.builder().settings(settings).build();
String[] nodes = EsHosts.split(",");
for (String node : nodes) {
if (node.length() > 0) {//跳过为空的node(当开头、结尾有逗号或多个连续逗号时会出现空node)
String[] hostPort = node.split(":");
tclient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostPort[0]), Integer.parseInt(hostPort[1])));
}
}
}//if
} catch (Exception e) {
e.printStackTrace();
}
return tclient;
}
示例5: batchDelete
import org.elasticsearch.client.transport.TransportClient; //导入依赖的package包/类
/**
* 批量删除
*
* @param transportClient
*/
private static void batchDelete(TransportClient transportClient) throws IOException {
BulkRequestBuilder bulkRequestBuilder = transportClient.prepareBulk();
DeleteRequestBuilder deleteRequestBuilder1 = transportClient.prepareDelete("product_index", "product", "1");
DeleteRequestBuilder deleteRequestBuilder2 = transportClient.prepareDelete("product_index", "product", "2");
DeleteRequestBuilder deleteRequestBuilder3 = transportClient.prepareDelete("product_index", "product", "3");
bulkRequestBuilder.add(deleteRequestBuilder1);
bulkRequestBuilder.add(deleteRequestBuilder2);
bulkRequestBuilder.add(deleteRequestBuilder3);
BulkResponse bulkResponse = bulkRequestBuilder.get();
for (BulkItemResponse bulkItemResponse : bulkResponse.getItems()) {
logger.info("--------------------------------version= " + bulkItemResponse.getVersion());
}
}
示例6: setUpClass
import org.elasticsearch.client.transport.TransportClient; //导入依赖的package包/类
@BeforeClass
public static void setUpClass() throws Exception {
faker = new Faker();
String ip = esContainer.getContainerIpAddress();
Integer transportPort = esContainer.getMappedPort(9300);
MapConfiguration memoryParams = new MapConfiguration(new HashMap<>());
memoryParams.setProperty(CONFIG_ES_CLUSTER_HOST, ip);
memoryParams.setProperty(CONFIG_ES_CLUSTER_PORT, transportPort);
memoryParams.setProperty(CONFIG_ES_CLUSTER_NAME, "elasticsearch");
injector = Guice.createInjector(
Modules.override(new ElasticSearchModule())
.with(binder -> {
binder.bind(Configuration.class).toProvider(() -> memoryParams);
})
);
productDao = injector.getInstance(ProductDao.class);
esClient = injector.getInstance(TransportClient.class);
}
示例7: wildcardQuery
import org.elasticsearch.client.transport.TransportClient; //导入依赖的package包/类
/**
* wildcard查询/or条件/and条件
*/
public static void wildcardQuery() {
try {
Settings settings = Settings.settingsBuilder().put("cluster.name", "elasticsearch1").build();
TransportClient transportClient = TransportClient.builder().
settings(settings).build().addTransportAddress(
new InetSocketTransportAddress(InetAddress.getByName("172.16.2.94"), 9300));
SearchRequestBuilder searchRequestBuilder = transportClient.prepareSearch("sqd.es_start");
// {"query": {"bool": {"must": [{"or": [{"wildcard": {"content": "*oracle*"}},{"wildcard": {"content": "*mysql*"}}]}],"must_not": [],"should": []}},"from": 0, "size": 10, "sort": [],"aggs": {}}
SearchResponse searchResponse = searchRequestBuilder.
setQuery(QueryBuilders.boolQuery()
.must(QueryBuilders.orQuery(QueryBuilders.wildcardQuery("content","*mysql*"),
QueryBuilders.wildcardQuery("content","*oracle*")))
.must(QueryBuilders.termQuery("tbool","false")))
.setFrom(0).setSize(100).setExplain(true).execute().actionGet();
SearchHits searchHits = searchResponse.getHits();
System.out.println();
System.out.println("Total Hits is " + searchHits.totalHits());
System.out.println();
for (int i = 0; i < searchHits.getHits().length; ++i) {
System.out.println("content is "
+ searchHits.getHits()[i].getSource().get("content"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
示例8: multisearch
import org.elasticsearch.client.transport.TransportClient; //导入依赖的package包/类
/**
* 多字段查询
*/
public static void multisearch() {
try {
Settings settings = Settings.settingsBuilder().put("cluster.name", "elasticsearch1").build();
TransportClient transportClient = TransportClient.builder().
settings(settings).build().addTransportAddress(
new InetSocketTransportAddress(InetAddress.getByName("172.16.2.93"), 9300));
SearchRequestBuilder searchRequestBuilder = transportClient.prepareSearch("service2","clients");
SearchResponse searchResponse = searchRequestBuilder.
setQuery(QueryBuilders.boolQuery()
.should(QueryBuilders.termQuery("id","5"))
.should(QueryBuilders.prefixQuery("content","oracle")))
.setFrom(0).setSize(100).setExplain(true).execute().actionGet();
SearchHits searchHits = searchResponse.getHits();
System.out.println();
System.out.println("Total Hits is " + searchHits.totalHits());
System.out.println();
} catch (Exception e) {
e.printStackTrace();
}
}
示例9: queryByMultiGet
import org.elasticsearch.client.transport.TransportClient; //导入依赖的package包/类
/**
* 获取多个对象(根据ID)
*
* @param transportClient
* @throws IOException
*/
private static void queryByMultiGet(TransportClient transportClient) throws IOException {
MultiGetResponse multiGetItemResponses = transportClient.prepareMultiGet()
.add("product_index", "product", "1")
.add("product_index", "product", "2")
.add("product_index", "product", "3")
.add("product_index", "product", "4")
.add("product_index", "product", "5")
.get();
String resultJSON = null;
for (MultiGetItemResponse multiGetItemResponse : multiGetItemResponses) {
GetResponse getResponse = multiGetItemResponse.getResponse();
if (getResponse.isExists()) {
resultJSON = getResponse.getSourceAsString();
}
}
logger.info("--------------------------------:" + resultJSON);
}
示例10: queryByScroll
import org.elasticsearch.client.transport.TransportClient; //导入依赖的package包/类
/**
* Scroll 获取多个对象
*
* @param transportClient
* @throws IOException
*/
private static void queryByScroll(TransportClient transportClient) throws IOException {
//setSize 是设置每批查询多少条数据
SearchResponse searchResponse = transportClient.prepareSearch("product_index").setTypes("product")
.setQuery(QueryBuilders.termQuery("product_name", "飞利浦"))
.setScroll(new TimeValue(60000))
.setSize(3)
.get();
int count = 0;
do {
for (SearchHit searchHit : searchResponse.getHits().getHits()) {
//打印查询结果,或者做其他处理
logger.info("count=" + ++count);
logger.info(searchHit.getSourceAsString());
}
searchResponse = transportClient.prepareSearchScroll(searchResponse.getScrollId()).setScroll(new TimeValue(60000))
.execute()
.actionGet();
} while (searchResponse.getHits().getHits().length != 0);
}
示例11: buildBulkProcessor
import org.elasticsearch.client.transport.TransportClient; //导入依赖的package包/类
public BulkProcessor buildBulkProcessor(Context context, TransportClient client) {
bulkActions = context.getInteger(ES_BULK_ACTIONS,
DEFAULT_ES_BULK_ACTIONS);
bulkProcessorName = context.getString(ES_BULK_PROCESSOR_NAME,
DEFAULT_ES_BULK_PROCESSOR_NAME);
bulkSize = Util.getByteSizeValue(context.getInteger(ES_BULK_SIZE),
context.getString(ES_BULK_SIZE_UNIT));
concurrentRequest = context.getInteger(ES_CONCURRENT_REQUEST,
DEFAULT_ES_CONCURRENT_REQUEST);
flushIntervalTime = Util.getTimeValue(context.getString(ES_FLUSH_INTERVAL_TIME),
DEFAULT_ES_FLUSH_INTERVAL_TIME);
backoffPolicyTimeInterval = context.getString(ES_BACKOFF_POLICY_TIME_INTERVAL,
DEFAULT_ES_BACKOFF_POLICY_START_DELAY);
backoffPolicyRetries = context.getInteger(ES_BACKOFF_POLICY_RETRIES,
DEFAULT_ES_BACKOFF_POLICY_RETRIES);
return build(client);
}
示例12: configure
import org.elasticsearch.client.transport.TransportClient; //导入依赖的package包/类
@Override
public void configure(Context context) {
String[] hosts = getHosts(context);
if(ArrayUtils.isNotEmpty(hosts)) {
TransportClient client = new ElasticsearchClientBuilder(
context.getString(PREFIX + ES_CLUSTER_NAME, DEFAULT_ES_CLUSTER_NAME), hosts)
.setTransportSniff(context.getBoolean(
PREFIX + ES_TRANSPORT_SNIFF, false))
.setIgnoreClusterName(context.getBoolean(
PREFIX + ES_IGNORE_CLUSTER_NAME, false))
.setTransportPingTimeout(Util.getTimeValue(context.getString(
PREFIX + ES_TRANSPORT_PING_TIMEOUT), DEFAULT_ES_TIME))
.setNodeSamplerInterval(Util.getTimeValue(context.getString(
PREFIX + ES_TRANSPORT_NODE_SAMPLER_INTERVAL), DEFAULT_ES_TIME))
.build();
buildIndexBuilder(context);
buildSerializer(context);
bulkProcessor = new BulkProcessorBuilder().buildBulkProcessor(context, client);
} else {
logger.error("Could not create transport client, No host exist");
}
}
示例13: getClient
import org.elasticsearch.client.transport.TransportClient; //导入依赖的package包/类
public TransportClient getClient() {
if (esClient == null) {
synchronized (this) {
if (esClient == null) {
try {
//判断配置
Preconditions.checkNotNull(clusterName, "es 服务clusterName未配置");
Preconditions.checkNotNull(addresses, "es 服务ip未配置");
//Preconditions.checkArgument(esPort > 0, "es 服务服务port未配置");
//设置集群的名字
Settings settings = Settings.settingsBuilder().put("client.node", true).put("cluster.name", clusterName).put("client.transport.sniff", sniff).build();
//Settings settings = Settings.settingsBuilder().put("client.transport.sniff", sniff).build();
//创建集群client并添加集群节点地址
esClient = TransportClient.builder().settings(settings).build();
for (String address : addresses) {
String[] inetAddress = address.split(":");
esClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(inetAddress[0]), new Integer(inetAddress[1])));
}
}catch (Exception e){
LOGGER.error("客户端连接初始化异常",e);
}
}
}
}
return esClient;
}
示例14: failureHandlerExecutesFailoverForEachBatchItemSeparately
import org.elasticsearch.client.transport.TransportClient; //导入依赖的package包/类
@Test
public void failureHandlerExecutesFailoverForEachBatchItemSeparately() {
// given
Builder builder = createTestObjectFactoryBuilder();
ClientObjectFactory<TransportClient, BulkRequest> config = builder.build();
FailoverPolicy failoverPolicy = spy(new NoopFailoverPolicy());
String payload1 = "test1";
String payload2 = "test2";
BulkRequest bulk = new BulkRequest()
.add(spy(new IndexRequest().source(payload1, XContentType.CBOR)))
.add(spy(new IndexRequest().source(payload2, XContentType.CBOR)));
// when
config.createFailureHandler(failoverPolicy).apply(bulk);
// then
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(failoverPolicy, times(2)).deliver((String) captor.capture());
assertTrue(captor.getAllValues().contains(payload1));
assertTrue(captor.getAllValues().contains(payload2));
}
示例15: failureHandlerExecutesFailoverForEachBatchItemSeparately
import org.elasticsearch.client.transport.TransportClient; //导入依赖的package包/类
@Test
public void failureHandlerExecutesFailoverForEachBatchItemSeparately() {
// given
Builder builder = createTestObjectFactoryBuilder();
ClientObjectFactory<TransportClient, BulkRequest> config = builder.build();
FailoverPolicy failoverPolicy = spy(new NoopFailoverPolicy());
String payload1 = "test1";
String payload2 = "test2";
BulkRequest bulk = new BulkRequest()
.add(spy(new IndexRequest().source(payload1)))
.add(spy(new IndexRequest().source(payload2)));
// when
config.createFailureHandler(failoverPolicy).apply(bulk);
// then
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(failoverPolicy, times(2)).deliver((String) captor.capture());
assertTrue(captor.getAllValues().contains(payload1));
assertTrue(captor.getAllValues().contains(payload2));
}