文档库 最新最全的文档下载
当前位置:文档库 › neo4j批量创建关系

neo4j批量创建关系

neo4j批量创建关系
neo4j批量创建关系

package neo4j215;

/*neo4j 2.1.5

*neo4j-rest-graphdb-2.0.1.jar

* jersey-client-1.9.jar

*根据节点创建关系,同时如果节点不存在,把节点也创建上;

* 在对关系的操作上,将call-len这个字段实现叠加

* */

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import org.neo4j.rest.graphdb.RestAPI;

import org.neo4j.rest.graphdb.RestAPIFacade;

import org.neo4j.rest.graphdb.batch.BatchCallback;

public class createRelation {

static class Process implements BatchCallback {

private static final String QUERY = " merge (n:Person {call:{call_src}}) on create set n.call={call_src} merge(m:Person {call:{call_dest}}) on create set m.call={call_dest} merge (n)-[r:phone]->(m) on match set r.call_len=r.call_len+{call_length} on create set r.call_len={call_length},r.p1={rp1},r.p2={rp2},r.p3={rp3},r.p4={rp4},r.p5={rp5};";

private List> params;

Process(final List> params) {

this.params = params;

}

@Override

public Object recordBatch(final RestAPI restApi) {

for (final Map param : params) {

restApi.query(QUERY, param);

}

return null;

}

}

public static String getCurDateTime(String ptn) {

Date d = new Date();

SimpleDateFormat sdf = new SimpleDateFormat(ptn);

return sdf.format(d);

}

public static void main(String[] args ){

//System.setProperty("org.neo4j.rest.batch_transaction", "true");

final RestAPI restAPI = new RestAPIFacade("http://localhost:7474/db/data");

int counter = 0;

List> statements = new ArrayList<>();

File csv = new File("/home/neo4j/testdata/relationaa");

BufferedReader br= null;

long begintime = System.currentTimeMillis();

System.out.print("开始时间:" + getCurDateTime("yyyy-MM-dd hh:mm:ss")+"\n");

try{

br = new BufferedReader(new FileReader(csv));

String line = "";

//2199660,19713972,WCCGN,65,rdbnhndjjj,VFCQN,YHCRP,QDHIH,QGMOW

while ((line = br.readLine()) != null && line.length()!=0) {

String mapresult[] = line.split(",", -1);

Map properties= new HashMap<>();

properties.put("call_src", Long.parseLong(mapresult[0]));

properties.put("call_dest", mapresult[1]);

properties.put("address", mapresult[2]);

properties.put("call_length", Integer.parseInt(mapresult[3]));

properties.put("rp1", mapresult[4]);

properties.put("rp2", mapresult[5]);

properties.put("rp3", mapresult[6]);

properties.put("rp4", mapresult[7]);

properties.put("rp5", mapresult[8]);

statements.add(properties) ;

if (++counter % 2000 == 0) {

restAPI.executeBatch(new Process(statements));

statements = new ArrayList<>();

}

}

restAPI.executeBatch(new Process(statements));

System.out.print("总记录条数:" + counter+"\n");

System.out.print("完成时间:" + getCurDateTime("yyyy-MM-dd hh:mm:ss")+"\n");

System.out.println(" 用时(秒):"+(System.currentTimeMillis() - begintime)/1000+"\n");

} catch (Exception e){e.printStackTrace();} finally {try {

br.close();

} catch (Exception e) {

System.out.println("BR Close Exception");

}

}

}

}

相关文档