1,图数数据库引擎TinkerPop
本文原文连接: http://blog.csdn.net/freewebsys/article/details/46519901 转载请注明出处!
之前简单测试了下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 [email protected]"%" IDENTIFIED BY "tinkerpop";
FLUSH PRIVILEGES;
配置pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
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">
<modelVersion>4.0.0</modelVersion>
<groupId>tinkerpop-demo</groupId>
<artifactId>tinkerpop-demo</artifactId>
<version>1.0</version>
<!--
https://maven.apache.org/guides/mini/guide-multiple-repositories.html
增加wingnest-repo repo。
-->
<repositories>
<repository>
<id>wingnest-repo</id>
<name>wingnest repo</name>
<url>http://www.wingnest.com/mvn-repo/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.apache.tinkerpop</groupId>
<artifactId>gremlin-core</artifactId>
<version>3.0.0.M9-incubating</version>
</dependency>
<dependency>
<groupId>org.apache.tinkerpop</groupId>
<artifactId>gremlin-driver</artifactId>
<version>3.0.0.M9-incubating</version>
</dependency>
<dependency>
<groupId>com.tinkerpop.blueprints</groupId>
<artifactId>blueprints-core</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>com.wingnest.blueprints</groupId>
<artifactId>blueprints-jpa-graph</artifactId>
<version>2.5.0_01</version>
</dependency>
<!-- add hibernate dependency -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.6.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.11.1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbytools</artifactId>
<version>10.11.1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.10.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.10.Final</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.11.1.1</version>
</dependency>
</dependencies>
<!-- 插件管理 -->
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<!-- 设置路径,读取路径问题. -->
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
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;
/**
* 创建数据库:
* <p/>
* CREATE DATABASE tinkerpop DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
* GRANT ALL PRIVILEGES ON tinkerpop.* TO [email protected]"%" IDENTIFIED BY "tinkerpop";
* FLUSH PRIVILEGES;
*/
public class TinkerPopMysqlDemo {
private JpaGraph jpaGraph = null;
@Before
public void setUp() {
//初始化jpa。保存到数据库中。使用hibernate 自动创建表结构。
//如果要使用mysql,这里修改属性。
Map<String, Object> props = new HashMap<String, Object>();
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<Vertex> 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<Vertex> 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 | t lisi | NULL | 2 | NULL | NULL |
| BpJpaProperty | 7 | | 0 | name | t wangwu | NULL | 3 | NULL | NULL |
| BpJpaProperty | 8 | | 0 | name | tzhangsan | NULL | 1 | NULL | NULL |
+---------------+----+---------+---------+---------+-----------------+--------+------------+-------------------+-------------------+
8 rows in set (0.00 sec)
mysql> select * from INDEX_BASE;
Empty set (0.00 sec)
其中entry数据表结构:其中valueData是blob格式,id是自增的。
数据库分为3类,BpJpaVertex,BpJpaEdge,BpJpaProperty,分别是图对象,图关系,图的属性,但是这个存储到了一张表里面,感觉不科学啊,数据量会比较大呢。
CREATE TABLE `ENTITY` (
`DTYPE` varchar(31) NOT NULL,
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`REMOVED` bit(1) DEFAULT NULL,
`VERSION` bigint(20) DEFAULT NULL,
`KEYNAME` varchar(255) DEFAULT NULL,
`valueData` tinyblob,
`LABEL` varchar(255) DEFAULT NULL,
`element_id` bigint(20) DEFAULT NULL,
`incomingVertex_id` bigint(20) DEFAULT NULL,
`outgoingVertex_id` bigint(20) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `UK_7hyan3asmdvrxrlywl7v0lj6e` (`REMOVED`),
KEY `UK_ejspyse8mcceolr4vps96aohp` (`KEYNAME`),
KEY `FK_wb8dqej10hriw3o6m72yj9p` (`element_id`),
KEY `FK_ay9plo3t1cprfwdwjvp6fdwi6` (`incomingVertex_id`),
KEY `FK_9r4w6h2jlb9a4e9pt3d95girc` (`outgoingVertex_id`),
CONSTRAINT `FK_9r4w6h2jlb9a4e9pt3d95girc` FOREIGN KEY (`outgoingVertex_id`) REFERENCES `ENTITY` (`id`),
CONSTRAINT `FK_ay9plo3t1cprfwdwjvp6fdwi6` FOREIGN KEY (`incomingVertex_id`) REFERENCES `ENTITY` (`id`),
CONSTRAINT `FK_wb8dqej10hriw3o6m72yj9p` FOREIGN KEY (`element_id`) REFERENCES `ENTITY` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8
Blueprints里面persistence.xml 配置文件:(在Blueprints的jar里面)
要修改参数都可以放到map里面。
<persistence-unit name="HibernateUnit" transaction-type="RESOURCE_LOCAL">
<mapping-file>META-INF/BpJpaEntity.normal.xml</mapping-file>
<mapping-file>META-INF/BpJpaIndexBase.normal.xml</mapping-file>
<mapping-file>META-INF/BpJpaElement.nq.normal.xml</mapping-file>
<mapping-file>META-INF/BpJpaElement.hibernate.xml</mapping-file>
<mapping-file>META-INF/BpJpaVertex.nq.normal.xml</mapping-file>
<mapping-file>META-INF/BpJpaVertex.normal.xml</mapping-file>
<mapping-file>META-INF/BpJpaEdge.nq.normal.xml</mapping-file>
<mapping-file>META-INF/BpJpaEdge.normal.xml</mapping-file>
<mapping-file>META-INF/BpJpaProperty.normal.xml</mapping-file>
<mapping-file>META-INF/BpJpaIndex.nq.normal.xml</mapping-file>
<mapping-file>META-INF/BpJpaIndex.hibernate.xml</mapping-file>
<mapping-file>META-INF/BpJpaIndexItem.nq.normal.xml</mapping-file>
<mapping-file>META-INF/BpJpaIndexItem.normal.xml</mapping-file>
<mapping-file>META-INF/BpJpaKeyIndex.nq.normal.xml</mapping-file>
<mapping-file>META-INF/BpJpaKeyIndex.normal.xml</mapping-file>
<mapping-file>META-INF/BpJpaKeyIndexedProperty.nq.normal.xml</mapping-file>
<mapping-file>META-INF/BpJpaKeyIndexedProperty.normal.xml</mapping-file>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<!-- <shared-cache-mode>NONE</shared-cache-mode> -->
<properties>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:derby:db/HibernateUnit;create=true" />
<property name="javax.persistence.jdbc.user" value="admin" />
<property name="javax.persistence.jdbc.password" value="admin" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
4,总结
本文原文连接: http://blog.csdn.net/freewebsys/article/details/46519901 转载请注明出处!
图数据库引擎,已经了解的差不多了,就是将图的数据变成一个更好理解的方式去操作。很直观,很直接,很方便。
默认TinkerPop使用内存存储数据,这个在线上系统肯定不太靠谱,于是研究大家最常用的mysql数据库,基本上可以线上小规模使用了,但是这个项目还在孵化中,稳定性还的检验下。相比neo4j是另外的一种选择。