Using Java to Operate ArangoDB
To operate ArangoDB using Java, the following steps need to be taken:
1. Add ArangoDB Maven dependency: Add the following dependencies in the pom.xml file:
<dependency>
<groupId>com.arangodb</groupId>
<artifactId>arangodb-java-driver</artifactId>
<version>7.16.0</version>
</dependency>
2. Create ArangoDB connection: Create an ArangoDB connection object through the ArangoDB. Builder class. You need to provide the host name, port, username, and password of the ArangoDB server.
import com.arangodb.ArangoDB;
import com.arangodb.ArangoDBException;
public class Main {
public static void main(String[] args) {
try {
ArangoDB arangoDB = new ArangoDB.Builder()
.host("localhost", 8529)
.user("username")
.password("password")
.build();
//Perform data operations
arangoDB.shutdown();
} catch (ArangoDBException e) {
e.printStackTrace();
}
}
}
3. Insert Data: Use ArangoDB's arangoDB. db (dbName). collection (collectionName). insertDocument() method to insert data.
import com.arangodb.entity.DocumentCreateEntity;
DocumentCreateEntity<MyObject> entity = arangoDB.db("mydb").collection("mycollection").insertDocument(new MyObject("value1", "value2"));
String documentKey = entity.getKey();
4. Modify data: Use ArangoDB's arangoDB. db (dbName). collection (collectionName). updateDocument() method to modify the data. The key and new data of the document need to be passed in.
arangoDB.db("mydb").collection("mycollection").updateDocument(documentKey, new MyObject("new value1", "new value2"));
5. Query data: use arangoDB. db (dbName). query() method of ArangoDB to query data, and AQL Query language can be used.
import com.arangodb.CursorResult;
import com.arangodb.entity.BaseDocument;
import com.arangodb.util.MapBuilder;
String query = "FOR doc IN mycollection FILTER doc.field1 == @value RETURN doc";
Map<String, Object> bindVars = new MapBuilder().put("value", "value1").get();
CursorResult<BaseDocument> result = arangoDB.db("mydb").query(query, bindVars, null, BaseDocument.class);
for (BaseDocument document : result) {
System.out.println(document.getKey());
}
6. Delete data: Use ArangoDB's arangoDB. db (dbName). collection (collectionName). deleteDocument() method to delete data. The Key of the document needs to be passed in.
arangoDB.db("mydb").collection("mycollection").deleteDocument(documentKey);
Note: MyObject in the above example is a custom class, defined according to specific requirements.