Friday, 1 October 2021

ArangoDB: Java: Read a document

ArangoCollection interface provides 'getDocument' method to get a document by key.

 

Signature

<T> T getDocument(String key, Class<T> type) throws ArangoDBException;
<T> T getDocument(String key, Class<T> type, DocumentReadOptions options) throws ArangoDBException;

 

You can provides additional options like waitForSync, overwrite, returnNew, returnOld etc., using DocumentCreateOptions

 

Example

<T> T getDocument(String key, Class<T> type) throws ArangoDBException;

 

Find the below working application.

 

DocumentReadDemo.java

package com.sample.app;

import java.util.Arrays;
import java.util.Collection;

import com.arangodb.ArangoCollection;
import com.arangodb.ArangoDB;
import com.arangodb.ArangoDatabase;
import com.arangodb.entity.BaseDocument;
import com.arangodb.entity.DocumentCreateEntity;
import com.arangodb.mapping.ArangoJack;

public class DocumentReadDemo  {
	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 = "test1";

		arangoDatabase.createCollection(collectionName);

		ArangoCollection collection = arangoDatabase.collection(collectionName);

		BaseDocument empAddress = new BaseDocument();
		empAddress.addAttribute("city", "Bangalore");
		empAddress.addAttribute("country", "India");

		BaseDocument empDocument = new BaseDocument();
		// myObject.setKey("myKey");
		empDocument.addAttribute("firstName", "Krishna");
		empDocument.addAttribute("lastName", "Gurram");
		empDocument.addAttribute("hobbies", Arrays.asList("trekking", "playing cricket"));
		empDocument.addAttribute("address", empAddress);

		DocumentCreateEntity<BaseDocument> docEntity = collection.insertDocument(empDocument);

		String key = docEntity.getKey();
		String id = docEntity.getId();

		System.out.println("key : " + key);
		System.out.println("id : " + id);

		BaseDocument persistedDoc = collection.getDocument(key, BaseDocument.class);
		System.out.println(persistedDoc);

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

 

Output

key : 59019
id : test1/59019
BaseDocument [documentRevision=_cS4h4pe---, documentHandle=test1/59019, documentKey=59019, properties={firstName=Krishna, lastName=Gurram, address={country=India, city=Bangalore}, hobbies=[trekking, playing cricket]}]

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment