JanusGraph provides drivers for various languages such as Python and Javascript, and it is possible to access the Gremlin server from those languages, but I wanted to create an embedded application that incorporates the DB itself. In that case, the choices are effectively limited to Java and Groovy.
This time, I will explain how to create an application that incorporates Janus Graph (with Berkeley DB) in Java.
DB settings and persistent data are saved in directories, but other than that, it is a simple application configuration that is completed independently.
If you just want to run JanusGraph, you can just use JRE, but if you want to develop, you need JDK. The version will be Java 8. It seems that you need to register an account when downloading from Oracle, so this time I chose AdoptOpenJDK.
Go to the URL and select ʻOpenJDK 8 (LTS)in 1. and
HotSpot` in 2. (both are defaults). From the displayed files, download the JDK that matches your OS.
I used Version jdk8u242-b08 (8.0.242.08) for Windows x64.
The installation method is omitted. After installation, register the JDK directory as the environment variable JAVA_HOME. In my case, it was as follows.
JAVA_HOME=c:\Program Files\AdoptOpenJDK\jdk-8.0.242.08-hotspot
Apache NetBeans
The IDE used for development can actually be anything, but here we will explain how to use Apache NetBeans. I also tried Eclipse, but it didn't go smoothly so I gave up.
Download from the following.
I chose 11.3 as the version (because the installer was available for Windows). If the environment variable JAVA_HOME is not set, an error will occur during installation.
All installers proceeded with the standard settings to complete the installation.
When you start NetBeans, the following screen will be displayed.
Select "File"-"New Project ..." from the menu.
Since Java Application
of Java with Maven
is selected in the initial state, just press the "Next>" button.
If it is the first time, a message that Plugin etc. needs to be downloaded will be displayed, so press the "Download and Activate" button to proceed.
Next, set the project name and directory location. Either is appropriate. After confirming all, click the "Finish" button to complete the project creation.
From the line with Project
, Files
, and Services
in the upper left, select Files
to display the tree of files existing in the project.
Double-click pom.xml
to open it. The dependency information of Janus Graph is described here.
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>com.mycompany</groupId>
<artifactId>janusexample</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.janusgraph</groupId>
<artifactId>janusgraph-core</artifactId>
<version>0.5.1</version>
</dependency>
<dependency>
<groupId>org.janusgraph</groupId>
<artifactId>janusgraph-berkeleyje</artifactId>
<version>0.5.1</version>
</dependency>
</dependencies>
</project>
Added between <dependencies>
to </ dependencies>
. As the name implies, janus graph-core
is the core part of Janus Graph. janusgraph-berkeleyje
is a library required to use the built-in Berkeley DB. In addition, add the necessary libraries as appropriate depending on the purpose.
Library name(Artifact ID) | function |
---|---|
janusgraph-all | A collection of all JanusGraph related libraries. If it's troublesome, this one pom.If you write it in xml, you can use it all |
janusgraph-backend-testutils | Backend DB test collection |
janusgraph-berkeleyje | Graph DB implementation with Berkeley DB as backend |
janusgraph-bigtable | Driver for Google Cloud Bigtable |
janusgraph-core | JanusGraph's core libraries |
janusgraph-cql | Graph DB implementation with Apache Cassandra as the back end |
janusgraph-dist | For distribution archive generation(Probably not used by us users) |
janusgraph-doc | document |
janusgraph-driver | Driver to connect to the server |
janusgraph-es | Indexing implementation with ElasticSearch as the back end |
janusgraph-examples | Sample code collection |
janusgraph-hadoop | Graph analysis engine using Apache Hadoop |
janusgraph-hbase-parent | Apache HBase related libraries |
janusgraph-inmemory | In-memory graph DB implementation |
janusgraph-lucene | Indexing implementation using Lucene |
janusgraph-server | JanusGraph Gremlin server implementation |
janusgraph-solr | Indexing implementation using Apache Solr |
janusgraph-test | Test code |
This time I want to include Berkeley DB, so I will add such a configuration file to the project.
Create a conf
directory in the root directory of the file tree and add the file ʻembedded.properties` to it (filename can be anything).
Write the following three lines in the file.
embedded.properties
gremlin.graph=org.janusgraph.core.JanusGraphFactory
storage.backend=berkeleyje
storage.directory=../db/berkeley
Add the source code of the program. Create your own class file in the directory with the same name as the project name created in the deepest part of the src
directory. The name is JanusExample.java
.
The content of the code is as follows at first.
JanusExample.java
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.janusgraph.core.JanusGraphFactory;
public class JanusExample {
public static void main(String args[])
{
Graph graph = JanusGraphFactory.open("conf/embedded.properties");
System.out.println(graph.toString());
graph.close();
}
}
This is a simple sample that just opens and closes the graph. Build is "Run" "Build Project" (F11 key in Windows) from the menu. It can be executed from "Run" and "Run Project" (F6 key on Windows). The first time Maven downloads various things, so it takes a lot of time. When executed, a db
directory will be created and the data will be stored there.
To query, get the Graph Traversal Source. Getting the return value is a statically typed language, so it's quite difficult, but I hope you do it well.
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.janusgraph.core.JanusGraphFactory;
public class JanusExample {
public static void main(String args[])
{
Graph graph = JanusGraphFactory.open("conf/embedded.properties");
GraphTraversalSource g = graph.traversal();
//Empty the graph
g.V().drop().iterate();
//Add one vertex
g.addV("person").property("name", "bob").property("age", 27).iterate();
//Confirm change
g.tx().commit();
//Try issuing a query to retrieve the value
List<Map<Object, Object>> result = g.V().valueMap("name", "age").toList();
//The return value is[{name: ["bob"], age: [27]}]Should be returned
for(Map<Object, Object> vertex : result){
//vertex is{name:["bob"], age: [27]}Should be
ArrayList<String> names = (ArrayList<String>)vertex.get("name"); // ["bob"]Get
ArrayList<Integer> ages = (ArrayList<Integer>)vertex.get("age"); // [27]Get
String name = names.get(0); //"bob"Get
Integer age = ages.get(0); //Get 27
System.out.printf("name: %s, age: %s\n", name, age);
}
//Try issuing a query to retrieve the number of vertices
Long count = g.V().count().next();
System.out.printf("vertex count is %d\n", count);
graph.close();
}
}
The result should look like this:
name: bob, age: 27
vertex count is 1
If an exception occurs during execution, the program will not end. If you try to execute it again as it is, the graph cannot be opened, so you need to terminate the existing program. The x button is displayed at the bottom right of the IDE, so press it to exit.
If you write a class that is not imported in the code, it will be displayed as a red line at the bottom. You can import the class by moving the cursor to this part and pressing ʻAlt + Enter`. To be honest, I can't develop in Java without this function.
If you are not familiar with Java, there are some pains (I myself am an amateur, so it was actually painful), but it is surprisingly easy to incorporate, so I think it is quite easy to use.
You can make it a stand-alone application, or you can convert and send the query results to JSON and operate it as a simple self-made GraphDB server.
Personally, it would be ideal if I could incorporate it in Python like this ...
Recommended Posts