程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> 圖數數據庫引擎tinkerpop(3)使用java調用tinkerpop,存儲到mysql數據庫

圖數數據庫引擎tinkerpop(3)使用java調用tinkerpop,存儲到mysql數據庫

編輯:MySQL綜合教程

圖數數據庫引擎tinkerpop(3)使用java調用tinkerpop,存儲到mysql數據庫


1,圖數數據庫引擎TinkerPop

之前簡單測試了下tinkerpop。
http://www.Bkjia.com/database/201506/406914.html
http://www.Bkjia.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是另外的一種選擇。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved