Wednesday, 22 September 2021

ArangoDB: Create a collection using Java driver

ArangoDatabase interface provides ‘createCollection’ method to create a collection.

 

Signature

CollectionEntity createCollection(String name) throws ArangoDBException;

 

Follow below step-by-step procedure to create a collection.

 

Step 1: Get ArangoDB instance.

ArangoDB arangoDB = new ArangoDB.Builder().user(USER_NAME).password(PASSWORD).host(HOST, PORT).serializer(new ArangoJack()).build();

 

Step 2: Get the ArangoDatabase instances

ArangoDatabase arangoDatabase = arangoDB.db(databaseName);

 

Step 3: Create a collection using ArangoDatabase instance.

CollectionEntity collectionEntity = arangoDatabase.createCollection(collectionName);

 

Find the below working application.

 

CollectionCreateDemo.java

package com.sample.app;

import com.arangodb.ArangoCollection;
import com.arangodb.ArangoDB;
import com.arangodb.ArangoDatabase;
import com.arangodb.entity.CollectionEntity;
import com.arangodb.entity.CollectionStatus;
import com.arangodb.entity.CollectionType;
import com.arangodb.mapping.ArangoJack;

public class CollectionCreateDemo {
  private static final String USER_NAME = "root";
  private static final String PASSWORD = "tiger";
  private static final String HOST = "127.0.0.1";
  private static final int PORT = 8529;

  public static void main(String args[]) {
    // Get an ArangoDB instance
    ArangoDB arangoDB = new ArangoDB.Builder().user(USER_NAME).password(PASSWORD).host(HOST, PORT)
        .serializer(new ArangoJack()).build();

    String databaseName = "testdb";
    arangoDB.createDatabase(databaseName);

    ArangoDatabase arangoDatabase = arangoDB.db(databaseName);

    String collectionName = "users";

    CollectionEntity collectionEntity = arangoDatabase.createCollection(collectionName);

    String collectionId = collectionEntity.getId();
    Boolean isSystemCollection = collectionEntity.getIsSystem();
    collectionName = collectionEntity.getName();
    CollectionStatus collectionStatus = collectionEntity.getStatus();
    CollectionType collectionType = collectionEntity.getType();

    System.out.println("Collection details : ");
    System.out.println("collectionId : " + collectionId);
    System.out.println("isSystemCollection : " + isSystemCollection);
    System.out.println("collectionName : " + collectionName);
    System.out.println("collectionStatus : " + collectionStatus);
    System.out.println("collectionType : " + collectionType);

    ArangoCollection arangoCollection = arangoDatabase.collection(collectionName);

    // Dropping the collection and database
    arangoCollection.drop();
    arangoDatabase.drop();
  }
}

Output

Collection details : 
collectionId : 50657
isSystemCollection : false
collectionName : users
collectionStatus : LOADED
collectionType : DOCUMENT

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment