2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

How to connect with ArangoDB using java driver?

How to connect with ArangoDB using java driver? What is the best way to connect arangodb latest version.
userimage

If you are using Arangodb latest version like 3.1.X probably you need latest java driver:

Try below methods

<dependency>
    <groupId>com.arangodb</groupId>
    <artifactId>arangodb-java-driver</artifactId>
    <version>4.1.3</version>
</dependency>


and use below code to get the connection:

package com.arangodb.connection;

import java.util.Objects;

import com.arangodb.ArangoDB;
import com.arangodb.ArangoDatabase;

/**
 * Will help to get the arango db connection.
 * 
 * @author Debs 08 Dec 2016
 *
 */
public class ArangoDBConnection {

	private static ArangoDatabase arangoDatabase;

	static {
		createDatabase();
	}

	private static void createDatabase() {
		ArangoDB arangoDB = new ArangoDB.Builder()
				.password("Your password")
				.host("host IP else 127.0.0.1")
				.port(Port number)
				.user("User id").build();
		arangoDatabase = arangoDB.db("database name");
	}

	public static ArangoDatabase getDb() {
		if (Objects.isNull(arangoDatabase)) {
			createDatabase();
			return arangoDatabase;
		} else {
			return arangoDatabase;
		}
	}
}
userimage

Try below code for connection to arangodb:


package com.connection;

import java.util.Objects;

import com.arangodb.ArangoConfigure;
import com.arangodb.ArangoDriver;
import com.arangodb.ArangoHost;

/**
 *
 * @author Moe
 *
 */
public class ArangoDBConnection {

    private static ArangoDriver arangoDriver;

    static {
        arangoDriver = new ArangoDriver(new ArangoDBConnection().arangoConfig());
    }

    public static ArangoDriver getDB() {
        if (Objects.isNull(arangoDriver)) {
            arangoDriver = new ArangoDriver(new ArangoDBConnection().arangoConfig());
            return arangoDriver;
        } else {
            return arangoDriver;
        }
    }

    private ArangoConfigure arangoConfig() {
        ArangoConfigure configure = new ArangoConfigure();
        configure.setArangoHost(new ArangoHost(<IP>, <PORT NUMBRT>));
        configure.setDefaultDatabase(<DATABASE NAME>);
        configure.setPassword(<PASSWORD>);
        configure.setUser(<USERNAME>);
        configure.init();
        return configure;
    }
}

userimage
Add the dependency driver of java:

<dependency>
    <groupId>com.arangodb</groupId>
    <artifactId>arangodb-java-driver</artifactId>
    <version>3.0.0</version>
</dependency>


// Initialize configure
ArangoConfigure configure = new ArangoConfigure();
configure.setArangoHost(new ArangoHost(<IP or host name>, <port number>));
configure.init();

// Create Driver (this instance is thread-safe)
ArangoDriver arangoDriver = new ArangoDriver(configure);

For more configuration set the required configuration and from arangoDriver you will get the connection for doing CRUD operation.
Answer is