use of org.elasticsearch.transport.client.PreBuiltTransportClient in project xmall by Exrick.
the class SearchItemServiceImpl method importAllItems.
@Override
public int importAllItems() {
try {
Settings settings = Settings.builder().put("cluster.name", ES_CLUSTER_NAME).build();
TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(new TransportAddress(InetAddress.getByName(ES_CONNECT_IP), 9300));
// 批量添加
BulkRequestBuilder bulkRequest = client.prepareBulk();
// 查询商品列表
List<SearchItem> itemList = itemMapper.getItemList();
// 遍历商品列表
for (SearchItem searchItem : itemList) {
String image = searchItem.getProductImageBig();
if (image != null && !"".equals(image)) {
String[] strings = image.split(",");
image = strings[0];
} else {
image = "";
}
searchItem.setProductImageBig(image);
bulkRequest.add(client.prepareIndex(ITEM_INDEX, ITEM_TYPE, String.valueOf(searchItem.getProductId())).setSource(jsonBuilder().startObject().field("productId", searchItem.getProductId()).field("salePrice", searchItem.getSalePrice()).field("productName", searchItem.getProductName()).field("subTitle", searchItem.getSubTitle()).field("productImageBig", searchItem.getProductImageBig()).field("categoryName", searchItem.getCategoryName()).field("cid", searchItem.getCid()).endObject()));
}
BulkResponse bulkResponse = bulkRequest.get();
log.info("更新索引成功");
client.close();
} catch (Exception e) {
e.printStackTrace();
throw new XmallException("导入ES索引库出错,请再次尝试");
}
return 1;
}
use of org.elasticsearch.transport.client.PreBuiltTransportClient in project elasticsearch by elastic.
the class TransportClientBenchmark method client.
@Override
protected TransportClient client(String benchmarkTargetHost) throws Exception {
TransportClient client = new PreBuiltTransportClient(Settings.EMPTY, NoopPlugin.class);
client.addTransportAddress(new TransportAddress(InetAddress.getByName(benchmarkTargetHost), 9300));
return client;
}
use of org.elasticsearch.transport.client.PreBuiltTransportClient in project camel by apache.
the class ElasticsearchClusterBaseTest method cleanUpOnce.
@BeforeClass
public static void cleanUpOnce() throws Exception {
deleteDirectory("target/testcluster/");
clusterName = "es-cl-run-" + System.currentTimeMillis();
// create runner instance
runner = new ElasticsearchClusterRunner();
// create ES nodes
runner.onBuild(new ElasticsearchClusterRunner.Builder() {
@Override
public void build(final int number, final Builder settingsBuilder) {
settingsBuilder.put("http.cors.enabled", true);
settingsBuilder.put("http.cors.allow-origin", "*");
}
}).build(newConfigs().clusterName(clusterName).numOfNode(3).basePath("target/testcluster/").useLogger());
// wait for green status
runner.ensureGreen();
client = new PreBuiltTransportClient(getSettings()).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9301));
}
use of org.elasticsearch.transport.client.PreBuiltTransportClient in project jena by apache.
the class BaseESTest method setupTransportClient.
/**
* Make sure that we have connectivity to the locally running ES node.
* The ES is started during the pre-integration-test phase
*/
@BeforeClass
public static void setupTransportClient() {
Settings settings = Settings.builder().put("cluster.name", CLUSTER_NAME).build();
transportClient = new PreBuiltTransportClient(settings);
try {
transportClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ADDRESS), PORT));
} catch (UnknownHostException ex) {
Assert.fail("Failed to create transport client" + ex.getMessage());
}
classToTest = new TextIndexES(config(), transportClient, INDEX_NAME);
Assert.assertNotNull("Transport client was not created successfully", transportClient);
}
use of org.elasticsearch.transport.client.PreBuiltTransportClient in project API by ca-cwds.
the class DataAccessModule method elasticsearchClient.
// @Singleton
@Provides
public synchronized Client elasticsearchClient(ApiConfiguration apiConfiguration) {
if (client == null) {
ElasticsearchConfiguration config = apiConfiguration.getElasticsearchConfiguration();
try {
// Settings settings = Settings.settingsBuilder()
// .put("cluster.name", config.getElasticsearchCluster()).build();
// client = TransportClient.builder().settings(settings).build().addTransportAddress(
// new InetSocketTransportAddress(InetAddress.getByName(config.getElasticsearchHost()),
// Integer.parseInt(config.getElasticsearchPort())));
TransportClient ret = new PreBuiltTransportClient(Settings.builder().put("cluster.name", config.getElasticsearchCluster()).build());
ret.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(config.getElasticsearchHost()), Integer.parseInt(config.getElasticsearchPort())));
client = ret;
} catch (Exception e) {
LOGGER.error("Error initializing Elasticsearch client: {}", e.getMessage(), e);
throw new ApiException("Error initializing Elasticsearch client: " + e.getMessage(), e);
}
}
return client;
}
Aggregations