Java Code Examples for org.elasticsearch.node.Node#client()

The following examples show how to use org.elasticsearch.node.Node#client() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BulkProcessorObjectFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
public EmbeddedElasticsearchServer(String storagePath) {
    this.storagePath = storagePath;
    try {
        tempFile = File.createTempFile("elasticsearch", "test");
        this.storagePath = tempFile.getParent();
        tempFile.deleteOnExit();
    } catch (IOException e) {
        e.printStackTrace();
    }

    Settings.Builder elasticsearchSettings = Settings.builder()
            .put("http.enabled", "false")
            .put("path.data", this.storagePath)
            .put("path.home", System.getProperty("user.dir"))
            .put("transport.type", "local");

    node = new Node(elasticsearchSettings.build());
    client = node.client();
}
 
Example 2
Source File: BulkNodeClient.java    From elasticsearch-helper with Apache License 2.0 6 votes vote down vote up
@Override
protected void createClient(Settings settings) throws IOException {
    if (client != null) {
        logger.warn("client is open, closing...");
        client.threadPool().shutdown();
        logger.warn("client is closed");
        client = null;
    }
    if (settings != null) {
        String version = System.getProperty("os.name")
                + " " + System.getProperty("java.vm.name")
                + " " + System.getProperty("java.vm.vendor")
                + " " + System.getProperty("java.runtime.version")
                + " " + System.getProperty("java.vm.version");
        Settings effectiveSettings = Settings.builder().put(settings)
                .put("node.client", true)
                .put("node.master", false)
                .put("node.data", false).build();
        logger.info("creating node client on {} with effective settings {}",
                version, effectiveSettings.getAsMap());
        Collection<Class<? extends Plugin>> plugins = Collections.<Class<? extends Plugin>>singletonList(HelperPlugin.class);
        Node node = new BulkNode(new Environment(effectiveSettings), plugins);
        node.start();
        this.client = node.client();
    }
}
 
Example 3
Source File: AbstractNodeTest.java    From elasticsearch-gatherer with Apache License 2.0 6 votes vote down vote up
public Node buildNode(String id, Settings settings) {
    String settingsSource = getClass().getName().replace('.', '/') + ".yml";
    Settings finalSettings = settingsBuilder()
            .loadFromClasspath(settingsSource)
            .put(defaultSettings)
            .put(settings)
            .put("name", id)
            .build();
    if (finalSettings.get("gateway.type") == null) {
        finalSettings = settingsBuilder().put(finalSettings).put("gateway.type", "none").build();
    }
    if (finalSettings.get("cluster.routing.schedule") != null) {
        finalSettings = settingsBuilder().put(finalSettings).put("cluster.routing.schedule", "50ms").build();
    }
    Node node = nodeBuilder().settings(finalSettings).build();
    Client client = node.client();
    nodes.put(id, node);
    clients.put(id, client);
    return node;
}
 
Example 4
Source File: ElasticSearchTransportClient.java    From ElasticsearchSink2 with Apache License 2.0 5 votes vote down vote up
private void openLocalDiscoveryClient() {
    logger.info("Using ElasticSearch AutoDiscovery mode");
    Node node = NodeBuilder.nodeBuilder().client(true).local(true).node();
    if (client != null) {
        client.close();
    }
    client = node.client();
}
 
Example 5
Source File: NodeClientFactory.java    From samza with Apache License 2.0 5 votes vote down vote up
@Override
public Client getClient() {
  Settings settings = Settings.settingsBuilder()
      .put(clientSettings)
      .build();

  Node node = NodeBuilder.nodeBuilder()
      .client(true)
      .settings(settings)
      .build();

  return node.client();
}
 
Example 6
Source File: ESFilesystemSupport.java    From attic-polygene-java with Apache License 2.0 5 votes vote down vote up
@Override
protected void activateElasticSearch()
    throws Exception
{
    configuration.refresh();
    ElasticSearchIndexingConfiguration config = configuration.get();

    String clusterName = config.clusterName().get() == null ? DEFAULT_CLUSTER_NAME : config.clusterName().get();
    index = config.index().get() == null ? DEFAULT_INDEX_NAME : config.index().get();
    indexNonAggregatedAssociations = config.indexNonAggregatedAssociations().get();

    Identity identity = hasIdentity.identity().get();
    File homeDir = new File( new File( fileConfig.temporaryDirectory(), identity.toString() ), "home" );
    File logsDir = new File( fileConfig.logDirectory(), identity.toString() );
    File dataDir = new File( fileConfig.dataDirectory(), identity.toString() );
    File confDir = new File( fileConfig.configurationDirectory(), identity.toString() );
    Stream.of( homeDir, logsDir, dataDir, confDir ).forEach( File::mkdirs );
    Settings settings = Settings.builder()
                                .put( "cluster.name", clusterName )
                                .put( "path.home", homeDir.getAbsolutePath() )
                                .put( "path.logs", logsDir.getAbsolutePath() )
                                .put( "path.data", dataDir.getAbsolutePath() )
                                .put( "path.conf", confDir.getAbsolutePath() )
                                .put( "transport.type", "local" )
                                .put( "http.enabled", false )
                                .build();
    node = new Node( settings );
    node.start();
    client = node.client();
}
 
Example 7
Source File: NodeTestUtils.java    From elasticsearch-analysis-baseform with Apache License 2.0 5 votes vote down vote up
private Node buildNode(String id) throws IOException {
    Settings nodeSettings = settingsBuilder()
            .put(getNodeSettings())
            .put("name", id)
            .build();
    logger.info("settings={}", nodeSettings.getAsMap());
    // ES 2.1 renders NodeBuilder as useless
    Node node = new MockNode(nodeSettings, AnalysisBaseformPlugin.class);
    AbstractClient client = (AbstractClient)node.client();
    nodes.put(id, node);
    clients.put(id, client);
    logger.info("clients={}", clients);
    return node;
}
 
Example 8
Source File: NodeTestUtils.java    From elasticsearch-helper with Apache License 2.0 5 votes vote down vote up
private Node buildNode(String id) throws IOException {
    Settings nodeSettings = settingsBuilder()
            .put(getNodeSettings())
            .put("name", id)
            .build();
    logger.info("settings={}", nodeSettings.getAsMap());
    // ES 2.1 renders NodeBuilder as useless
    Node node = new MockNode(nodeSettings, HelperPlugin.class);
    AbstractClient client = (AbstractClient)node.client();
    nodes.put(id, node);
    clients.put(id, client);
    logger.info("clients={}", clients);
    return node;
}
 
Example 9
Source File: NodeTestUtils.java    From elasticsearch-xml with Apache License 2.0 5 votes vote down vote up
private Node buildNode(String id) throws IOException {
    Settings nodeSettings = settingsBuilder()
            .put(getNodeSettings())
            .put("name", id)
            .build();
    logger.info("settings={}", nodeSettings.getAsMap());
    // ES 2.1 renders NodeBuilder as useless
    Node node = new MockNode(nodeSettings, XmlPlugin.class);
    AbstractClient client = (AbstractClient)node.client();
    nodes.put(id, node);
    clients.put(id, client);
    logger.info("clients={}", clients);
    return node;
}
 
Example 10
Source File: NodeClientFactory.java    From trident-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("rawtypes")
public Client makeClient(Map conf) {
    String clusterName = (String) conf.get(CLUSTER_NAME);
    synchronized (MUTEX) {
        LOG.info("Attaching node client to cluster: '{}'", clusterName);
        Node node = NODES.get(clusterName);
        if (node == null) {
            node = nodeBuilder().clusterName(clusterName).client(true).data(false).node();
            NODES.put(clusterName, node);
        }
        return node.client();
    }
}
 
Example 11
Source File: ClientFactory.java    From storm-trident-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public Client makeClient(Map conf) {
    String clusterName  = (String)conf.get(NAME);

    final Node node = NodeBuilder.nodeBuilder().settings(buildSettings(clusterName)).node();
    registerShutdownHook(node);

    return node.client();
}
 
Example 12
Source File: NodeClient.java    From elasticsearch-jest-example with MIT License 5 votes vote down vote up
private static Client getNodeClient(){
    Node node = nodeBuilder().clusterName("elasticsearch")
            .settings(ImmutableSettings.settingsBuilder().put("http.enabled", false))
            .node();
    Client client = node.client();
    return client;
}
 
Example 13
Source File: ESStorageEngine.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("resource")
@Override
public void initialise() throws StorageEngineException {
	if (config.isUseNodeClient()) {
		Node node = new NodeBuilder().client(true).clusterName(config.getClusterName()).build();
		client = node.client();
	} else {
		TransportAddress[] serverAddresses = config.getServers().stream()
				.map(HostAndPort::fromString)
				.map(hp -> new InetSocketTransportAddress( new InetSocketAddress(hp.getHostText(), hp.getPortOrDefault(DEFAULT_PORT))))
				.toArray(size -> new TransportAddress[size]);
		client = TransportClient.builder().build().addTransportAddresses(serverAddresses);
	}
}
 
Example 14
Source File: ElasticSearchTransportClient.java    From ingestion with Apache License 2.0 5 votes vote down vote up
private void openLocalDiscoveryClient() {
  logger.info("Using ElasticSearch AutoDiscovery mode");
  Node node = NodeBuilder.nodeBuilder().client(true).local(true).node();
  if (client != null) {
    client.close();
  }
  client = node.client();
}
 
Example 15
Source File: NodeTestConfig.java    From elastic-crud with Apache License 2.0 4 votes vote down vote up
@Bean(destroyMethod = "close")
@Autowired
Client client(final Node node) {
  return node.client();
}
 
Example 16
Source File: ElasticSearchSingleton.java    From trident-tutorial with Apache License 2.0 4 votes vote down vote up
public static Client getInstance() {
        Node node = nodeBuilder().node();
        Client client = node.client();
        createIndex(INSTANCE);
        return client;
}
 
Example 17
Source File: ElasticSearchService.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
protected Client initializeElasticSearchClient(Node node) {
    return node.client();
}
 
Example 18
Source File: NodeClient.java    From bigdata-tutorial with Apache License 2.0 4 votes vote down vote up
public void startup() {


//		NodeBuilder nodeBuilder = new NodeBuilder();
//		NodeBuilder nodeBuilder = NodeBuilder.nodeBuilder();

		//是否加载配置文件
		//NodeBuilder nodeBuilder = NodeBuilder.nodeBuilder().loadConfigSettings(true);

		//cluster.name  in elasticsearch.yml
		//NodeBuilder nodeBuilder = NodeBuilder.nodeBuilder().clusterName("yourclustername")

		//是否只作为客户端,即不存储索引数据,默认值为false
		NodeBuilder nodeBuilder = NodeBuilder.nodeBuilder().client(true);

		Node node = nodeBuilder.node();
		client = node.client();
	}
 
Example 19
Source File: ElasticSearchService.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
protected Client initializeElasticSearchClient(Node node) {
    return node.client();
}
 
Example 20
Source File: EsProvider.java    From usergrid with Apache License 2.0 2 votes vote down vote up
/**
 * Create a node client
 * @return
 */
public Client createNodeClient() {

    // we will connect to ES on all configured hosts


    final String clusterName = indexFig.getClusterName();
    final String nodeName = indexFig.getNodeName();
    final int port = indexFig.getPort();

    /**
     * Create our hosts
     */
    final StringBuffer hosts = new StringBuffer();

    for ( String host : indexFig.getHosts().split( "," ) ) {
        hosts.append( host ).append( ":" ).append( port ).append( "," );
    }

    //remove the last comma
    hosts.deleteCharAt( hosts.length() - 1 );

    final String hostString = hosts.toString();


    Settings settings = ImmutableSettings.settingsBuilder()

        .put( "cluster.name", clusterName )

        // this assumes that we're using zen for host discovery.  Putting an
        // explicit set of bootstrap hosts ensures we connect to a valid cluster.
        .put( "discovery.zen.ping.unicast.hosts", hostString )
        .put( "discovery.zen.ping.multicast.enabled", "false" ).put( "http.enabled", false )

        .put( "client.transport.ping_timeout", 2000 ) // milliseconds
        .put( "client.transport.nodes_sampler_interval", 100 ).put( "network.tcp.blocking", true )
        .put( "node.client", true ).put( "node.name", nodeName )

        .build();

    if (logger.isTraceEnabled()) {
        logger.trace("Creating ElasticSearch client with settings: {}", settings.getAsMap());
    }

    Node node = NodeBuilder.nodeBuilder().settings( settings ).client( true ).data( false ).node();

    return node.client();
}