An Introduction to RavenDB With Java
This article will look at about how to install RavenDB and how to integrate it with Java using Eclipse JNoSQL.
Join the DZone community and get the full member experience.
Join For FreeRavenDB is a NoSQL document type that is written in C#, and it is easy to install and configure a fully transactional NoSQL. Also, it has a terrific UI, which allows reading performance statistics, live monitoring, deep insight into database internals, and extensive logging and incident reporting. This article will look at about how to install it and how to integrate it with Java using Eclipse JNoSQL.
RavenDB is an intuitive and multiplatform database, thereby, it has support to Windows, Linux, Docker, and even on the Py with several clients language that includes Java. The robust feature at RavenDB is indisputably the UI, which is intuitive and easy to use even to admin operations such as log and set configurations that a Java developer can use online from here.
Installing RavenDB Using Docker
To install RavenDB using docker follows the step below:
- Install docker: https://www.docker.com/
- Run docker command
docker run -d -p 8080:8080 -p 38888:38888 ravendb/ravendb
3. Go to: http://localhost:8080/
4. Accept the terms
5. Follow the RavenDB Setup Wizard (to test, you might choose an unsecured option)
6. Create a database "developers"
The next step is to configure the application to use it.
Setting the Application Dependencies
The next step is to configure a smooth Java SE application with CDI and Eclipse JNoSQL with RavenDB. Internally, the communication — aka Diana — layer does not create its own communication; it uses the RavenDB client and uses the Communication API as an adapter. This demo uses a Maven project that needs to set the dependencies beyond CDI 2.0 implementation. It needs the Eclipse JNosQL dependencies as shown below:
<dependencies>
<dependency>
<groupId>org.jnosql.artemis</groupId>
<artifactId>artemis-document</artifactId>
<version>${jnosql.version}</version>
</dependency>
<dependency>
<groupId>org.jnosql.diana</groupId>
<artifactId>ravendb-driver</artifactId>
<version>${jnosql.version}</version>
</dependency>
</dependencies>
Equally important is to do a DocumentCollectionManager; eligible to CDI setting an instance as producer.
@ApplicationScoped
public class DocumentCollectionManagerProducer {
private static final String COLLECTION = "developers";
private RavenDBDocumentConfiguration configuration;
private DocumentCollectionManagerFactory managerFactory;
@PostConstruct
public void init() {
configuration = new RavenDBDocumentConfiguration();
Settings settings = Settings.builder()
.put("ravendb-server-host-1", "http://localhost:8080")
.build();
managerFactory = configuration.get(settings);
}
@Produces
public DocumentCollectionManager getManager() {
return managerFactory.get(COLLECTION);
}
}
This sample will use a natural model to a developer who is a person and has a job and an address.
@Entity
public class Person {
@Id
private String id;
@Column
private String name;
@Column
private List<String> phones;
@Column
private Integer age;
@Column
private Address address;
@Column
private Job job;
}
@Entity
public class Job {
@Column
private double salary;
@Column
private String ocupation;
}
@Entity
public class Address {
@Column
private String street;
@Column
private String city;
}
The model and the configuration are ready, and it is time to create the class that will run this demo.
public class App {
public static void main(String[] args) {
try (SeContainer container = SeContainerInitializer.newInstance().initialize()) {
Address address = new Address("Av nove de julho", "São Paulo");
Job job = new Job(12.12, "Developer");
Person person = Person.builder().
withPhones(Arrays.asList("234", "432"))
.withName("Ada Lovelace")
.withAddress(address)
.withAge(30)
.withJob(job)
.build();
DocumentTemplate template = container.select(DocumentTemplate.class).get();
Person saved = template.insert(person);
System.out.println("Person saved" + saved);
DocumentQuery query = select().from("Person")
.where("_id").eq(person.getId()).build();
Optional<Person> personOptional = template.singleResult(query);
System.out.println("Entity found: " + personOptional);
}
}
private App() {
}
}
To make that integration easier, there is the repository that handles the implementation of the methods that do exist in the Repository interface and new methods once its method query convention.
public interface PersonRepository extends Repository<Person, Long> {
List<Person> findByName(String name);
}
public class App2 {
public static void main(String[] args) {
try (SeContainer container = SeContainerInitializer.newInstance().initialize()) {
Address address = new Address("10880 Malibu Point", "Malibu");
Job job = new Job(1_000, "Rich");
Person person = Person.builder().
withPhones(Arrays.asList("9999999", "55555"))
.withName("Tony stark")
.withAddress(address)
.withAge(50)
.withJob(job)
.build();
PersonRepository repository = container.select(PersonRepository.class)
.select(DatabaseQualifier.ofDocument()).get();
repository.save(person);
List<Person> people = repository.findByName("Tony stark");
System.out.println("Entity found: " + people);
}
}
private App2() {
}
}
Also, there is the text as String that came from the version 0.0.6. A Java developer can query as text from both Repository and DocumentTemplate.
public interface PersonRepository extends Repository<Person, Long> {
List<Person> findByName(String name);
@Query("select * from Person where age > @age")
List<Person> findByAge(@Param("age") Integer age);
}
public class App3 {
public static void main(String[] args) {
try (SeContainer container = SeContainerInitializer.newInstance().initialize()) {
DocumentTemplate template = container.select(DocumentTemplate.class).get();
PersonRepository repository = container.select(PersonRepository.class)
.select(DatabaseQualifier.ofDocument()).get();
PreparedStatement prepare = template.prepare("select * from Person where name = @name");
prepare.bind("name", "Tony stark");
List<Person> people = prepare.getResultList();
System.out.println("Person from name: " + people);
System.out.println("Person from age: " + repository.findByAge(30));
}
}
}
This post covered how easy it is to use RavenDB; a document NoSQL database that has a fantastic UI.
RavenDB code sample: https://github.com/JNOSQL/artemis-demo/tree/master/artemis-demo-java-se/ravendb
Opinions expressed by DZone contributors are their own.
Comments