neo4j教程
作者: Tony
neo4j java driver

maven 依赖:


<dependency>
  <groupId>org.neo4j.driver</groupId>
  <artifactId>neo4j-java-driver</artifactId>
  <version>1.4.0</version>
</dependency>


例子:

import org.apache.commons.lang3.StringUtils;
import org.neo4j.driver.v1.AccessMode;
import org.neo4j.driver.v1.AuthTokens;
import org.neo4j.driver.v1.Driver;
import org.neo4j.driver.v1.GraphDatabase;
import org.neo4j.driver.v1.Session;
import org.neo4j.driver.v1.Transaction;
import org.neo4j.driver.v1.TransactionWork;
import org.neo4j.driver.v1.Values;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


    public static void example() {
        final List<String> cypherList = new ArrayList<String>();
        final List<List<Map<String, Object>>> paramList = new ArrayList<List<Map<String, Object>>>();
        cypherList.add("UNWIND {props} as item MERGE (a:Person{userid:item.userid})" +
                " ON CREATE set a.name=item.name ON MATCH set a.name=item.name");

        List<Map<String, Object>> itemList = new ArrayList<Map<String, Object>>();
        Map<String, Object> item = new HashMap<String, Object>();
        item.put("userid", "1");
        item.put("name", "zhangsan");
        itemList.add(item);

        item = new HashMap<String, Object>();
        item.put("userid", "2");
        item.put("name", "lisi");
        itemList.add(item);

        paramList.add(itemList);



        Driver driver = GraphDatabase.driver("bolt://localhost:9002", AuthTokens.basic("neo4j", "neo4j"));
        Session session = driver.session(AccessMode.WRITE);
        session.writeTransaction(new TransactionWork<Integer>() {
            public Integer execute(Transaction tx) {
                Map<String, List<Map<String, Object>>> props = new HashMap<String, List<Map<String, Object>>>();
                for (int i = 0; i < cypherList.size(); i++) {
                    try {
                        props.put("props", paramList.get(i));
                        tx.run(cypherList.get(i), Values.value(props));
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
                return 1;
            }
        });

    }

标签: driver、neo4j、item、import、v1
一个创业中的苦逼程序员
  • 回复
隐藏