Friday 26 November 2021

Spring Data: ArangoDB: @ QueryOptions example

 

@QueryOptions annotation is used to set additional query options for custom AQL queries defined on repository methods.

 

Below table summarizes different attributes we can set for @QueryOptions annotation.

 

Attribute

Description

batchSize

Maximum number of result documents to be transferred from the server to the client in one roundtrip.

cache

Flag to determine whether the AQL query cache shall be used.

count

Indicates whether the number of documents in the result set should be returned in the "count" attribute of the result.

fullCount

If set to true and the query contains a LIMIT clause, then the result will have an extra attribute with the sub-attributes stats and fullCount, { ... "extra": { "stats": { "fullCount": 123 } } }. The fullCount attribute will contain the number of documents in the result before the last LIMIT in the query was applied.

maxPlans

Limits the maximum number of plans that are created by the AQL query optimizer.

profile

If set to true, then the additional query profiling information will be returned in the sub-attribute profile of the extra return attribute if the query result is not served from the query cache.

stream

If it is set to true, then the query will be executed in a streaming fashion.

memoryLimit

The maximum number of memory (measured in bytes) that the query is allowed to use.

allowDirtyRead

If it is set to true, then it allows reading from followers in an active-failover setup.

 

For example, below snippet get the documents along with total number of documents matched to given query. 

public interface EmployeeRepository extends ArangoRepository<Employee, String> {

	@Query("FOR e IN employees FILTER e.lastName == @lName SORT e.age ASC RETURN e ")
	@QueryOptions(count = true)
	public ArangoCursor<Employee> getWithLastName(@BindVars Map<String, Object> bindvars);

}

 

Follow below step-by-step procedure to build complete working application.

 

Step 1: Create new maven project 'query-options-demo'.

 

Step 2: Update pom.xml with maven dependencies.

 

pom.xml

 

<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.sample.app</groupId>
	<artifactId>query-options-demo</artifactId>
	<version>1</version>

	<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-parent -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.5</version>
	</parent>


	<dependencies>
		<dependency>
			<groupId>com.arangodb</groupId>
			<artifactId>arangodb-spring-boot-starter</artifactId>
			<version>2.3.3.RELEASE</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

 

Step 3: Define entity class.

 

Employee.java

 

package com.sample.app.entity;

import org.springframework.data.annotation.Id;

import com.arangodb.springframework.annotation.ArangoId;
import com.arangodb.springframework.annotation.Document;

@Document("employees")
public class Employee {

	@Id // db document field: _key
	private String key;

	@ArangoId // db document field: _id
	private String arangoId;

	private Integer id;
	private String firstName;
	private String lastName;
	private Integer age;

	public Employee() {

	}

	public Employee(Integer id, String firstName, String lastName, Integer age) {
		super();
		this.id = id;
		this.firstName = firstName;
		this.lastName = lastName;
		this.age = age;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getArangoId() {
		return arangoId;
	}

	public void setArangoId(String arangoId) {
		this.arangoId = arangoId;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", age=" + age + "]";
	}

}

 

Step 4: Define EmployeeRepository interface.

 

EmployeeRepository.java

package com.sample.app.repository;

import java.util.Map;

import com.arangodb.ArangoCursor;
import com.arangodb.springframework.annotation.BindVars;
import com.arangodb.springframework.annotation.Query;
import com.arangodb.springframework.annotation.QueryOptions;
import com.arangodb.springframework.repository.ArangoRepository;
import com.sample.app.entity.Employee;

public interface EmployeeRepository extends ArangoRepository<Employee, String> {

	@Query("FOR e IN employees FILTER e.lastName == @lName SORT e.age ASC RETURN e ")
	@QueryOptions(count = true)
	public ArangoCursor<Employee> getWithLastName(@BindVars Map<String, Object> bindvars);

}

 

Step 5: Define arango db configuration class.

 

ArangoConfig.java

 

package com.sample.app.config;

import org.springframework.context.annotation.Configuration;

import com.arangodb.ArangoDB;
import com.arangodb.springframework.annotation.EnableArangoRepositories;
import com.arangodb.springframework.config.ArangoConfiguration;

@Configuration
@EnableArangoRepositories(basePackages = { "com.sample.app" })
public class ArangoConfig implements ArangoConfiguration {

	@Override
	public ArangoDB.Builder arango() {
		return new ArangoDB.Builder().host("localhost", 8529).user("root").password("tiger");
	}

	@Override
	public String database() {
		return "abc_org";
	}
}

 

Step 6: Define main application class.

 

App.java

 

package com.sample.app;

import java.util.HashMap;
import java.util.Map;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import com.arangodb.ArangoCursor;
import com.sample.app.entity.Employee;
import com.sample.app.repository.EmployeeRepository;

@SpringBootApplication
public class App {
	public static void main(String args[]) {
		SpringApplication.run(App.class, args);
	}

	@Bean
	public CommandLineRunner demo(EmployeeRepository employeeRepo) {

		return (args) -> {

			Employee emp1 = new Employee(1, "Ram", "Gurram", 31);
			Employee emp2 = new Employee(2, "Rudra", "Gurram", 32);
			Employee emp3 = new Employee(3, "Gopi", "Battu", 31);
			Employee emp4 = new Employee(4, "Joel", "Chelli", 32);
			Employee emp5 = new Employee(5, "Sailu", "Ptr", 31);
			Employee emp6 = new Employee(6, "Lahari", "Gurram", 23);

			emp1 = employeeRepo.save(emp1);
			emp2 = employeeRepo.save(emp2);
			emp3 = employeeRepo.save(emp3);
			emp4 = employeeRepo.save(emp4);
			emp5 = employeeRepo.save(emp5);
			emp6 = employeeRepo.save(emp6);

			Map<String, Object> map = new HashMap<>();
			map.put("lName", "Gurram");

			ArangoCursor<Employee> empsWithLastNameGurram = (ArangoCursor<Employee>) employeeRepo.getWithLastName(map);
			System.out.println("Total employees matched to lastName 'Gurram' : " + empsWithLastNameGurram.getCount());

			while (empsWithLastNameGurram.hasNext()) {
				System.out.println(empsWithLastNameGurram.next());
			}

		};
	}
}

 

Total project structure looks like below.

 


Run App.java, you will see below messages in console.

Total employees matched to lastName 'Gurram' : 3
Employee [id=6, firstName=Lahari, lastName=Gurram, age=23]
Employee [id=1, firstName=Ram, lastName=Gurram, age=31]
Employee [id=2, firstName=Rudra, lastName=Gurram, age=32]

 

You can download complete working application from below github link.

https://github.com/harikrishna553/springboot/tree/master/arangodb/query-options-demo

 

 


 

Previous                                                    Next                                                    Home

No comments:

Post a Comment