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

tinkerpop(2)使用java調用tinkerpop,存儲到derby數據庫

編輯:Oracle教程

tinkerpop(2)使用java調用tinkerpop,存儲到derby數據庫


1,關於tinkerpop

之前體驗了下tinkerpop的console服務。
存儲數據,然後進行查詢數據。
之前寫的文章:
http://www.Bkjia.com/database/201506/406914.html

2,關於blueprints

這裡寫圖片描述
Blueprints是一組針對屬性圖數據模型的接口、實現、測試套件,有些類似於JDBC,不同之處在於Blueprints是針對圖形數據庫的。Blueprints提供了一組通用的接口,允許開發者在他們的圖形數據庫後端即插即用。
這裡寫圖片描述
這個圖表示了blueprints的位置,在最底層,決定數據存儲。

其中blueprints支持的存儲:
TinkerGraph (TinkerGraph)
Neo4j Implementation (Neo4jGraph)
Neo4jHA Implementation (Neo4jHaGraph)
Neo4jBatch Implementation (Neo4jBatchGraph)
Sail Implementation (SailGraph)
Sparksee Implementation (SparkseeGraph)
Rexster Implementation (RexsterGraph)
Accumulo Implementation (AccumuloGraph – 3rd party – implements Blueprints 2.4.0)
ArangoDB Implementation (ArangoDBGraph – 3rd party – implements Blueprints 2.3.0)
Bitsy (BitsyGraph – 3rd party – implements Blueprints 2.4.0)
Bigdata (BigdataGraph – 3rd party – implements Blueprints 2.5.0)
FluxGraph - Datomic Implementation (FluxGraph – 3rd party – implements Blueprints 2.1.0)
FoundationDB Implementation (FoundationDBGraph – 3rd party – implements Blueprints 2.4.0)
InfiniteGraph Implementation (IGGraph – 3rd party – implements Blueprints 2.1.0)
JPA Implementation (JpaGraph – 3rd party – implements Blueprints 2.5.0)
MongoDB Implementation (MongoDBGraph – 3rd party – implements Blueprints 2.3.0)
Oracle NoSQL Implementation (KVGraph – 3rd party – implements Blueprints 2.1.0)
OrientDB Implementation (OrientGraph – 3rd party – implements Blueprints 2.4.0)
SQL/JDBC Implementation – 3rd party – implements Blueprints 2.4.0)
Titan Implementation (TitanGraph – 3rd party – implements Blueprints 2.3.0)

3,配置依賴包

maven依賴:


    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

            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

測試代碼:

package com.tinkerpop.demo;

import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Graph;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.tg.TinkerGraph;
import com.wingnest.blueprints.impls.jpa.JpaGraph;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 *
 */

public class TinkerPopDemo {

    private JpaGraph jpaGraph = null;

    @Before
    public void setUp() {
        //初始化jpa。保存到數據庫中。使用hibernate 自動創建表結構。
        //如果要使用mysql,這裡修改屬性。
        Map props = new HashMap();
        props.put("javax.persistence.jdbc.url", String.format("jdbc:derby:db/HibernateUnit_test_perf;create=true"));
        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 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 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();
    }

}

運行結果:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Jun 12, 2015 5:19:11 PM org.hibernate.ejb.HibernatePersistence logDeprecation
WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.
Jun 12, 2015 5:19:11 PM org.hibernate.ejb.HibernatePersistence logDeprecation
WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.
Jun 12, 2015 5:19:11 PM org.hibernate.ejb.HibernatePersistence logDeprecation
WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.
Jun 12, 2015 5:19:11 PM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation
INFO: HHH000204: Processing PersistenceUnitInfo [
    name: HibernateUnit
    ...]
Jun 12, 2015 5:19:11 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.10.Final}
Jun 12, 2015 5:19:11 PM org.hibernate.cfg.Environment 
INFO: HHH000206: hibernate.properties not found
Jun 12, 2015 5:19:11 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Jun 12, 2015 5:19:12 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager 
INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
Jun 12, 2015 5:19:13 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Jun 12, 2015 5:19:13 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [org.apache.derby.jdbc.EmbeddedDriver] at URL [jdbc:derby:db/HibernateUnit_test_perf;create=true]
Jun 12, 2015 5:19:13 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=admin, password=****}
Jun 12, 2015 5:19:13 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
Jun 12, 2015 5:19:13 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Jun 12, 2015 5:19:14 PM org.hibernate.dialect.Dialect 
INFO: HHH000400: Using dialect: org.hibernate.dialect.DerbyTenSevenDialect
Jun 12, 2015 5:19:14 PM org.hibernate.cfg.annotations.reflection.JPAOverriddenAnnotationReader checkForOrphanProperties
WARN: HHH000207: Property com.wingnest.blueprints.impls.jpa.internal.models.BpJpaElement.propMap not found in class but described in  (possible typo error)
Jun 12, 2015 5:19:14 PM org.hibernate.cfg.annotations.reflection.JPAOverriddenAnnotationReader checkForOrphanProperties
WARN: HHH000207: Property com.wingnest.blueprints.impls.jpa.internal.models.index.BpJpaIndexItem.indexName not found in class but described in  (possible typo error)
Jun 12, 2015 5:19:14 PM org.hibernate.cfg.annotations.reflection.JPAOverriddenAnnotationReader checkForOrphanProperties
WARN: HHH000207: Property com.wingnest.blueprints.impls.jpa.internal.models.index.BpJpaKeyIndex.keyIndexedItems not found in class but described in  (possible typo error)
Jun 12, 2015 5:19:14 PM org.hibernate.cfg.annotations.reflection.JPAOverriddenAnnotationReader checkForOrphanProperties
WARN: HHH000207: Property com.wingnest.blueprints.impls.jpa.internal.models.index.BpJpaIndex.indexClassName not found in class but described in  (possible typo error)
Jun 12, 2015 5:19:14 PM org.hibernate.cfg.annotations.reflection.JPAOverriddenAnnotationReader checkForOrphanProperties
WARN: HHH000207: Property com.wingnest.blueprints.impls.jpa.internal.models.index.BpJpaIndex.indexItems not found in class but described in  (possible typo error)
Jun 12, 2015 5:19:14 PM org.hibernate.cfg.annotations.reflection.JPAOverriddenAnnotationReader checkForOrphanProperties
WARN: HHH000207: Property com.wingnest.blueprints.impls.jpa.internal.models.index.BpJpaIndexBase.version not found in class but described in  (possible typo error)
Jun 12, 2015 5:19:14 PM org.hibernate.cfg.annotations.reflection.JPAOverriddenAnnotationReader checkForOrphanProperties
WARN: HHH000207: Property com.wingnest.blueprints.impls.jpa.internal.models.BpJpaProperty.keyName not found in class but described in  (possible typo error)
Jun 12, 2015 5:19:14 PM org.hibernate.cfg.annotations.reflection.JPAOverriddenAnnotationReader checkForOrphanProperties
WARN: HHH000207: Property com.wingnest.blueprints.impls.jpa.internal.models.index.BpJpaKeyIndexedProperty.propertyId not found in class but described in  (possible typo error)
Jun 12, 2015 5:19:14 PM org.hibernate.cfg.annotations.reflection.JPAOverriddenAnnotationReader checkForOrphanProperties
WARN: HHH000207: Property com.wingnest.blueprints.impls.jpa.internal.models.index.BpJpaKeyIndexedProperty.keyName not found in class but described in  (possible typo error)
Jun 12, 2015 5:19:14 PM org.hibernate.cfg.annotations.reflection.JPAOverriddenAnnotationReader checkForOrphanProperties
WARN: HHH000207: Property com.wingnest.blueprints.impls.jpa.internal.models.index.BpJpaKeyIndexedProperty.elementType not found in class but described in  (possible typo error)
Jun 12, 2015 5:19:14 PM org.hibernate.cfg.annotations.reflection.JPAOverriddenAnnotationReader checkForOrphanProperties
WARN: HHH000207: Property com.wingnest.blueprints.impls.jpa.internal.models.index.BpJpaKeyIndexedProperty.elementId not found in class but described in  (possible typo error)
Jun 12, 2015 5:19:14 PM org.hibernate.cfg.annotations.reflection.JPAOverriddenAnnotationReader checkForOrphanProperties
WARN: HHH000207: Property com.wingnest.blueprints.impls.jpa.internal.models.index.BpJpaKeyIndexedProperty.bpJpaKeyIndex not found in class but described in  (possible typo error)
Jun 12, 2015 5:19:14 PM org.hibernate.cfg.AnnotationBinder bindClass
WARN: HHH000139: Illegal use of @Table in a subclass of a SINGLE_TABLE hierarchy: com.wingnest.blueprints.impls.jpa.internal.models.BpJpaElement
Jun 12, 2015 5:19:14 PM org.hibernate.cfg.AnnotationBinder bindClass
WARN: HHH000139: Illegal use of @Table in a subclass of a SINGLE_TABLE hierarchy: com.wingnest.blueprints.impls.jpa.internal.models.index.BpJpaIndexItem
Jun 12, 2015 5:19:14 PM org.hibernate.cfg.AnnotationBinder bindClass
WARN: HHH000139: Illegal use of @Table in a subclass of a SINGLE_TABLE hierarchy: com.wingnest.blueprints.impls.jpa.internal.models.BpJpaVertex
Jun 12, 2015 5:19:14 PM org.hibernate.cfg.AnnotationBinder bindClass
WARN: HHH000139: Illegal use of @Table in a subclass of a SINGLE_TABLE hierarchy: com.wingnest.blueprints.impls.jpa.internal.models.BpJpaEdge
Jun 12, 2015 5:19:14 PM org.hibernate.cfg.AnnotationBinder bindClass
WARN: HHH000139: Illegal use of @Table in a subclass of a SINGLE_TABLE hierarchy: com.wingnest.blueprints.impls.jpa.internal.models.index.BpJpaKeyIndex
Jun 12, 2015 5:19:14 PM org.hibernate.cfg.AnnotationBinder bindClass
WARN: HHH000139: Illegal use of @Table in a subclass of a SINGLE_TABLE hierarchy: com.wingnest.blueprints.impls.jpa.internal.models.index.BpJpaIndex
Jun 12, 2015 5:19:14 PM org.hibernate.cfg.AnnotationBinder bindClass
WARN: HHH000139: Illegal use of @Table in a subclass of a SINGLE_TABLE hierarchy: com.wingnest.blueprints.impls.jpa.internal.models.BpJpaProperty
Jun 12, 2015 5:19:14 PM org.hibernate.cfg.AnnotationBinder bindClass
WARN: HHH000139: Illegal use of @Table in a subclass of a SINGLE_TABLE hierarchy: com.wingnest.blueprints.impls.jpa.internal.models.index.BpJpaKeyIndexedProperty
Jun 12, 2015 5:19:14 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory 
INFO: HHH000397: Using ASTQueryTranslatorFactory
Jun 12, 2015 5:19:14 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000228: Running hbm2ddl schema update
Jun 12, 2015 5:19:14 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000102: Fetching database metadata
Jun 12, 2015 5:19:14 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000396: Updating schema
Jun 12, 2015 5:19:15 PM org.hibernate.tool.hbm2ddl.TableMetadata 
INFO: HHH000261: Table found: .ADMIN.ENTITY
Jun 12, 2015 5:19:15 PM org.hibernate.tool.hbm2ddl.TableMetadata 
INFO: HHH000037: Columns: [element_id, id, valuedata, incomingvertex_id, dtype, removed, label, keyname, outgoingvertex_id, version]
Jun 12, 2015 5:19:15 PM org.hibernate.tool.hbm2ddl.TableMetadata 
INFO: HHH000108: Foreign keys: [fk_9r4w6h2jlb9a4e9pt3d95girc, fk_wb8dqej10hriw3o6m72yj9p, fk_ay9plo3t1cprfwdwjvp6fdwi6]
Jun 12, 2015 5:19:15 PM org.hibernate.tool.hbm2ddl.TableMetadata 
INFO: HHH000126: Indexes: [uk_7hyan3asmdvrxrlywl7v0lj6e, sql150612170336740, uk_ejspyse8mcceolr4vps96aohp, sql150612170337000, sql150612170337030, sql150612170336940]
Jun 12, 2015 5:19:15 PM org.hibernate.tool.hbm2ddl.TableMetadata 
INFO: HHH000261: Table found: .ADMIN.INDEX_BASE
Jun 12, 2015 5:19:15 PM org.hibernate.tool.hbm2ddl.TableMetadata 
INFO: HHH000037: Columns: [id, bpjpakeyindex_id, indexclassname, elementtype, dtype, removed, elementid, keyname, indexvalue, bpjpaindex_id, propertyid, indexname, version]
Jun 12, 2015 5:19:15 PM org.hibernate.tool.hbm2ddl.TableMetadata 
INFO: HHH000108: Foreign keys: [fk_7ipiov9b2bb97hkncnl76mkkm, fk_olnpc5iekqlpsk6n61ucruk96]
Jun 12, 2015 5:19:15 PM org.hibernate.tool.hbm2ddl.TableMetadata 
INFO: HHH000126: Indexes: [sql150612170337100, uk_o7ehuddxk2u2rgtqv9c8kbj75, uk_fd9ey72b1ypwplyllo410siv1, sql150612170337080, sql150612170336780, uk_l9wg7u7fi95namecr9p3apnn5, uk_tidr0ggw8dtylnj1ldr1oy4gb, uk_bke0r11vg7q6vtfud90jnfx4, uk_2u6spckbs5deg5048qfvxg5ca]
Jun 12, 2015 5:19:15 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete
Jun 12, 2015 5:19:15 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper$StandardWarningHandler logWarning
WARN: SQL Warning Code: 10000, SQLState: 01J01
Jun 12, 2015 5:19:15 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper$StandardWarningHandler logWarning
WARN: Database 'db/HibernateUnit_test_perf' not created, connection made to existing database instead.
zhangsan:1
lisi:2
wangwu:3
create finish
Jun 12, 2015 5:19:16 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH000030: Cleaning up connection pool [jdbc:derby:db/HibernateUnit_test_perf;create=true]

Process finished with exit code 0

對數據進行CRUD沒有問題。

4,總結

代碼就一個pom和java類,非常簡單的就把圖的數據存儲到了數據庫中。
存儲到mysql裡面類似,需要修改下jdbc連接即可。
如果作為業務服務,現在的代碼再加點就基本滿足了。
雖然數據庫存儲的比neo4j,solr等稍微慢點,但是運維成本低,數據庫可以做主從復制,保護數據,同時多個服務訪問一個數據庫也可以解決單點服務問題。
數據庫的優化非常多,非常方便,同時索引速度也挺快的。這個存儲的是關系,一般情況先數據不太大,再使用mycat進行分庫分表就可以擴展了。

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