Search in sources :

Example 16 with InetSocketTransportAddress

use of org.elasticsearch.common.transport.InetSocketTransportAddress in project flink by apache.

the class ElasticsearchSinkExample method main.

public static void main(String[] args) throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    DataStream<String> source = env.generateSequence(0, 20).map(new MapFunction<Long, String>() {

        @Override
        public String map(Long value) throws Exception {
            return "message #" + value;
        }
    });
    Map<String, String> userConfig = new HashMap<>();
    userConfig.put("cluster.name", "elasticsearch");
    // This instructs the sink to emit after every element, otherwise they would be buffered
    userConfig.put(ElasticsearchSink.CONFIG_KEY_BULK_FLUSH_MAX_ACTIONS, "1");
    List<TransportAddress> transports = new ArrayList<>();
    transports.add(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
    source.addSink(new ElasticsearchSink<>(userConfig, transports, new ElasticsearchSinkFunction<String>() {

        @Override
        public void process(String element, RuntimeContext ctx, RequestIndexer indexer) {
            indexer.add(createIndexRequest(element));
        }
    }));
    env.execute("Elasticsearch Sink Example");
}
Also used : HashMap(java.util.HashMap) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) TransportAddress(org.elasticsearch.common.transport.TransportAddress) ArrayList(java.util.ArrayList) RequestIndexer(org.apache.flink.streaming.connectors.elasticsearch.RequestIndexer) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) ElasticsearchSinkFunction(org.apache.flink.streaming.connectors.elasticsearch.ElasticsearchSinkFunction) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) RuntimeContext(org.apache.flink.api.common.functions.RuntimeContext)

Example 17 with InetSocketTransportAddress

use of org.elasticsearch.common.transport.InetSocketTransportAddress 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();
    // Check if transport client needs to be used (To connect to multiple
    // elasticsearch nodes)
    remoteMode = Boolean.parseBoolean(props.getProperty("es.remote", "false"));
    final String pathHome = props.getProperty("path.home");
    // when running in embedded mode, require path.home
    if (!remoteMode && (pathHome == null || pathHome.isEmpty())) {
        throw new IllegalArgumentException("path.home must be specified when running in embedded mode");
    }
    this.indexKey = props.getProperty("es.index.key", DEFAULT_INDEX_KEY);
    int numberOfShards = parseIntegerProperty(props, "es.number_of_shards", NUMBER_OF_SHARDS);
    int numberOfReplicas = parseIntegerProperty(props, "es.number_of_replicas", NUMBER_OF_REPLICAS);
    Boolean newdb = Boolean.parseBoolean(props.getProperty("es.newdb", "false"));
    Builder settings = Settings.settingsBuilder().put("cluster.name", DEFAULT_CLUSTER_NAME).put("node.local", Boolean.toString(!remoteMode)).put("path.home", pathHome);
    // if properties file contains elasticsearch user defined properties
    // add it to the settings file (will overwrite the defaults).
    settings.put(props);
    final String clusterName = settings.get("cluster.name");
    System.err.println("Elasticsearch starting node = " + clusterName);
    System.err.println("Elasticsearch node path.home = " + settings.get("path.home"));
    System.err.println("Elasticsearch Remote Mode = " + remoteMode);
    // Remote mode support for connecting to remote elasticsearch cluster
    if (remoteMode) {
        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
        String[] nodeList = props.getProperty("es.hosts.list", DEFAULT_REMOTE_HOST).split(",");
        System.out.println("Elasticsearch Remote Hosts = " + props.getProperty("es.hosts.list", DEFAULT_REMOTE_HOST));
        TransportClient tClient = TransportClient.builder().settings(settings).build();
        for (String h : nodeList) {
            String[] nodes = h.split(":");
            try {
                tClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(nodes[0]), Integer.parseInt(nodes[1])));
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException("Unable to parse port number.", e);
            } catch (UnknownHostException e) {
                throw new IllegalArgumentException("Unable to Identify host.", e);
            }
        }
        client = tClient;
    } else {
        // Start node only if transport client mode is disabled
        node = nodeBuilder().clusterName(clusterName).settings(settings).node();
        node.start();
        client = node.client();
    }
    final boolean exists = client.admin().indices().exists(Requests.indicesExistsRequest(indexKey)).actionGet().isExists();
    if (exists && newdb) {
        client.admin().indices().prepareDelete(indexKey).execute().actionGet();
    }
    if (!exists || newdb) {
        client.admin().indices().create(new CreateIndexRequest(indexKey).settings(Settings.builder().put("index.number_of_shards", numberOfShards).put("index.number_of_replicas", numberOfReplicas).put("index.mapping._id.indexed", true))).actionGet();
    }
    client.admin().cluster().health(new ClusterHealthRequest().waitForGreenStatus()).actionGet();
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) UnknownHostException(java.net.UnknownHostException) ClusterHealthRequest(org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentFactory.jsonBuilder(org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder) NodeBuilder.nodeBuilder(org.elasticsearch.node.NodeBuilder.nodeBuilder) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) Builder(org.elasticsearch.common.settings.Settings.Builder) Properties(java.util.Properties) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest)

Example 18 with InetSocketTransportAddress

use of org.elasticsearch.common.transport.InetSocketTransportAddress in project fess by codelibs.

the class EsDataStoreImpl method storeData.

@Override
protected void storeData(final DataConfig dataConfig, final IndexUpdateCallback callback, final Map<String, String> paramMap, final Map<String, String> scriptMap, final Map<String, Object> defaultDataMap) {
    final String hostsStr = paramMap.get(HOSTS);
    if (StringUtil.isBlank(hostsStr)) {
        logger.info("hosts is empty.");
        return;
    }
    final long readInterval = getReadInterval(paramMap);
    final Settings settings = Settings.builder().put(paramMap.entrySet().stream().filter(e -> e.getKey().startsWith(SETTINGS_PREFIX)).collect(Collectors.toMap(e -> e.getKey().replaceFirst("^settings\\.", StringUtil.EMPTY), e -> e.getValue()))).build();
    logger.info("Connecting to " + hostsStr + " with [" + settings.toDelimitedString(',') + "]");
    final InetSocketTransportAddress[] addresses = split(hostsStr, ",").get(stream -> stream.map(h -> {
        final String[] values = h.trim().split(":");
        try {
            if (values.length == 1) {
                return new InetSocketTransportAddress(InetAddress.getByName(values[0]), 9300);
            } else if (values.length == 2) {
                return new InetSocketTransportAddress(InetAddress.getByName(values[0]), Integer.parseInt(values[1]));
            }
        } catch (final Exception e) {
            logger.warn("Failed to parse address: " + h, e);
        }
        return null;
    }).filter(v -> v != null).toArray(n -> new InetSocketTransportAddress[n]));
    try (PreBuiltTransportClient client = new PreBuiltTransportClient(settings)) {
        client.addTransportAddresses(addresses);
        processData(dataConfig, callback, paramMap, scriptMap, defaultDataMap, readInterval, client);
    }
}
Also used : CrawlingAccessException(org.codelibs.fess.crawler.exception.CrawlingAccessException) Constants(org.codelibs.fess.Constants) MultipleCrawlingAccessException(org.codelibs.fess.crawler.exception.MultipleCrawlingAccessException) SearchHits(org.elasticsearch.search.SearchHits) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) IndexUpdateCallback(org.codelibs.fess.ds.IndexUpdateCallback) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) InetAddress(java.net.InetAddress) LinkedHashMap(java.util.LinkedHashMap) StreamUtil.split(org.codelibs.core.stream.StreamUtil.split) Settings(org.elasticsearch.common.settings.Settings) Map(java.util.Map) SearchResponse(org.elasticsearch.action.search.SearchResponse) DataConfig(org.codelibs.fess.es.config.exentity.DataConfig) SearchHit(org.elasticsearch.search.SearchHit) DataStoreCrawlingException(org.codelibs.fess.exception.DataStoreCrawlingException) Logger(org.slf4j.Logger) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) Client(org.elasticsearch.client.Client) StringUtil(org.codelibs.core.lang.StringUtil) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) DataStoreException(org.codelibs.fess.exception.DataStoreException) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) Collectors(java.util.stream.Collectors) ComponentUtil(org.codelibs.fess.util.ComponentUtil) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) FailureUrlService(org.codelibs.fess.app.service.FailureUrlService) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) Settings(org.elasticsearch.common.settings.Settings) CrawlingAccessException(org.codelibs.fess.crawler.exception.CrawlingAccessException) MultipleCrawlingAccessException(org.codelibs.fess.crawler.exception.MultipleCrawlingAccessException) DataStoreCrawlingException(org.codelibs.fess.exception.DataStoreCrawlingException) DataStoreException(org.codelibs.fess.exception.DataStoreException)

Example 19 with InetSocketTransportAddress

use of org.elasticsearch.common.transport.InetSocketTransportAddress in project flink by apache.

the class ElasticsearchUtils method convertInetSocketAddresses.

/**
	 * Utility method to convert a {@link List} of {@link InetSocketAddress} to Elasticsearch {@link TransportAddress}.
	 *
	 * @param inetSocketAddresses The list of {@link InetSocketAddress} to convert.
	 */
public static List<TransportAddress> convertInetSocketAddresses(List<InetSocketAddress> inetSocketAddresses) {
    if (inetSocketAddresses == null) {
        return null;
    } else {
        List<TransportAddress> converted;
        converted = new ArrayList<>(inetSocketAddresses.size());
        for (InetSocketAddress address : inetSocketAddresses) {
            converted.add(new InetSocketTransportAddress(address));
        }
        return converted;
    }
}
Also used : TransportAddress(org.elasticsearch.common.transport.TransportAddress) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) InetSocketAddress(java.net.InetSocketAddress) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress)

Example 20 with InetSocketTransportAddress

use of org.elasticsearch.common.transport.InetSocketTransportAddress in project crate by crate.

the class BlobPathITest method launchNodeAndInitClient.

private void launchNodeAndInitClient(Settings settings) throws Exception {
    // using numDataNodes = 1 to launch the node doesn't work:
    // if globalBlobPath is created within nodeSetting it is sometimes not available for the tests
    internalCluster().startNode(settings);
    blobAdminClient = internalCluster().getInstance(BlobAdminClient.class);
    HttpServerTransport httpServerTransport = internalCluster().getInstance(HttpServerTransport.class);
    InetSocketAddress address = ((InetSocketTransportAddress) httpServerTransport.boundAddress().publishAddress()).address();
    client = new BlobHttpClient(address);
}
Also used : BlobAdminClient(io.crate.blob.v2.BlobAdminClient) InetSocketAddress(java.net.InetSocketAddress) HttpServerTransport(org.elasticsearch.http.HttpServerTransport) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress)

Aggregations

InetSocketTransportAddress (org.elasticsearch.common.transport.InetSocketTransportAddress)35 TransportClient (org.elasticsearch.client.transport.TransportClient)15 Settings (org.elasticsearch.common.settings.Settings)14 ImmutableSettings (org.elasticsearch.common.settings.ImmutableSettings)7 TransportAddress (org.elasticsearch.common.transport.TransportAddress)7 PreBuiltTransportClient (org.elasticsearch.transport.client.PreBuiltTransportClient)7 InetSocketAddress (java.net.InetSocketAddress)5 UnknownHostException (java.net.UnknownHostException)5 IOException (java.io.IOException)4 InetAddress (java.net.InetAddress)3 ArrayList (java.util.ArrayList)3 NodesInfoResponse (org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse)3 BoundTransportAddress (org.elasticsearch.common.transport.BoundTransportAddress)3 File (java.io.File)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Properties (java.util.Properties)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Logger (org.apache.log4j.Logger)2 ElasticsearchClusterRunner (org.codelibs.elasticsearch.runner.ElasticsearchClusterRunner)2