我的代码如下:
Test类:
@Test
public void aa (UserVo userVo) throws IOException {
try {
MongoClient mongo = new MongoClient("47.106.125.227", 27017);//连接mongo
DB db = mongo.getDB("qhc");//获取数据库
DBCollection table = db.getCollection("sjs_list");//获取表名
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http")));//连接es
//找到数据
List<UserVo> userVoList=new ArrayList<>();
userVoList.add(userVo);
//放入数据
batchInsertToEsSync(client,userVoList,"user","_search");//导出到es 表 字段
} catch (Exception e) {
e.printStackTrace();
}
}
batchInsertToEsSync:
public void batchInsertToEsSync(RestHighLevelClient client,List<UserVo> objs,String tableName,String type) throws IOException {//导出
BulkRequest bulkRequest=new BulkRequest();
for(UserVo obj:objs) {
IndexRequest req = new IndexRequest(tableName, type);
Map<String,Object> map=new HashMap<>();
//获取数据
String id = obj.getId();
String title = obj.getTitle();
String content = obj.getContent();
String source = obj.getSource();
String date = obj.getDate();
String pageUrl = obj.getPageUrl();
String areaTag = obj.getAreaTag();
String affairsTag = obj.getAffairsTag();
String contentTag = obj.getContentTag();
List<FuJian> enclosure= obj.getEnclosure();
String img = obj.getImg();
String summary = obj.getSummary();
String tag = obj.getTag();
String labelName = obj.getLabelName();
//添加数据
map.put("_id",id);
map.put("_title",title);
map.put("_content",content);
map.put("_source",source);
map.put("_date",date);
map.put("_pageUrl",pageUrl);
map.put("_areaTag",areaTag);
map.put("_affairsTag",affairsTag);
map.put("_contentTag",contentTag);
map.put("_enclosure",enclosure);
map.put("_img",img);
map.put("_summary",summary);
map.put("_tag",tag);
map.put("_labelName",labelName);
req.id(map.get("_id").toString());
req.source(map, XContentType.JSON);
bulkRequest.add(req);
}
BulkResponse bulkResponse=client.bulk(bulkRequest);
for (BulkItemResponse bulkItemResponse : bulkResponse) {
if (bulkItemResponse.isFailed()) {
System.out.println(bulkItemResponse.getId()+","+bulkItemResponse.getFailureMessage());
}
}
}
pom.xml一部分:
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.2.3</version>
<exclusions>
<exclusion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</exclusion>
</exclusions>
</dependency>
是包冲突还是?我的elasticsearch版本是6.2.3,jdk1.8,有遇到过的朋友加下QQ1479756648 ,万分感谢