赞
踩
CREATE (n:Label {name:"L1", type:"T1"})
MATCH(a:Test) WHERE a.id="1001" RETURN a; //根据节点属性id进行查询
MATCH(a:Test{id:'1001'}) RETURN a;
MATCH(a:Test) WHERE id(a)="1001" RETURN a; //根据neo4j自增id进行查询
CREATE (节点1),(节点2),(节点3),(节点1)-[关系1]->(节点2),(节点2)-[关系2]->(节点3)
根据关联进行数据查询。
MATCH (a)-[:Have]-(b)-[:Have]-(c)-[:Have]-(d) WHERE a.name="G1" RETURN a,b,c,d; MATCH (a)-[*..3]-(d) WHERE a.name="G1" return a; // 查询和 Cloud Atlas 有关的所有人以及关系 MATCH (people:Person)-[relatedTo]-(movie:Movie {title: "Cloud Atlas"}) RETURN people, relatedTo, movie; // 查询 Kevin Bacon 和其他人的二层联系,关联关系不限 MATCH (bacon:Person {name:"Kevin Bacon"})-[*..2]-(hollywood) RETURN DISTINCT hollywood; // 查询 Kevin Bacon 与 Meg Ryan 的最短关系 MATCH p=shortestPath((bacon:Person {name:"Kevin Bacon"})-[*]-(meg:Person {name:"Meg Ryan"})) RETURN p; RETURN nineties.title //查询 Tom Hanks 演过的电影,关系查询 MATCH (tom:Person {name: "Tom Hanks"})-[:ACTED_IN]->(tomHanksMovies:Movie) RETURN tom,tomHanksMovies //查询和TOM有关的合作演员 MATCH p=(tom:Person {name:"Tom Hanks"})-[r:ACTED_IN]->(m:Movie)<-[:ACTED_IN]-(coActors:Person) RETURN p //查询 Kevin Bacon 的四级所有关系 MATCH (bacon:Person {name:"Kevin Bacon"})-[*1..4]-(hollywood) RETURN DISTINCT hollywood //培根,与梅格·瑞恩关系中最短的一条路径 MATCH p=shortestPath((bacon:Person {name:"Kevin Bacon"})-[*]-(meg:Person {name:"Meg Ryan"})) RETURN p //既和汤姆·汉克斯演过戏又和汤姆·克鲁斯演过戏的人 MATCH (tom:Person {name:"Tom Hanks"})-[:ACTED_IN]->(m)<-[:ACTED_IN]-(coActors), (coActors)-[:ACTED_IN]->(m2)<-[:ACTED_IN]-(cruise:Person {name:"Tom Cruise"}) RETURN tom, m, coActors, m2, cruise
MATCH(n:Label) WHERE n.name="N2" SET n.update = "2018-06-26"
MATCH(n) WHERE ID(n)=100 DELETE n
在查询语句前加上
EXPLAIN 或者 PROFILE
<!-- 服务器开发需要的jar包 -->
<dependency>
<groupId>org.neo4j.driver</groupId>
<artifactId>neo4j-java-driver</artifactId>
<version>1.5.0</version>
</dependency>
<!-- 嵌入式开发需要的jar包 -->
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j</artifactId>
<version>3.3.4</version>
</dependency>
public static void main(String[] args) { // TODO Auto-generated method stub Driver driver = GraphDatabase.driver("bolt://127.0.0.1:7687", AuthTokens.basic("neo4j", "123456")); Session session = driver.session(); session.run("CREATE (a:Person {name: {name}, title: {title}})", parameters("name", "gm", "title", "King")); StatementResult result = session.run( "MATCH (a:Person) WHERE a.name = {name} " + "RETURN a.name AS name, a.title AS title", parameters("name", "gm")); while (result.hasNext()) { Record record = (Record) result.next(); System.out.println(record.get("title").asString() + " " + record.get("name").asString()); } session.close(); driver.close(); }
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-neo4j</artifactId>
</dependency>
单机配置
spring:
data:
neo4j:
uri: bolt://localhost:7687
username: neo4j
password: neo4j
集群配置
spring:
data:
neo4j:
# leader:ip1 follower:ip2 ,ip3
uri: bolt://ip1:7687
uris: bolt://ip2:7687,bolt://ip3:7687
username: neo4j
password:neo4j
( 有点类似于Mysql中的table 映射的对象类,mysql中叫做ORM,neo4j中叫做OGM [object graph mapping])
【节点的标签实体】
@NodeEntity(type = "supply") @Builder @Data public class SupplyGraph { @GraphId private Long nodeId; @Id @GeneratedValue @Property(id = "id") private Long id; /** * 供应商名称 */ @Property(name = "name") private String name; }
【关系实体】
# 供应关系(公司节点和供应商节点节点是供应关系) @Data @Builder @RelationshipEntity(type = "supply") public class SupplyRelationship { @Id @GeneratedValue private Long id; private String indexName; //关系的一端节点是 公司节点 @StartNode private CompanyGraph company; //关系的另一端节点是 供应商节点 @EndNode private SupplyGraph supply; //下面是关系的属性 /** * 采购占比 */ @Property(name = "scale") private String scale; /** * 采购金额 */ @Property(name = "amount") private String amount; }
// 节点Repository
public interface SupplyGraphRepository extends Neo4jRepository<SupplyGraph,Long> {
@Query("MATCH (n:supply) RETURN n ")
List<SupplyGraph> getSupplyNodeList();
@Query("create (n:supply{name:{name}}) RETURN n ")
List<SupplyGraph> addSupplyNodeList(@Param("name") String name);
}
// 关系Repository
public interface SupplyRelationshipRepository extends Neo4jRepository<SupplyRelationship, Long> {}
@Configuration
@EnableNeo4jRepositories(basePackages = "io.fredia.femicro.graph.repository")
@EnableTransactionManagement // 激活SDN隐式事务
public class Neo4jConfig {
}
关键字 | 说明 | 关键字 | 说明 | 关键字 | 说明 | 关键字 | 说明 | |||
---|---|---|---|---|---|---|---|---|---|---|
从句 | ||||||||||
CALL | OPTIONAL | FOREACH | UNION | |||||||
CREATE | REMOVE | MATCH | UNWIND | |||||||
DELETE | RETURN | LOAD | WITH | |||||||
DETACH | SET | MERGE | START | |||||||
EXISTS | ||||||||||
子句 | ||||||||||
LIMIT | ORDER | SKIP | WHERE | |||||||
YIELD | ||||||||||
修饰语 | ||||||||||
ASC | ASCENDING | ASSERT | BY | |||||||
CSV | DESC | DESCENDING | ON | |||||||
表达式 | ||||||||||
ALL | CASE | ELSE | END | |||||||
THEN | WHEN | |||||||||
操作符 | ||||||||||
AND | AS | CONTAINS | DISTINCT | 去重 | ||||||
ENDS | IN | IS | NOT | |||||||
OR | STARTS | XOR | ||||||||
Schema | ||||||||||
CONSTRAINT | CREATE | DROP | EXISTS | |||||||
INDEX | NODE | KEY | UNIQUE | |||||||
Hints | ||||||||||
INDEX | JOIN | PERIODIC | COMMIT | |||||||
SCAN | USING | |||||||||
常量 | ||||||||||
false | null | true | ||||||||
备用 | ||||||||||
ADD | DO | FOR | MANDATORY | |||||||
OF | REQUIRE | SCALAR |
函数 | 描述 | 函数 | 描述 | |
---|---|---|---|---|
UPPER | 它用于将所有字母更改为大写字母。 | LOWER | 它用于将所有字母改为小写字母。 | |
SUBSTRING | 它用于获取给定String的子字符串。 | REPLACE | 它用于替换一个字符串的子字符串。 | |
COUNT | 它返回由MATCH命令返回的行数。 | MAX | 它从MATCH命令返回的一组行返回最大值。 | |
MIN | 它返回由MATCH命令返回的一组行的最小值。 | SUM | 它返回由MATCH命令返回的所有行的求和值。 | |
AVG | 它返回由MATCH命令返回的所有行的平均值。 | shortestPath | 查询最短路径 | |
collect | 收集?? |
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。