java elasticsearch add
2021-06-03 21:03
标签:vat cat tst icc otf parse ace junit address 2.add 测试代码 java elasticsearch add 标签:vat cat tst icc otf parse ace junit address 原文地址:https://www.cnblogs.com/zhanchaohan/p/14668853.html${es.version}版本可以为6.8.*注意es 6.8和7.9有很大变动。7.9.*未做测试
1.建立连接对象import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Properties;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
/**
* @author zhanchaohan
*
*/
public class ElasticConfig {
String ClusterName;
String EsIp;
String EsPort;
public TransportClient init() throws FileNotFoundException, IOException {
Properties properties=new Properties();
properties.load(new FileReader(ElasticConfig.class.getResource("/elasticsearch.properties").getPath()));
Settings settings = Settings.builder().put("cluster.name", properties.getProperty("ClusterName")).build();
try {
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName(properties.getProperty("EsIp")), Integer.parseInt(properties.getProperty("EsPort"))));
return client;
} catch (UnknownHostException e) {
e.printStackTrace();
}
return null;
}
}
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.client.transport.TransportClient;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/***
*
* @author zhanchaohan
*
*/
public class add {
private TransportClient client;
private String EsIndex="etest";
private String EsType="doc";
@Before
public void init () throws FileNotFoundException, IOException {
ElasticConfig ec=new ElasticConfig();
client=ec.init();
}
@After
public void destroy () {
if ( client != null ) {
client.close ();
}
}
// 单条随机ID
@Test
public void add () {
Map
文章标题:java elasticsearch add
文章链接:http://soscw.com/index.php/essay/90133.html