老师让我们了解janusgraph并且使用java api熟悉增删查改功能.
因为大二毫无基础,第一次用idea第一次接触java,开始的时候连数据库都不了解.搭环境都用了一个月,后面补了一些基础知识后才发现原来搭环境非常简单.用了三个小时重新搭了环境并调用java api
1,看官网下载相应版本(非常重要!!!不要偷懒图方便省事!!!踩了无数坑)
github查看版本网址:
我用的版本janusgraph:0.6.2,cassandra:3.11.10
(这么多包足以看出我踩了多少坑)
2,因为我只是将canssandra作为存储后端,所以图的配置文件选用conf/
3,打开conf/gremlin.server/gremlin-server.yaml,将其中的图配置内容更改为janusgraph-cql.properties
这样就配置好了
4,开启canssandra和janusgraph的server
5,打开IDEA,创建一个maven工程
6,配置依赖,相关版本在github里你安装的janusgraph对应的文档连接,里面有相关配置信息.
我配置的依赖:
7,配置文件: 可直接从下载janusgraph的文件夹里复制过来,也可以自己建一个,内容在上面的文档里都有.
remote-graph.properties.xml
remote-objects.yaml
注意,hosts文件是localhost,改成自己的host就可以了
8.编写测试非常简单的demo
package janusgraph_test; import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; import org.apache.tinkerpop.gremlin.structure.Vertex; import org.janusgraph.core.JanusGraph; import org.janusgraph.core.JanusGraphFactory; import org.janusgraph.core.schema.JanusGraphManagement; public class test2 { public static void main(String[] args) throws Exception { //1,连接图 JanusGraph graph = JanusGraphFactory.open("D:\\application\\janusgraph\\janusgraph-full-0.6.2\\conf\\janusgraph-cql.properties"); //2,获得mgmt,这里没用到,但之后会很有用 JanusGraphManagement mgmt = graph.openManagement(); //3,获得遍历图柄 GraphTraversalSource g = graph.traversal(); //4加入属性为person,key值为name,value为John的顶点 g.addV("person").property("name", "John").next(); //5获取顶点并输出 Vertex vertex = g.V().has("name","John").next(); String name = vertex.value("name"); System.out.println(name); //6释放资源 g.close(); mgmt.commit(); graph.close(); } }
输出结果:说明连上啦
9,在idea里面可以看到cassandra里的键空间,我设置的janusgraph名字为janusgraphWithCassandra,这个在properties.xml文件里更改就可以