首页 > 数据库 > MySQL >

图数数据库引擎tinkerpop(3)使用java调用tinkerpop,存储到mysql数据库

2015-06-17

1,图数数据库引擎TinkerPop 本文原文连接: http: blog csdn net freewebsys article details 46519901 转载请注明出处! 之前简单测试了下tinkerpop。 http: blog csdn net freewebsys article details

之前简单测试了下tinkerpop。
http://www.2cto.com/database/201506/406914.html
http://www.2cto.com/database/201506/407588.html
现在要把数据放到mysql数据库,这样更加接近线上系统使用,TinkerPop不解决数据存储的问题,需要使用扩展放到其他存储上面,通过blueprints 接口存储。
支持MongoDB,hadoop,Titan,Neo4j等存储数据。
同时也支持jpa,我使用的是hibernate实现的jpa。当然支持所有数据库了,这里使用mysql进行存储。
代码和pom 放到github上面了:(1个java类而已)

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



    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;

/**
 * 创建数据库:
 * <p/>
 * 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<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();
    }

}
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 tinkerpop@"%" 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里面。


        META-INF/BpJpaEntity.normal.xml
        META-INF/BpJpaIndexBase.normal.xml
        META-INF/BpJpaElement.nq.normal.xml        
        META-INF/BpJpaElement.hibernate.xml            
        META-INF/BpJpaVertex.nq.normal.xml
        META-INF/BpJpaVertex.normal.xml
        META-INF/BpJpaEdge.nq.normal.xml
        META-INF/BpJpaEdge.normal.xml
        META-INF/BpJpaProperty.normal.xml
        META-INF/BpJpaIndex.nq.normal.xml                      
        META-INF/BpJpaIndex.hibernate.xml
        META-INF/BpJpaIndexItem.nq.normal.xml      
        META-INF/BpJpaIndexItem.normal.xml
        META-INF/BpJpaKeyIndex.nq.normal.xml
        META-INF/BpJpaKeyIndex.normal.xml
        META-INF/BpJpaKeyIndexedProperty.nq.normal.xml
        META-INF/BpJpaKeyIndexedProperty.normal.xml

        true
        

4,总结

图数据库引擎,已经了解的差不多了,就是将图的数据变成一个更好理解的方式去操作。很直观,很直接,很方便。
默认TinkerPop使用内存存储数据,这个在线上系统肯定不太靠谱,于是研究大家最常用的mysql数据库,基本上可以线上小规模使用了,但是这个项目还在孵化中,稳定性还的检验下。相比neo4j是另外的一种选择。

相关文章
最新文章
热点推荐