sqlg 可以让关系型数据库支持Apache TinkerPop,当前支持的数据库有postgresql,hsqldb,h2,mariadb,mysql,mssqlserver
以下是一个简单的使用

环境准备

  • postgresql
version: "3"
services:
  postgres:
    image: postgres:9.6.11
    ports:
    - "5432:5432"
    environment:
    - "POSTGRES_PASSWORD:dalong"

代码

简单测试,了解下实现处理

  • maven pom文件
<?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>com.dalong</groupId>
    <artifactId>tinkerpop-postgresql</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <encoding>UTF-8</encoding>
        <java.version>1.8</java.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.umlg</groupId>
            <artifactId>sqlg-postgres</artifactId>
            <version>2.0.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>
 
 
  • 代码结构
├── README.md
├── docker-compose.yaml
├── pom.xml
├── src
│ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ └── dalong
│ │ │ └── Application.java
│ │ └── resources
│ │ └── datasource.properties
│ └── test
│ └── java
│ └── InsertTest.java
 
  • 测试代码
    InsertTest.java
 
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.T;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.junit.Before;
import org.junit.Test;
import org.umlg.sqlg.structure.SqlgGraph;
import java.time.LocalDate;
import java.util.List;
import static junit.framework.Assert.assertEquals;
public class InsertTest {
    Graph sqlgGraph = null;
    public InsertTest(){
        sqlgGraph = SqlgGraph.open("datasource.properties");
    }
    @Test
    public void useAsPerNormal() {
        Vertex person = this.sqlgGraph.addVertex(T.label, "Person", "name", "John");
        Vertex address = this.sqlgGraph.addVertex(T.label, "Address", "street", "13th");
        person.addEdge("livesAt", address, "since", LocalDate.of(2010, 1, 21));
        this.sqlgGraph.tx().commit();
        List<Vertex> addresses = this.sqlgGraph.traversal().V().hasLabel("Person").out("livesAt").toList();
        assertEquals(3, addresses.size());
    }
    @Test
    public void testElementsInSchema() {
        Vertex john = this.sqlgGraph.addVertex(T.label, "Manager", "name", "john");
        Vertex palace1 = this.sqlgGraph.addVertex(T.label, "continent.House", "name", "palace1");
        Vertex corrola = this.sqlgGraph.addVertex(T.label, "fleet.Car", "model", "corrola");
        palace1.addEdge("managedBy", john);
        corrola.addEdge("owner", john);
        this.sqlgGraph.tx().commit();
        assertEquals(1, this.sqlgGraph.traversal().V().hasLabel("Manager").count().next().intValue());
        assertEquals(0, this.sqlgGraph.traversal().V().hasLabel("House").count().next().intValue());
        assertEquals(1, this.sqlgGraph.traversal().V().hasLabel("continent.House").count().next().intValue());
        assertEquals(0, this.sqlgGraph.traversal().V().hasLabel("Car").count().next().intValue());
        assertEquals(1, this.sqlgGraph.traversal().V().hasLabel("fleet.Car").count().next().intValue());
        assertEquals(1, this.sqlgGraph.traversal().E().hasLabel("managedBy").count().next().intValue());
        assertEquals(1, this.sqlgGraph.traversal().E().hasLabel("owner").count().next().intValue());
    }
}
 
 
  • 启动&&运行
    使用junt 运行测试
  • 效果

  • sql 生成处理
    官方包含了关于sql生成的处理过程
    如下以下处理:
 
@Test
public void showVertexStep() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a1");
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "name", "b1");
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B", "name", "b2");
    Vertex c1 = this.sqlgGraph.addVertex(T.label, "C", "name", "c1");
    Vertex c2 = this.sqlgGraph.addVertex(T.label, "C", "name", "c2");
    a1.addEdge("ab", b1);
    a1.addEdge("ab", b2);
    b1.addEdge("bc", c1);
    b2.addEdge("bc", c2);
    this.sqlgGraph.tx().commit();
    GraphTraversal<Vertex, Vertex> traversal = this.sqlgGraph.traversal().V()
            .hasLabel("A")
            .out()
            .out();
    System.out.println(traversal);
    traversal.hasNext();
    System.out.println(traversal);
    List<Vertex> c = traversal.toList();
    assertEquals(2, c.size()); 

生成的sql

SELECT
    "public"."V_C"."ID" AS "alias1",
    "public"."V_C"."name" AS "alias2"
FROM
    "public"."V_A" INNER JOIN
    "public"."E_ab" ON "public"."V_A"."ID" = "public"."E_ab"."public.A__O" INNER JOIN
    "public"."V_B" ON "public"."E_ab"."public.B__I" = "public"."V_B"."ID" INNER JOIN
    "public"."E_bc" ON "public"."V_B"."ID" = "public"."E_bc"."public.B__O" INNER JOIN
    "public"."V_C" ON "public"."E_bc"."public.C__I" = "public"."V_C"."ID"
 

以上只是部分,实际上sqlg 也做了很多的优化处理

说明

facebook 开源的ent 也是一个类似的,但是ent 从性能以及使用上就很简单了,ent支持代码生成(主要基于模版)
ent 对于图遍历的各种模型的支持还是比较全的,只是当前支持的数据库比较少,mysql,sqlite 以及gremlin,计划
线路有支持postgresql,同时代码中也包含了,部分关于pg 的处理。

参考资料

https://github.com/apache/tinkerpop/
http://sqlg.org/docs/2.0.0-SNAPSHOT/#_introduction
https://github.com/facebookincubator/ent

sqlg rdbms 上实现的Apache TinkerPop的更多相关文章

  1. Windows OS上安装运行Apache Kafka教程

    Windows OS上安装运行Apache Kafka教程 下面是分步指南,教你如何在Windows OS上安装运行Apache Zookeeper和Apache Kafka. 简介 本文讲述了如何在 ...

  2. WEB文件上传之apache common upload使用(一)

    文件上传一个经常用到的功能,它有许多中实现的方案. 页面表单 + RFC1897规范 + http协议上传 页面控件(flash/html5/activeX/applet) + RFC1897规范 + ...

  3. 在Mac上搭建本地Apache服务器一些注意点

    一般在开发ios程序中,我们需要使用到和服务器的交互操作. 一般我们在Mac上使用Apache来搭建服务器.数据库采用MySQL.在Mac中Apache是自带的.所有,我们可以不需要额外的去Apple ...

  4. 转载:centos上yum安装apache+php+mysql等

    1. 更新系统内核到最新. [root@linuxfei ~]#yum -y update 系统更新后,如果yum安装时提示错误信息,请执行以下命令修复. [root@linuxfei ~]#rpm ...

  5. Linux上安装Mysql+Apache+Php

    一.安装Mysql 1.卸载默认的mysql yum -y remove mysql-libs-* Removed:  mysql-libs.x86_64 0:5.1.73-3.el6_5 卸载成功 ...

  6. Web---文件上传-用apache的工具处理、打散目录、简单文件上传进度

    我们需要先准备好2个apache的类: 上一个博客文章只讲了最简单的入门,现在来开始慢慢加深. 先过渡一下:只上传一个file项 index.jsp: <h2>用apache的工具处理文件 ...

  7. windows上不能启动Apache,遇到错误的方法之一

    最近在2008服务器上安装apache,出现了No installed ConfigArgs for the service "Apache2.4"这个错误. 启动不了,重装了一样 ...

  8. (转)文件上传org.apache.tomcat.util.http.fileupload.FileUploadException: Stream closed

    文件上传时,tomcat报错org.springframework.web.multipart.MultipartException: Failed to parse multipart servle ...

  9. 文件上传之Apache commons fileupload使用

    后台servlet代码:         File file1 = null,file2=null;         String description1 = null,description2 = ...

随机推荐

  1. Docker 下的Zookeeper以及.ne core 的分布式锁

    单节点 1.拉取镜像:docker pull zookeeper 2.运行容器 a.我的容器同一放在/root/docker下面,然后创建相应的目录和文件, mkdir zookeeper cd zo ...

  2. react 中 Modal 多次使用且带参数不同实现

    一.举例:对于 echatrs 的柱子分别需要弹窗 带参数 触发弹窗出现事件 showModalhref myChart.on('click', (params) => { switch (pa ...

  3. Visual Studio 技巧

    Visual Studio 技巧 1 常用设置 2 常用快捷键 2.1 系统默认快捷键 2.2 自定义快捷键 3 修复系统错误 1 常用设置 Text Editor -> All Languag ...

  4. C#读写设置修改调整UVC摄像头画面-光圈

    有时,我们需要在C#代码中对摄像头的光圈进行读和写,并立即生效.如何实现呢? 建立基于SharpCamera的项目 首先,请根据之前的一篇博文 点击这里 中的说明,建立基于SharpCamera的摄像 ...

  5. ubuntu Ifconfig只显示一个lo

    第一步启动网卡 是网卡未启动 命令: ifconfig -a,显示所有网络接口的信息. ifconfig显示当前激活的网络接口信息 ifconfig eth0 up 启动网卡 ifconfig -a ...

  6. Java自学-接口与继承 多态

    Java的多态 操作符的多态 +可以作为算数运算,也可以作为字符串连接 类的多态 父类引用指向子类对象 示例 1 : 操作符的多态 同一个操作符在不同情境下,具备不同的作用 如果+号两侧都是整型,那么 ...

  7. 使用MQ消息队列的优缺点

    前言 公司的项目一直都是在使用MQ的,但是由于使用的功能很简单,所以一直都是知其然不知其所以然,作为一个程序猿有必要了解每一个使用的技术,为什么使用它?它的优点是什么?缺点是什么?等等... 使用mq ...

  8. Vue学习之全局和私有组件小结(七)

    一.组件: 组件的出现,就是为了拆分Vue实例的代码量的,能够让我们以不同的组件,来划分不同的功能模块,将来我们需要什么样的功能,就可以去调用相应的组件即可. 二.组件和模块: 1.模块化:是从代码逻 ...

  9. 石油petrolaeum单词petrolaeum原油

    petroleum 1.a flammable liquid ranging in color from clear to very dark brown and black, consisting ...

  10. Java 数组实例——将阿拉伯数字转换为最大写

    题目:将阿拉伯数字转换为最大写,比如1234转换为壹仟贰佰叁拾肆. package my_package; public class Transform { private String[] arr1 ...