开发者学堂课程【ElasticSearch 入门精讲:bean 方式】学习笔记,与课程紧密连接,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/631/detail/9985
bean 方式
一、知识解读
二、使用 Bean 的方式新增索引
一、知识解读
*/
@Test
pub1ic void testAddIndexByBean throws JsonProcessi ngException {
IndexRequestBuilder indexRequestBuilder = client. prepareIndex("bigdata",
"product", "3");
BigdataProduct bean = new BigdataProduct("HBase", "周润发","2.6.7");
objectMapper mapper = new objectmapper();
byte[] bytes = mapper . writevalueasBytes (bean);
//
System. out. print1n("使用objectMapper实例封装bean,内容是: "+new string (bytes));//
{"name": "SQOOP" , "author":"张学友","last_ _version":"5.6.7"}
IndexResponse indexResponse = indexRequestBuilder. setsource(bgtes). getO;
//下述方式可以,但是不推荐
// IndexResponse i ndexResponse =
indexRequestBuilder. setsource ("name","SQ0OP" , "author","张学友","7ast_ version","5.6.7").getO;
System. out. print1n("增加索引方式之Bean方式,成功否?" + indexResponse. iscreated());
}
二、使用 Bean 的方式新增索引
@Test
publicvoidtestNewAddIndexBeanWay(throws JsonProcessingException {
System. out.printIn("正式开始使用[使用Bean的方式新增索引]了...");
Byte[]beanBytes=new ObjectMapper().writeValueAsBytes(newProduct(name: "Flink", author.“舍我其谁",version: "2.3.8"); .
IndexRestonse response = client.preparelndex(INDEX, TYPE, id: "2").setSource(beanBytes,XConterType.SMILE).get();
System. out.print("待新增的索引信息是: %s, es集群反馈的结果是: %s%n",new String(beanBytes),response.toString();
}