//---传参数------------------------------------------------------------
//参数可以用 Values.parameters("aid", 25,"person_name", person, "company_name", company), 也可以是Map
//需要用到参数的地方
// 1) where 语句中的比对条件 = >< != $p
// 2) where id(a) in [1,2,3,4] 如何设置参数?
// 3) 字段 CREATE (:Company {name: $name})
// 4) set b+={....} 如何设置参数?
@Test
public void TestUseProps() {
String cql = "match (a:User)-[r]-(b) where id(a)=$aid return a,b,r";
try (Session session = GraphDb.newSession()) {
List<Record> list = session.run(cql, Values.parameters("aid", 25)).list();
System.out.println(list.size());
}
}
@Test
public void TestUseProps2() {
String cql = "match (a:User)-[r]-(b) where id(a)=$aid return a,b,r";
try (Session session = GraphDb.newSession()) {
Map<String, Object> pp = new HashMap<>();
pp.put("aid", 25);
List<Record> list = session.run(cql, pp).list();
System.out.println(list.size());
}
}
// in
@Test
public void TestUseProps3() {
String cql = "match (a) where id(a) in $ids return a";
try (Session session = GraphDb.newSession()) {
Map<String, Object> pp = new HashMap<>();
pp.put("ids", new int[]{5, 74});
List<Record> list = session.run(cql, pp).list();
System.out.println(list.size());
}
}
/*
2.6.5. Create node with properties
Parameters.
{
"props" : {
"name" : "Andy",
"position" : "Developer"
}
}
Query.
CREATE ($props)
*/
@Test
public void TestUseProps4() {
String cql = "CREATE ($props) ";
try (Session session = GraphDb.newSession()) {
String json="{\n" +
" \"props\" : {\n" +
" \"name\" : \"Andy\",\n" +
" \"position\" : \"Developer\"\n" +
" }\n" +
" }";
Map map = js.toMap(json);
session.run(cql, map).list();
System.out.println("ok");
}
}
@Test // set n={...}
public void TestUseProps5() {
String cql = "UNWIND $props AS properties\n" +
"CREATE (n:Person)\n" +
"SET n = properties\n" +
"RETURN n";
try (Session session = GraphDb.newSession()) {
String json="{\n" +
" \"props\" : {\n" +
" \"name\" : \"Andy1\",\n" +
" \"position\" : \"Developer1\"\n" +
" }\n" +
" }";
Map map = js.toMap(json);
List<Record> list = session.run(cql, map).list();
System.out.println(list.size());
}
}
@Test // set n+={...}
public void TestUseProps6() {
String cql = "UNWIND $props AS properties\n" +
"match (n) where id(n)=25 \n" +
"SET n += properties\n" +
"RETURN n";
try (Session session = GraphDb.newSession()) {
String json="{\n" +
" \"props\" : {\n" +
" \"sex\" : \"男\",\n" +
" \"position\" : \"Developer\"\n" +
" }\n" +
" }";
Map map = js.toMap(json);
List<Record> list = session.run(cql, map).list();
System.out.println(list.size());
}
}
@Test // set n={...} 一次创建多个节点
public void TestUseProps7() {
String cql = "UNWIND $props AS properties\n" +
"CREATE (n:Person)\n" +
"SET n = properties\n" +
"RETURN n";
try (Session session = GraphDb.newSession()) {
String json="{\n" +
" \"props\" : [ {\n" +
" \"awesome\" : true,\n" +
" \"name\" : \"Andy3\",\n" +
" \"position\" : \"Developer\"\n" +
" }, {\n" +
" \"children\" : 3,\n" +
" \"name\" : \"Michael\",\n" +
" \"position\" : \"Developer\"\n" +
" } ]\n" +
"}";
Map map = js.toMap(json);
List<Record> list = session.run(cql, map).list();
System.out.println(list.size());
}
}
/*
2.6.3. Regular expression
Parameters.
{
"regex" : ".*h.*"
}
Query.
MATCH (n:Person)
WHERE n.name =~ $regex
RETURN n.name
*/
@Test // 使用正则表达式
public void TestUseProps8() {
String cql = "MATCH (n:Person)\n" +
" WHERE n.name =~ $regex\n" +
" RETURN n.name";
try (Session session = GraphDb.newSession()) {
String json="{\n" +
" \"regex\" : \".*h.*\"\n" +
" }";
Map map = js.toMap(json);
List<Record> list = session.run(cql, map).list();
System.out.println(list.size());
}
}
/*
2.6.4. Case-sensitive string pattern matching
Parameters.
{
"name" : "Michael"
}
Query.
MATCH (n:Person)
WHERE n.name STARTS WITH $name
RETURN n.name
*/
@Test
public void TestUseProps9() {
String cql = "MATCH (n:Person)\n" +
" WHERE n.name STARTS WITH $name\n" +
" RETURN n.name";
try (Session session = GraphDb.newSession()) {
String json="{\n" +
" \"name\" : \"Michael\"\n" +
" }";
Map map = js.toMap(json);
List<Record> list = session.run(cql, map).list();
System.out.println(list.size());
}
}
js.toMap 是将 json 转成Map的工具类