How to embed Janus Graph in Java

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.

JanusGraph-article01.png

DB settings and persistent data are saved in directories, but other than that, it is a simple application configuration that is completed independently.

JDK installation and environment variables

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. andHotSpot` 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.

Installation

Download from the following.

Apache NetBeans

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.

Creating a project

When you start NetBeans, the following screen will be displayed.

netbeans_01.jpg

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.

netbeans_02.jpg

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.

netbeans_03.jpg

Add dependency (see Janus Graph)

From the line with Project, Files, and Services in the upper left, select Files to display the tree of files existing in the project.

netbeans_04.jpg

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

Add DB configuration file

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).

netbeans_05.jpg netbeans_06.jpg netbeans_07.jpg netbeans_08.jpg

Write the following three lines in the file.

embedded.properties


gremlin.graph=org.janusgraph.core.JanusGraphFactory
storage.backend=berkeleyje
storage.directory=../db/berkeley

Creating a program

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.

netbeans_09.jpg

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

Note: What to do when an error occurs

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.

netbeans_10.jpg

Reference of class to use

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.

netbeans_11.jpg

in conclusion

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

How to embed Janus Graph in Java
How to learn JAVA in 7 days
How to use classes in Java?
How to name variables in Java
How to concatenate strings in java
How to implement date calculation in Java
How to implement Kalman filter in Java
Multilingual Locale in Java How to use Locale
How to do base conversion in Java
How to implement coding conventions in Java
How to get the date in java
How to display a web page in Java
How to get Class from Element in Java
How to hide null fields in response in Java
[Java] How to substitute Model Mapper in Jackson
How to solve an Expression Problem in Java
How to write Java String # getBytes in Kotlin?
How to embed JavaScript variables in HTML with Thymeleaf
[Java] How to use Map
How to call functions in bulk with Java reflection
How to create a Java environment in just 3 seconds
[Java] How to omit the private constructor in Lombok
How to lower java version
[Java] How to use Map
How to uninstall Java 8 (Mac)
Java --How to make JTable
How to input / output IBM mainframe files in Java?
How to write java comments
How to use java class
[Java] How to use Optional ②
How to create a data URI (base64) in Java
[Java] How to use removeAll ()
[Java] How to display Wingdings
How to use Java Map
How to set Java constants
How to generate / verify ID token in Java Memo
How to convert A to a and a to A using AND and OR in Java
How to convert a file to a byte array in Java
How to Git manage Java EE projects in Eclipse
How to use Java variables
Summary of how to implement default arguments in Java
How to convert Java radix
[Java] How to implement multithreading
[Java] How to use Optional ①
How to put old Java (8 series) in macOS 10.15 Catalina
How to initialize Java array
Notes on how to use regular expressions in Java
How to get the class name / method name running in Java
How to use JSON data in WebSocket communication (Java, JavaScript)
Memo: [Java] How to check groupId etc. described in pom.xml
How to store a string from ArrayList to String in Java (Personal)
How to call and use API in Java (Spring Boot)
How to use Java enums (Enum) in MyBatis Mapper XML
How to display a graph in Ruby on Rails (LazyHighChart)
How to dynamically switch JDK when building Java in Gradle
How to develop and register a Sota app in Java
How to simulate uploading a post-object form to OSS in Java
How to deploy Java application to Alibaba Cloud EDAS in Eclipse
How to derive the last day of the month in Java
Differences in how to handle strings between Java and Perl
How to switch Java in the OpenJDK era on Mac