下面的操作都基于索引库es,type为product
操作的入口TransportClient
一、构建TransportClient对象
public class ElasticTest {
private TransportClient tc;
private static final String INDEX = "es";
private static final String TYPE = "product";
@Before
public void setUp() throws UnknownHostException {
Settings setting = Settings.builder()
.put("cluster.name",clusterName)//默认为elasticsearch
//使客户端去嗅探整个集群的状态,自动把集群中其它机器的ip地址加到客户端中,
//好处是只需设置一个或多个ip,并且自动发现新加入集群的机器
//需要注意的是ES服务器监听使用内网服务器IP而访问使用外网IP时,自动发现失败,
//此时需要手动添加所有节点
.put("client.transport.snif",true)
.put("analyzer","ik")//使用ik分词 .build(); tc = TransportClient.builder().settings(setting).build(); TransportAddress host01= new InetSocketTransportAddress(InetAddress.getByName("host01"),9300); TransportAddress host02= new InetSocketTransportAddress(InetAddress.getByName("host02"),9300);
TransportAddress host03= new InetSocketTransportAddress(InetAddress.getByName("host03"),9300); tc.addTransportAddresses(host01,host02,host03); }}
二、往es中添加数据的时候,根据数据源类型的不同,可以有以下4中方式:
json、map、对象object、XContentBuilder
@Test
public void testAddIndexByJSON() {
String source = "{\"name\":\"derby\",\"author\":\"法拉狗\",\"lastest_version\":\"2.2.0\"}";
IndexResponse response = tc.prepareIndex(INDEX, TYPE, "1").setSource(source).get();
printResponse(response);
}
@Test
public void testAddIndexByMap() {
Map<String, Object> source = new HashMap<>();
source.put("name", "flume");
source.put("author", "cloudera");
source.put("lastest_version", "1.7.0");
IndexResponse response = tc.prepareIndex(INDEX, TYPE, "2").setSource(source).get();
printResponse(response);
}
@Test
public void testAddIndexByObj_1() {
/**
* 调用setSource(Object ... objs)
* 这些objs是成对儿出现的k-v的键值对儿,不能直接是对象,同时,个数必须偶数
*/
IndexResponse response = tc.prepareIndex(INDEX, TYPE, "3").setSource("name", "kafka",
"author", "apache", "lastest_version", "2.10-0.10.0.1").get();
printResponse(response);
}
@Test
public void testAddIndexByObj_2() throws JsonProcessingException {
/**
* 非得要操作Object的话,需要将Object转化为Map或者json字符串
*/
//自己创建对象,有三个属性,设置setter方法,此处省略该类
BigdataProduct bigdataProduct = new BigdataProduct();
bigdataProduct.setName("aaa");
bigdataProduct.setAuthor("parent");
bigdataProduct.setLastest_version("1.0.final");
ObjectMapper json = new ObjectMapper();
String jsonStr = json.writeValueAsString(bigdataProduct);
IndexResponse response = tc.prepareIndex(INDEX, TYPE, "4").setSource(jsonStr).get();
printResponse(response);
}
@Test
public void testAddIndexByXContentBuilder() throws IOException, IllegalAccessException, InvocationTargetException {
BigdataProduct bigdataProduct = new BigdataProduct();
bigdataProduct.setName("storm");
bigdataProduct.setAuthor("twitter");
bigdataProduct.setLastest_version("1.2.5");
XContentBuilder sourceBuildder = XContentFactory.jsonBuilder();
sourceBuildder.startObject();
/**
* 通过反射的方式给sourceBuildder赋值,动态获取BigdataProduct中的数据
* 通过反射做数据的自动装填
*/
Class<BigdataProduct> clz = BigdataProduct.class;
//1.方式一 获得私有属性, 自动装填
// Field[] fields = clz.getDeclaredFields();
// for(Field field : fields){
// field.setAccessible(true);
// String name = field.getName();
// String value = null;
// if(name.equals("name")){
// value = bigdataProduct.getName();
// }
// if(name.equals("author")){
// value = bigdataProduct.getAuthor();
// }
// if(name.equals("lastest_version")){
// value = bigdataProduct.getLastest_version();
// }
// sourceBuildder.field(name,value);
// }
//方式二:通过共有方法找到属性 自动装填
Method[] methods = clz.getMethods();
for (Method method : methods) {//get 通过反射做数据的自动装填
String methodName = method.getName();
if (methodName.startsWith("get") && !methodName.contains("Class")) {
System.out.println(methodName);
String fieldName = methodName.substring(3).toLowerCase();
String fieldValue = (String) method.invoke(bigdataProduct, null);
sourceBuildder.field(fieldName, fieldValue);
}
}
sourceBuildder.endObject();
IndexResponse response = tc.prepareIndex(INDEX, TYPE, "5").setSource(sourceBuildder).get();
printResponse(response);
}
private void printResponse(IndexResponse response) {
System.out.println("version: " + response.getVersion());
System.out.println("isCreated: " + response.isCreated());
}
三、更新操作
/**
* 如果执行的是局部更新,一定要操作的方法是setDoc而不是setSource
*/
@Test
public void testUpdate() throws Exception {
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject()
.field("name", "hadoop")
.field("author", "Dong Couting")
.endObject();
UpdateResponse response = tc.prepareUpdate(INDEX, TYPE, "5").setDoc(builder).get();
System.out.println("version: " + response.getVersion());
System.out.println("isCreated: " + response.isCreated());
}
//或者用 XContentBuilder
@Test
public void testUpdate() throws Exception {
XContentBuilder source= XContentFactory.jsonBuilder()
.startObject()
.field("name", "hadoop")
.field("author", "CDH")
.field("version", 2.7)
.endObject();
tc.prepareUpdate(index, type, "5").setDoc(source).get();
}
@After
public void cleanUp() {
if(tc != null) {
tc.close();
}
}
四、删除操作
@Test
public void testDelete() {
DeleteResponse response = tc.prepareDelete(index, type, "5").get();
System.out.println("version: " + response.getVersion());
}
五、批量处理bulk操作,以其他类型为例
@Test
public void testBulkInsert() {
String deptDev = "{\"name\":\"研发部\", \"deptNo\" : 1}";
String deptMarket = "{\"name\":\"市场部\", \"deptNo\" : 2}";
String deptOffice = "{\"name\":\"行政部\", \"deptNo\" : 3}";
tc.prepareBulk()
.add(new IndexRequest(INDEX, "dep", "1").source(deptDev))
.add(new IndexRequest(INDEX, "dep", "2").source(deptMarket))
.add(new IndexRequest(INDEX, "dep", "3").source(deptOffice))
.add(new DeleteRequest(INDEX, "dep", "3"))
.get();
}