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

  1. 云栖社区>
  2. 博客>
  3. 正文

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

freegolang 2015-06-16 16:34:00 浏览950
展开阅读全文

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 tinkerpop@"%" 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 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    | 

网友评论

登录后评论
0/500
评论
freegolang
+ 关注