java调用Neo4j和ES接口的一些使用笔记

场景:从neo4j中获取数据,更新同步到Es中。
1.从neo4j中获取数据。

org.neo4j.driver.v1.Driver driver = GraphDatabase.driver( "bolt://192.168.2.74:7687",AuthTokens.basic( "neo4j", "Neo4j" ));//配置驱动
Session session = driver.session();//建立连接
StatementResult result = session.run( "MATCH (a:"+label+") return a.name as name");//获取结果集

2.用delete-by-query方法删除ES中原有数据

//打开client
        esAPI.OpenClient2();
        TransportClient client=esAPI.get_Client();
        //删除ES原有数据
        //创建查询条件语句
        QueryBuilder queryBuilder1 = QueryBuilders.matchQuery("field", "name");
        QueryBuilder queryBuilder2 = QueryBuilders.matchQuery("type", type);  
        QueryBuilder queryBuilder3 = QueryBuilders.matchQuery("label", label);
        QueryBuilder queryBuilder4 = QueryBuilders.typeQuery(_type);
        //满足查询条件  
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); 
        boolQueryBuilder.must(queryBuilder1);  
        boolQueryBuilder.must(queryBuilder2); 
        boolQueryBuilder.must(queryBuilder3); 
        boolQueryBuilder.must(queryBuilder4); 
        //执行删除操作
        BulkIndexByScrollResponse response=  DeleteByQueryAction.INSTANCE.newRequestBuilder(client).source(_index).refresh(true).filter(boolQueryBuilder).execute().actionGet();
        System.out.println(response.getDeleted());//删除条数

值得说明的是,在ES-5.02中支持delete by query,如果ES2.X之前,还需要特意安装delete-by-query插件,而且删除语句和ES-5.02有很大差别。执行删除时,建议用 refresh(true),否则,ES中数据会隔一段时间才会刷新

3.将数据批量更新到ES中

      //更新数据到ES
        int i=0;
        BulkRequestBuilder bulkRequest = client.prepareBulk();
        while ( result.hasNext() )
        {
            i=i+1;
            Record record = result.next();//读取Neo4j结果集的数据
            String id=UUID.randomUUID().toString();
            Map ret=new HashMap<>();
            ret.put("label", label);
            ret.put("field", "name");
            ret.put("type", "e");
            ret.put("value", record.get("name").asString());
            bulkRequest.add(client.prepareIndex(_index, _type).setId(id).setSource(ret));
            if(i%100000==0) {//每100000条更新一次
                System.out.println(i);
                bulkRequest.execute().actionGet();
                bulkRequest = client.prepareBulk();//置空
            }

        }System.out.println(i);
        bulkRequest.execute().actionGet();

特别说明:参数_index 代表ES中索引,_type代表ES中type

你可能感兴趣的:(常见错误)