Search in sources :

Example 21 with PreBuiltTransportClient

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;
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) TransportAddress(org.elasticsearch.common.transport.TransportAddress) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) XmallException(cn.exrick.common.exception.XmallException) Settings(org.elasticsearch.common.settings.Settings) XmallException(cn.exrick.common.exception.XmallException) SearchItem(cn.exrick.manager.dto.front.SearchItem)

Example 22 with PreBuiltTransportClient

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;
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) TransportAddress(org.elasticsearch.common.transport.TransportAddress)

Example 23 with PreBuiltTransportClient

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));
}
Also used : ElasticsearchClusterRunner(org.codelibs.elasticsearch.runner.ElasticsearchClusterRunner) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) Builder(org.elasticsearch.common.settings.Settings.Builder) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) BeforeClass(org.junit.BeforeClass)

Example 24 with PreBuiltTransportClient

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);
}
Also used : PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) UnknownHostException(java.net.UnknownHostException) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) Settings(org.elasticsearch.common.settings.Settings) TextIndexES(org.apache.jena.query.text.es.TextIndexES) BeforeClass(org.junit.BeforeClass)

Example 25 with PreBuiltTransportClient

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;
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) ElasticsearchConfiguration(gov.ca.cwds.rest.ElasticsearchConfiguration) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) ApiException(gov.ca.cwds.rest.api.ApiException) ApiException(gov.ca.cwds.rest.api.ApiException) Provides(com.google.inject.Provides)

Aggregations

PreBuiltTransportClient (org.elasticsearch.transport.client.PreBuiltTransportClient)41 Settings (org.elasticsearch.common.settings.Settings)33 TransportClient (org.elasticsearch.client.transport.TransportClient)25 InetSocketTransportAddress (org.elasticsearch.common.transport.InetSocketTransportAddress)23 TransportAddress (org.elasticsearch.common.transport.TransportAddress)17 UnknownHostException (java.net.UnknownHostException)10 InetSocketAddress (java.net.InetSocketAddress)5 InetAddress (java.net.InetAddress)4 HashMap (java.util.HashMap)4 PostConstruct (javax.annotation.PostConstruct)4 SearchItem (cn.exrick.manager.dto.front.SearchItem)3 IOException (java.io.IOException)3 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)3 IndexResponse (org.elasticsearch.action.index.IndexResponse)3 Builder (org.elasticsearch.common.settings.Settings.Builder)3 XmallException (cn.exrick.common.exception.XmallException)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Logger (org.apache.log4j.Logger)2 ElasticsearchClusterRunner (org.codelibs.elasticsearch.runner.ElasticsearchClusterRunner)2