Search in sources :

Example 1 with NodeBuilder

use of org.elasticsearch.node.NodeBuilder in project titan by thinkaurelius.

the class ElasticSearchConfigTest method testIndexCreationOptions.

@Test
public void testIndexCreationOptions() throws InterruptedException, BackendException {
    final int shards = 77;
    ElasticsearchRunner esr = new ElasticsearchRunner(".", "indexCreationOptions.yml");
    esr.start();
    CommonsConfiguration cc = new CommonsConfiguration(new BaseConfiguration());
    cc.set("index." + INDEX_NAME + ".elasticsearch.create.ext.number_of_shards", String.valueOf(shards));
    cc.set("index." + INDEX_NAME + ".elasticsearch.ext.cluster.name", "indexCreationOptions");
    ModifiableConfiguration config = new ModifiableConfiguration(GraphDatabaseConfiguration.ROOT_NS, cc, BasicConfiguration.Restriction.NONE);
    config.set(INTERFACE, ElasticSearchSetup.NODE.toString(), INDEX_NAME);
    Configuration indexConfig = config.restrictTo(INDEX_NAME);
    IndexProvider idx = new ElasticSearchIndex(indexConfig);
    simpleWriteAndQuery(idx);
    ImmutableSettings.Builder settingsBuilder = ImmutableSettings.settingsBuilder();
    settingsBuilder.put("discovery.zen.ping.multicast.enabled", "false");
    settingsBuilder.put("discovery.zen.ping.unicast.hosts", "localhost,127.0.0.1:9300");
    settingsBuilder.put("cluster.name", "indexCreationOptions");
    NodeBuilder nodeBuilder = NodeBuilder.nodeBuilder().settings(settingsBuilder.build());
    nodeBuilder.client(true).data(false).local(false);
    Node n = nodeBuilder.build().start();
    GetSettingsResponse response = n.client().admin().indices().getSettings(new GetSettingsRequest().indices("titan")).actionGet();
    assertEquals(String.valueOf(shards), response.getSetting("titan", "index.number_of_shards"));
    idx.close();
    n.stop();
    esr.stop();
}
Also used : Configuration(com.thinkaurelius.titan.diskstorage.configuration.Configuration) CommonsConfiguration(com.thinkaurelius.titan.diskstorage.configuration.backend.CommonsConfiguration) BasicConfiguration(com.thinkaurelius.titan.diskstorage.configuration.BasicConfiguration) GraphDatabaseConfiguration(com.thinkaurelius.titan.graphdb.configuration.GraphDatabaseConfiguration) BaseConfiguration(org.apache.commons.configuration.BaseConfiguration) ModifiableConfiguration(com.thinkaurelius.titan.diskstorage.configuration.ModifiableConfiguration) GetSettingsResponse(org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse) Node(org.elasticsearch.node.Node) CommonsConfiguration(com.thinkaurelius.titan.diskstorage.configuration.backend.CommonsConfiguration) ModifiableConfiguration(com.thinkaurelius.titan.diskstorage.configuration.ModifiableConfiguration) NodeBuilder(org.elasticsearch.node.NodeBuilder) ImmutableSettings(org.elasticsearch.common.settings.ImmutableSettings) BaseConfiguration(org.apache.commons.configuration.BaseConfiguration) GetSettingsRequest(org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest) ElasticSearchIndex(com.thinkaurelius.titan.diskstorage.es.ElasticSearchIndex) Test(org.junit.Test)

Example 2 with NodeBuilder

use of org.elasticsearch.node.NodeBuilder in project spring-boot by spring-projects.

the class ElasticsearchAutoConfiguration method createNodeClient.

private Client createNodeClient() throws Exception {
    Settings.Builder settings = Settings.settingsBuilder();
    for (Map.Entry<String, String> entry : DEFAULTS.entrySet()) {
        if (!this.properties.getProperties().containsKey(entry.getKey())) {
            settings.put(entry.getKey(), entry.getValue());
        }
    }
    settings.put(this.properties.getProperties());
    Node node = new NodeBuilder().settings(settings).clusterName(this.properties.getClusterName()).node();
    this.releasable = node;
    return node.client();
}
Also used : Node(org.elasticsearch.node.Node) NodeBuilder(org.elasticsearch.node.NodeBuilder) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Settings(org.elasticsearch.common.settings.Settings)

Example 3 with NodeBuilder

use of org.elasticsearch.node.NodeBuilder in project titan by thinkaurelius.

the class ElasticSearchIndex method legacyConfiguration.

/**
     * Configure ElasticSearchIndex's ES client according to 0.4.x - 0.5.0 semantics.
     * This checks local-mode first.  If local-mode is true, then it creates a Node that
     * uses JVM local transport and can't talk over the network.  If local-mode is
     * false, then it creates a TransportClient that can talk over the network and
     * uses {@link com.thinkaurelius.titan.graphdb.configuration.GraphDatabaseConfiguration#INDEX_HOSTS}
     * as the server addresses.  Note that this configuration method
     * does not allow creating a Node that talks over the network.
     * <p>
     * This is activated by <b>not</b> setting an explicit value for {@link #INTERFACE} in the
     * Titan configuration.
     *
     * @see #interfaceConfiguration(com.thinkaurelius.titan.diskstorage.configuration.Configuration)
     * @param config a config passed to ElasticSearchIndex's constructor
     * @return a node and client object open and ready for use
     */
private ElasticSearchSetup.Connection legacyConfiguration(Configuration config) {
    Node node;
    Client client;
    if (config.get(LOCAL_MODE)) {
        log.debug("Configuring ES for JVM local transport");
        boolean clientOnly = config.get(CLIENT_ONLY);
        boolean local = config.get(LOCAL_MODE);
        NodeBuilder builder = NodeBuilder.nodeBuilder();
        Preconditions.checkArgument(config.has(INDEX_CONF_FILE) || config.has(INDEX_DIRECTORY), "Must either configure configuration file or base directory");
        if (config.has(INDEX_CONF_FILE)) {
            String configFile = config.get(INDEX_CONF_FILE);
            ImmutableSettings.Builder sb = ImmutableSettings.settingsBuilder();
            log.debug("Configuring ES from YML file [{}]", configFile);
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(configFile);
                sb.loadFromStream(configFile, fis);
                builder.settings(sb.build());
            } catch (FileNotFoundException e) {
                throw new TitanException(e);
            } finally {
                IOUtils.closeQuietly(fis);
            }
        } else {
            String dataDirectory = config.get(INDEX_DIRECTORY);
            log.debug("Configuring ES with data directory [{}]", dataDirectory);
            File f = new File(dataDirectory);
            if (!f.exists())
                f.mkdirs();
            ImmutableSettings.Builder b = ImmutableSettings.settingsBuilder();
            for (String sub : DATA_SUBDIRS) {
                String subdir = dataDirectory + File.separator + sub;
                f = new File(subdir);
                if (!f.exists())
                    f.mkdirs();
                b.put("path." + sub, subdir);
            }
            b.put("script.disable_dynamic", false);
            b.put("indices.ttl.interval", "5s");
            builder.settings(b.build());
            String clustername = config.get(CLUSTER_NAME);
            Preconditions.checkArgument(StringUtils.isNotBlank(clustername), "Invalid cluster name: %s", clustername);
            builder.clusterName(clustername);
        }
        node = builder.client(clientOnly).data(!clientOnly).local(local).node();
        client = node.client();
    } else {
        log.debug("Configuring ES for network transport");
        ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder();
        if (config.has(CLUSTER_NAME)) {
            String clustername = config.get(CLUSTER_NAME);
            Preconditions.checkArgument(StringUtils.isNotBlank(clustername), "Invalid cluster name: %s", clustername);
            settings.put("cluster.name", clustername);
        } else {
            settings.put("client.transport.ignore_cluster_name", true);
        }
        log.debug("Transport sniffing enabled: {}", config.get(CLIENT_SNIFF));
        settings.put("client.transport.sniff", config.get(CLIENT_SNIFF));
        settings.put("script.disable_dynamic", false);
        TransportClient tc = new TransportClient(settings.build());
        int defaultPort = config.has(INDEX_PORT) ? config.get(INDEX_PORT) : HOST_PORT_DEFAULT;
        for (String host : config.get(INDEX_HOSTS)) {
            String[] hostparts = host.split(":");
            String hostname = hostparts[0];
            int hostport = defaultPort;
            if (hostparts.length == 2)
                hostport = Integer.parseInt(hostparts[1]);
            log.info("Configured remote host: {} : {}", hostname, hostport);
            tc.addTransportAddress(new InetSocketTransportAddress(hostname, hostport));
        }
        client = tc;
        node = null;
    }
    return new ElasticSearchSetup.Connection(node, client);
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) Node(org.elasticsearch.node.Node) FileNotFoundException(java.io.FileNotFoundException) NodeBuilder(org.elasticsearch.node.NodeBuilder) ImmutableSettings(org.elasticsearch.common.settings.ImmutableSettings) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) FileInputStream(java.io.FileInputStream) TitanException(com.thinkaurelius.titan.core.TitanException) TransportClient(org.elasticsearch.client.transport.TransportClient) Client(org.elasticsearch.client.Client) File(java.io.File)

Example 4 with NodeBuilder

use of org.elasticsearch.node.NodeBuilder in project play2-elasticsearch by cleverage.

the class IndexClient method start.

public void start() throws Exception {
    // Load Elasticsearch Settings
    Settings.Builder settings = loadSettings();
    // Check Model
    if (this.isLocalMode()) {
        Logger.info("ElasticSearch : Starting in Local Mode");
        NodeBuilder nb = nodeBuilder().settings(settings).local(true).client(false).data(true);
        node = nb.node();
        client = node.client();
        Logger.info("ElasticSearch : Started in Local Mode");
    } else {
        Logger.info("ElasticSearch : Starting in Client Mode");
        TransportClient c = TransportClient.builder().settings(settings).build();
        if (config.client == null) {
            throw new Exception("Configuration required - elasticsearch.client when local model is disabled!");
        }
        String[] hosts = config.client.trim().split(",");
        boolean done = false;
        for (String host : hosts) {
            String[] parts = host.split(":");
            if (parts.length != 2) {
                throw new Exception("Invalid Host: " + host);
            }
            Logger.info("ElasticSearch : Client - Host: " + parts[0] + " Port: " + parts[1]);
            c.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(parts[0]), Integer.valueOf(parts[1])));
            done = true;
        }
        if (!done) {
            throw new Exception("No Hosts Provided for ElasticSearch!");
        }
        client = c;
        Logger.info("ElasticSearch : Started in Client Mode");
    }
    // Check Client
    if (client == null) {
        throw new Exception("ElasticSearch Client cannot be null - please check the configuration provided and the health of your ElasticSearch instances.");
    }
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) NodeBuilder(org.elasticsearch.node.NodeBuilder) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) Settings(org.elasticsearch.common.settings.Settings) SettingsException(org.elasticsearch.common.settings.SettingsException)

Aggregations

NodeBuilder (org.elasticsearch.node.NodeBuilder)4 Node (org.elasticsearch.node.Node)3 TransportClient (org.elasticsearch.client.transport.TransportClient)2 ImmutableSettings (org.elasticsearch.common.settings.ImmutableSettings)2 Settings (org.elasticsearch.common.settings.Settings)2 InetSocketTransportAddress (org.elasticsearch.common.transport.InetSocketTransportAddress)2 TitanException (com.thinkaurelius.titan.core.TitanException)1 BasicConfiguration (com.thinkaurelius.titan.diskstorage.configuration.BasicConfiguration)1 Configuration (com.thinkaurelius.titan.diskstorage.configuration.Configuration)1 ModifiableConfiguration (com.thinkaurelius.titan.diskstorage.configuration.ModifiableConfiguration)1 CommonsConfiguration (com.thinkaurelius.titan.diskstorage.configuration.backend.CommonsConfiguration)1 ElasticSearchIndex (com.thinkaurelius.titan.diskstorage.es.ElasticSearchIndex)1 GraphDatabaseConfiguration (com.thinkaurelius.titan.graphdb.configuration.GraphDatabaseConfiguration)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 BaseConfiguration (org.apache.commons.configuration.BaseConfiguration)1 GetSettingsRequest (org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest)1