1,图数数据库引擎TinkerPop
之前简单测试了下tinkerpop。
http://blog.csdn.net/freewebsys/article/details/46348975
http://blog.csdn.net/freewebsys/article/details/46470651
现在要把数据放到mysql数据库,这样更加接近线上系统使用,TinkerPop不解决数据存储的问题,需要使用扩展放到其他存储上面,通过blueprints 接口存储。
支持MongoDB,hadoop,Titan,Neo4j等存储数据。
同时也支持jpa,我使用的是hibernate实现的jpa。当然支持所有数据库了,这里使用mysql进行存储。
代码和pom 放到github上面了:(1个java类而已)
https://github.com/freewebsys/tinkerpop-demo
2,mysql数据库准备
使用mysql root进行操作:(创建数据库,并创建用户并赋予权限)
CREATE DATABASE tinkerpop DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
GRANT ALL PRIVILEGES ON tinkerpop.* TO tinkerpop@"%" IDENTIFIED BY "tinkerpop";
FLUSH PRIVILEGES;
配置pom.xml
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
tinkerpop-demo
tinkerpop-demo
1.0
wingnest-repo
wingnest repo
http://www.wingnest.com/mvn-repo/
org.apache.tinkerpop
gremlin-core
3.0.0.M9-incubating
org.apache.tinkerpop
gremlin-driver
3.0.0.M9-incubating
com.tinkerpop.blueprints
blueprints-core
2.6.0
com.wingnest.blueprints
blueprints-jpa-graph
2.5.0_01
org.slf4j
slf4j-log4j12
1.7.5
test
org.slf4j
slf4j-simple
1.6.1
test
mysql
mysql-connector-java
5.1.35
org.apache.derby
derby
10.11.1.1
test
org.apache.derby
derbytools
10.11.1.1
test
junit
junit
4.12
org.hibernate
hibernate-core
4.3.10.Final
org.hibernate
hibernate-entitymanager
4.3.10.Final
org.apache.derby
derby
10.11.1.1
src/main/java
src/main/resources
true
org.apache.maven.plugins
maven-compiler-plugin
2.5.1
1.7
1.7
UTF-8
org.apache.maven.plugins
maven-resources-plugin
2.4
java代码:
package com.tinkerpop.demo;
import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Vertex;
import com.wingnest.blueprints.impls.jpa.JpaGraph;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
/**
* 创建数据库:
*
* CREATE DATABASE tinkerpop DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
* GRANT ALL PRIVILEGES ON tinkerpop.* TO tinkerpop@"%" IDENTIFIED BY "tinkerpop";
* FLUSH PRIVILEGES;
*/
public class TinkerPopMysqlDemo {
private JpaGraph jpaGraph = null;
@Before
public void setUp() {
//初始化jpa。保存到数据库中。使用hibernate 自动创建表结构。
//如果要使用mysql,这里修改属性。
Map props = new HashMap();
props.put("javax.persistence.jdbc.driver", "com.mysql.jdbc.Driver");
props.put("javax.persistence.jdbc.url", String.format("jdbc:mysql://xxx.xxx.xxx.xxx:3306/tinkerpop?characterEncoding=UTF-8"));
props.put("javax.persistence.jdbc.user", "tinkerpop");
props.put("javax.persistence.jdbc.password", "tinkerpop");
jpaGraph = new JpaGraph("HibernateUnit", props);
}
@After
public void tearDown() throws Exception {
//关闭
jpaGraph.shutdown();
}
@Test
public void testCreate() {
//创建张三数据
Vertex zhangsan = jpaGraph.addVertex(null);
zhangsan.setProperty("name", "zhangsan");
System.out.println("zhangsan:" + zhangsan.getId());
//创建李四数据
Vertex lisi = jpaGraph.addVertex(null);
lisi.setProperty("name", "lisi");
System.out.println("lisi:" + lisi.getId());
//创建王五数据
Vertex wangwu = jpaGraph.addVertex(null);
wangwu.setProperty("name", "wangwu");
System.out.println("wangwu:" + wangwu.getId());
//设置李四和王五朋友关系,friend是连接的名字,可以随意取。
Edge friend1 = jpaGraph.addEdge(null, zhangsan, lisi, "friend");
//设置王五和李四朋友关系
Edge friend2 = jpaGraph.addEdge(null, wangwu, lisi, "friend");
System.out.println("create finish");
}
@Test
public void testQuery() {
//查询全部数据。
queryAll();
queryZhansanFriends();
System.out.println("query finish");
}
private void queryAll() {
Iterable allVertex = jpaGraph.getVertices();
System.out.println("######################query all######################");
for (Vertex vertex : allVertex) {
System.out.print("name:" + vertex.getProperty("name"));
System.out.println(",id:" + vertex.getId());
}
}
private void queryZhansanFriends() {
Vertex zhangsan = jpaGraph.getVertex(1);
System.out.println("######################query zhangsan friends######################");
Iterable zhansanFriends = zhangsan.getVertices(Direction.OUT, "friend");
for (Vertex vertex : zhansanFriends) {
System.out.print("name:" + vertex.getProperty("name"));
System.out.println(",id:" + vertex.getId());
}
}
@Test
public void testDelete() {
Vertex lisi = jpaGraph.getVertex(2);
jpaGraph.removeVertex(lisi);
//删除之后,查询全部。
queryAll();
queryZhansanFriends();
}
}
3,运行结果
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| tinkerpop |
+--------------------+
2 rows in set (0.01 sec)mysql> use tinkerpop
Database changed
mysql> show tables;
+---------------------+
| Tables_in_tinkerpop |
+---------------------+
| ENTITY |
| INDEX_BASE |
+---------------------+
2 rows in set (0.00 sec)
mysql> select * from ENTITY;
+---------------+----+---------+---------+---------+-----------------+--------+------------+-------------------+-------------------+
| DTYPE | id | REMOVED | VERSION | KEYNAME | valueData | LABEL | element_id | incomingVertex_id | outgoingVertex_id |
+---------------+----+---------+---------+---------+-----------------+--------+------------+-------------------+-------------------+
| BpJpaVertex | 1 | | 0 | NULL | NULL | NULL | NULL | NULL | NULL |
| BpJpaVertex | 2 | | 0 | NULL | NULL | NULL | NULL | NULL | NULL |
| BpJpaVertex | 3 | | 0 | NULL | NULL | NULL | NULL | NULL | NULL |
| BpJpaEdge | 4 | | 1 | NULL | NULL | friend | NULL | 2 | 1 |
| BpJpaEdge | 5 | | 1 | NULL | NULL | friend | NULL | 2 | 3 |
| BpJpaProperty | 6 | | 0 | name |