elasticsearch client java 简单code 应用

本文提供了一个使用Java API连接Elasticsearch集群并创建索引的具体示例。通过TransportClient连接三个不同节点,演示了如何指定索引名称、类型及ID来执行创建操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.kele,xuebi;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;

public class CreateIndex {

	public static void main(String[] args) throws UnknownHostException {
		// TODO Auto-generated method stub
		
		//client 配置参考官方资料链接 
		//https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/transport-client.html
		
		//my-application  本名字跟 /opt/elasticsearch-2.2.0/config/elasticsearch.yml 中配置的集群参数名字相同 cluster.name: my-application
		
		Settings  settings = Settings.settingsBuilder().put("cluster.name", "my-application").build();

		Client client = TransportClient.builder().settings(settings).build()
		.addTransportAddresses(new InetSocketTransportAddress(InetAddress.getByName("bigdataspark"), 9300))
		.addTransportAddresses(new InetSocketTransportAddress(InetAddress.getByName("bigdatacloud"), 9300))
		.addTransportAddresses(new InetSocketTransportAddress(InetAddress.getByName("bigdatastorm"), 9300));
		
		IndexResponse actionGet = client.prepareIndex("shuiguo101", "friut", "1").setSource().execute().actionGet();
		
		System.out.println(actionGet);
		client.close();
			
	}

}

这玩意不简单!!!!!!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值