Introducing Jakarta NoSQL
source link: https://www.tuicool.com/articles/YRbM3qI
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Recently approved as an EE4J project, Jakarta NoSQL is a specification in Jakarta EE to help developers create enterprise-grade applications using Java and NoSQL technologies. JNoSQL is the reference implementation of Jakarta NoSQL, providing a set of APIs and a standard implementation for a series of NoSQL databases, such as Cassandra , MongoDB , Neo4J , CouchDB , and OrientDB , among others.
Jakarta NoSQL consists of Communication Layer (Diana), which brings a set of APIs designed for defining the communication with NoSQL databases. It contains four modules according to each NoSQL database type: Key-Value, Column Family, Document, and Graph; and Mapping Layer (Artemis) which provides a series of APIs to help developers integrate Java applications with NoSQL databases. Mapping Layer is annotation-driven and uses technologies like CDI and Bean Validation, making it simple for developers to use. It is possible to compare Mapping Layer with JPA/Hibernate in the traditional RDBMS world.
(Image taken from github.com/eclipse-ee4j/nosql )
Let's go a little deeper and explore how to communicate with Key-Value, Column Family, Document, and Graph NoSQL databases.
The definition of an Entity is pretty similar to JPA. You basically use @Entity
, @Id
, @Column
and so on:
@Entity public class Person { @Id private long id; @Column private String name; @Column private List <string> phones; } </string>
Repositories looks like Spring Data repositories where you extend a Repository<T, ID>
:
public interface PersonRepository extends Repository <person long=""> { List<Person> findByName(String name); Stream<Person> findByPhones(String phone); } </person>
From now on, we will have to tweak it slightly, since the maven artifacts change according to the type of NoSQL database, as well the setup configuration and how we inject the repository in our services.
Let's compare the differences between Column and Document NoSQL databases:
Column
The following are the maven artifacts:
<dependency> <groupId>org.jnosql.artemis</groupId> <artifactId>artemis-column</artifactId> <version>0.0.9</version> </dependency> <dependency> <groupId>org.jnosql.diana</groupId> <artifactId>cassandra-driver</artifactId> <version>0.0.9</version> </dependency>
The following is a producer example used to set up the ColumnFamilyManager:
@ApplicationScoped public class ColumnFamilyManagerProducer { private static final String KEY_SPACE = "developers"; private ColumnConfiguration<> cassandraConfiguration; private ColumnFamilyManagerFactory managerFactory; @PostConstruct public void init() { cassandraConfiguration = new CassandraConfiguration(); managerFactory = cassandraConfiguration.get(); } @Produces public ColumnFamilyManager getManagerCassandra() { return managerFactory.get(KEY_SPACE); } }
And finally, an example of how to execute some inserts/queries:
Person person = Person.builder() .withPhones(Arrays.asList("234", "432")) .withName("Name") .withId(id) .build(); //using ColumnTemplate ColumnTemplate columnTemplate = container.select(CassandraTemplate.class).get(); Person saved = columnTemplate.insert(PERSON); System.out.println("Person saved" + saved); ColumnQuery query = select().from("Person").where(eq(Column.of("id", 1L))).build(); Optional<Person> person = columnTemplate.singleResult(query); System.out.println("Entity found: " + person); //using PersonRepository PersonRepository repository = container.select(PersonRepository.class).select(ofColumn()).get(); Person saved = repository.save(PERSON); System.out.println("Person saved" + saved); Optional<Person> person = repository.findById(1L); System.out.println("Entity found: " + person);
Document
The following are the maven artifacts:
<dependency> <groupId>org.jnosql.artemis</groupId> <artifactId>artemis-document</artifactId> <version>0.0.9</version> </dependency> <dependency> <groupId>org.jnosql.diana</groupId> <artifactId>mongodb-driver</artifactId> <version>0.0.9</version> </dependency>
The following is a producer example used to set up the the DocumentCollectionManager:
@ApplicationScoped public class DocumentCollectionManagerProducer { private static final String COLLECTION = "developers"; private DocumentConfiguration configuration; private DocumentCollectionManagerFactory managerFactory; @PostConstruct public void init() { configuration = new MongoDBDocumentConfiguration(); Map<String, Object> settings = Collections.singletonMap("mongodb-server-host-1", "localhost:27017"); managerFactory = configuration.get(Settings.of(settings)); } @Produces public DocumentCollectionManager getManager() { return managerFactory.get(COLLECTION); } }
And finally, an example of how to execute some inserts/queries:
Person person = Person.builder() .withPhones(Arrays.asList("234", "432")) .withName("Name") .withId(id) .build(); //using DocumentTemplate DocumentTemplate documentTemplate = container.select(DocumentTemplate.class).get(); Person saved = documentTemplate.insert(person); System.out.println("Person saved" + saved); DocumentQuery query = select().from("Person") .where(eq(Document.of("_id", id))).build(); Optional<Person> personOptional = documentTemplate.singleResult(query); System.out.println("Entity found: " + personOptional); //using PersonRepository PersonRepository repository = container.select(PersonRepository.class) .select(ofDocument()).get(); repository.save(person); List<Person> people = repository.findByName("Name"); System.out.println("Entity found: " + people); repository.findByPhones("234").forEach(System.out::println);
More details on Jakarta NoSQL's reference implementation can be found at the JNoSQL page .
There is also a presentation of JNoSQL on Jakarta One available.
Users who want to contribute can subscribe to the mailing list , or get in touch through Twitter .
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK