Merge pull request #4759 from eelhazati/master

jnosql
This commit is contained in:
Eric Martin 2018-07-20 09:45:24 -05:00 committed by GitHub
commit 94eff53eee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 800 additions and 0 deletions

View File

@ -0,0 +1,88 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>com.baeldung.jnosql</groupId>
<artifactId>jnosql</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>jnosql-artemis</artifactId>
<packaging>war</packaging>
<properties>
<liberty-maven-plugin.version>2.4.2</liberty-maven-plugin.version>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
<build>
<finalName>${artifactId}</finalName>
<plugins>
<plugin>
<groupId>net.wasdev.wlp.maven.plugins</groupId>
<artifactId>liberty-maven-plugin</artifactId>
<version>${liberty-maven-plugin.version}</version>
<configuration>
<assemblyArtifact>
<groupId>io.openliberty</groupId>
<artifactId>openliberty-webProfile8</artifactId>
<version>RELEASE</version>
<type>zip</type>
</assemblyArtifact>
<installAppPackages>project</installAppPackages>
<looseApplication>true</looseApplication>
<configFile>src/main/liberty/config/server.xml</configFile>
</configuration>
<executions>
<execution>
<id>install-server</id>
<phase>prepare-package</phase>
<goals>
<goal>install-server</goal>
<goal>create-server</goal>
<goal>install-feature</goal>
</goals>
</execution>
<execution>
<id>install-apps</id>
<phase>package</phase>
<goals>
<goal>install-apps</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jnosql.artemis</groupId>
<artifactId>artemis-configuration</artifactId>
<version>${jnosql.version}</version>
</dependency>
<dependency>
<groupId>org.jnosql.artemis</groupId>
<artifactId>artemis-document</artifactId>
<version>${jnosql.version}</version>
</dependency>
<dependency>
<groupId>org.jnosql.diana</groupId>
<artifactId>mongodb-driver</artifactId>
<version>${jnosql.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,10 @@
package com.baeldung.jnosql.artemis;
import javax.enterprise.context.Initialized;
import javax.enterprise.event.Observes;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("")
public class AppConfig extends Application {
}

View File

@ -0,0 +1,52 @@
package com.baeldung.jnosql.artemis;
import de.flapdoodle.embed.mongo.MongodExecutable;
import de.flapdoodle.embed.mongo.MongodProcess;
import de.flapdoodle.embed.mongo.MongodStarter;
import de.flapdoodle.embed.mongo.config.MongodConfigBuilder;
import de.flapdoodle.embed.mongo.config.Net;
import de.flapdoodle.embed.mongo.distribution.Version;
import de.flapdoodle.embed.process.runtime.Network;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Destroyed;
import javax.enterprise.context.Initialized;
import javax.enterprise.event.Observes;
import java.io.IOException;
@ApplicationScoped
public class EmbeddedMongoDBSetup {
private static final String MONGODB_HOST = "localhost";
private static final int MONGODB_PORT = 27019;
private static final MongodStarter starter = MongodStarter.getDefaultInstance();
private static MongodExecutable _mongodExe;
private static MongodProcess _mongod;
public void init(@Observes @Initialized(ApplicationScoped.class) Object init) {
try {
System.out.println("Starting Embedded MongoDB");
initdb();
System.out.println("Embedded MongoDB started");
} catch (IOException e) {
System.out.println("Embedded MongoDB starting error !!");
e.printStackTrace();
}
}
private void initdb() throws IOException {
_mongodExe = starter.prepare(new MongodConfigBuilder()
.version(Version.Main.DEVELOPMENT)
.net(new Net(MONGODB_HOST, MONGODB_PORT, Network.localhostIsIPv6()))
.build());
_mongod = _mongodExe.start();
}
public void destroy(@Observes @Destroyed(ApplicationScoped.class) Object init) {
System.out.println("Stopping Embedded MongoDB");
_mongod.stop();
_mongodExe.stop();
System.out.println("Embedded MongoDB stopped !");
}
}

View File

@ -0,0 +1,33 @@
package com.baeldung.jnosql.artemis;
import org.jnosql.artemis.ConfigurationUnit;
import org.jnosql.diana.api.document.DocumentCollectionManager;
import org.jnosql.diana.api.document.DocumentCollectionManagerFactory;
import org.jnosql.diana.mongodb.document.MongoDBDocumentCollectionManager;
import org.jnosql.diana.mongodb.document.MongoDBDocumentConfiguration;
import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Disposes;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
@ApplicationScoped
public class EntityManagerProducer {
private static final String DATABASE = "todos";
@Inject
@ConfigurationUnit(name = "document")
private DocumentCollectionManagerFactory<MongoDBDocumentCollectionManager> managerFactory;
@Produces
public DocumentCollectionManager getEntityManager() {
return managerFactory.get(DATABASE);
}
public void close(@Disposes DocumentCollectionManager entityManager) {
entityManager.close();
}
}

View File

@ -0,0 +1,41 @@
package com.baeldung.jnosql.artemis;
import com.baeldung.jnosql.artemis.qualifier.Repo;
import org.jnosql.artemis.Database;
import org.jnosql.artemis.DatabaseType;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import java.util.List;
import java.util.Optional;
@ApplicationScoped
@Repo
public class RepositoryTodoManager implements TodoManager {
@Inject
@Database(DatabaseType.DOCUMENT)
private TodoRepository repository;
@Override
public Todo add(Todo todo) {
return repository.save(todo);
}
@Override
public Todo get(String id) {
Optional<Todo> todo = repository.findById(id);
return todo.get();
}
@Override
public List<Todo> getAll() {
return repository.findAll();
}
@Override
public void delete(String id) {
repository.deleteById(id);
}
}

View File

@ -0,0 +1,43 @@
package com.baeldung.jnosql.artemis;
import com.baeldung.jnosql.artemis.qualifier.Template;
import org.jnosql.artemis.document.DocumentTemplate;
import org.jnosql.diana.api.document.DocumentQuery;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import java.util.List;
import java.util.Optional;
import static org.jnosql.diana.api.document.query.DocumentQueryBuilder.select;
@ApplicationScoped
@Template
public class TemplateTodoManager implements TodoManager {
@Inject
private DocumentTemplate documentTemplate;
@Override
public Todo add(Todo todo) {
return documentTemplate.insert(todo);
}
@Override
public Todo get(String id) {
Optional<Todo> todo = documentTemplate.find(Todo.class, id);
return todo.get();
}
@Override
public List<Todo> getAll() {
DocumentQuery query = select().from("Todo").build();
return documentTemplate.select(query);
}
@Override
public void delete(String id) {
documentTemplate.delete(Todo.class, id);
}
}

View File

@ -0,0 +1,51 @@
package com.baeldung.jnosql.artemis;
import org.jnosql.artemis.Column;
import org.jnosql.artemis.Entity;
import org.jnosql.artemis.Id;
import java.io.Serializable;
@Entity
public class Todo implements Serializable {
@Id("id")
public String id;
@Column
public String name;
@Column
public String description;
public Todo() {
}
public Todo(String id, String name, String description) {
this.id = id;
this.name = name;
this.description = description;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.jnosql.artemis;
import java.util.List;
public interface TodoManager {
Todo add(Todo todo);
Todo get(String id);
List<Todo> getAll();
void delete(String id);
}

View File

@ -0,0 +1,10 @@
package com.baeldung.jnosql.artemis;
import org.jnosql.artemis.Repository;
import java.util.List;
public interface TodoRepository extends Repository<Todo, String> {
List<Todo> findByName(String name);
List<Todo> findAll();
}

View File

@ -0,0 +1,48 @@
package com.baeldung.jnosql.artemis;
import com.baeldung.jnosql.artemis.qualifier.Repo;
import com.baeldung.jnosql.artemis.qualifier.Template;
import javax.inject.Inject;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
@Path("todos")
public class TodoResource {
/*
Use eiher @Template or @Repo
*/
@Inject
@Template
//@Repo
private TodoManager todoManager;
@GET
@Path("")
@Produces(MediaType.APPLICATION_JSON)
public Response all() {
return Response.ok(todoManager.getAll()).build();
}
@GET
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response get(@PathParam("id") String id) {
Todo todo = todoManager.get(id);
return Response.ok(todo).build();
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response add(Todo todo) {
Todo savedTodo = todoManager.add(todo);
System.out.println(savedTodo.id);
return Response.created(
UriBuilder.fromResource(this.getClass()).path(String.valueOf(savedTodo.id)).build())
.build();
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.jnosql.artemis.qualifier;
import javax.inject.Qualifier;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.TYPE})
@Qualifier
public @interface Repo {
}

View File

@ -0,0 +1,13 @@
package com.baeldung.jnosql.artemis.qualifier;
import javax.inject.Qualifier;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.TYPE})
@Qualifier
public @interface Template {
}

View File

@ -0,0 +1,6 @@
<server description="OpenLiberty Server">
<featureManager>
<feature>webProfile-8.0</feature>
</featureManager>
<httpEndpoint httpPort="9080" httpsPort="9443" id="defaultHttpEndpoint" host="*"/>
</server>

View File

@ -0,0 +1,6 @@
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
bean-discovery-mode="all">
</beans>

View File

@ -0,0 +1,10 @@
[
{
"description": "The mongodb document configuration",
"name": "document",
"provider": "org.jnosql.diana.mongodb.document.MongoDBDocumentConfiguration",
"settings": {
"mongodb-server-host-1":"localhost:27019"
}
}
]

View File

@ -0,0 +1,10 @@
[
{
"description": "The mongodb document configuration",
"name": "document",
"provider": "org.jnosql.diana.mongodb.document.MongoDBDocumentConfiguration",
"settings": {
"mongodb-server-host-1":"localhost:27019"
}
}
]

View File

@ -0,0 +1,93 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>com.baeldung.jnosql</groupId>
<artifactId>jnosql</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>jnosql-diana</artifactId>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<id>document</id>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>com.baeldung.jnosql.diana.document.DocumentApp</mainClass>
</configuration>
</execution>
<execution>
<id>column</id>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>com.baeldung.jnosql.diana.column.ColumnFamilyApp</mainClass>
</configuration>
</execution>
<execution>
<id>key</id>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>com.baeldung.jnosql.diana.key.KeyValueApp</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<!--NoSQL Document oriented-->
<dependency>
<groupId>org.jnosql.diana</groupId>
<artifactId>diana-document</artifactId>
<version>0.0.5</version>
</dependency>
<dependency>
<groupId>org.jnosql.diana</groupId>
<artifactId>mongodb-driver</artifactId>
<version>0.0.5</version>
</dependency>
<!--NoSQL Column oriented-->
<dependency>
<groupId>org.jnosql.diana</groupId>
<artifactId>diana-column</artifactId>
<version>0.0.5</version>
</dependency>
<dependency>
<groupId>org.jnosql.diana</groupId>
<artifactId>cassandra-driver</artifactId>
<version>0.0.5</version>
</dependency>
<!--NoSQL Key Value oriented-->
<dependency>
<groupId>org.jnosql.diana</groupId>
<artifactId>diana-key-value</artifactId>
<version>0.0.5</version>
</dependency>
<dependency>
<groupId>org.jnosql.diana</groupId>
<artifactId>hazelcast-driver</artifactId>
<version>0.0.5</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,32 @@
package com.baeldung.jnosql.diana.column;
import org.cassandraunit.utils.EmbeddedCassandraServerHelper;
import org.jnosql.diana.api.column.*;
import org.jnosql.diana.cassandra.column.CassandraConfiguration;
public class ColumnFamilyApp {
private static final String KEY_SPACE = "myKeySpace";
private static final String COLUMN_FAMILY = "books";
public static void main(String... args) throws Exception {
EmbeddedCassandraServerHelper.startEmbeddedCassandra();
ColumnConfiguration configuration = new CassandraConfiguration();
try(ColumnFamilyManagerFactory entityManagerFactory = configuration.get()) {
ColumnFamilyManager entityManager = entityManagerFactory.get(KEY_SPACE);
ColumnEntity columnEntity = ColumnEntity.of(COLUMN_FAMILY);
Column key = Columns.of("id", 10L);
Column name = Columns.of("name", "JNoSQL in Acion");
columnEntity.add(key);
columnEntity.add(name);
ColumnEntity saved = entityManager.insert(columnEntity);
System.out.println(saved);
}
EmbeddedCassandraServerHelper.cleanEmbeddedCassandra();
EmbeddedCassandraServerHelper.stopEmbeddedCassandra();
}
}

View File

@ -0,0 +1,67 @@
package com.baeldung.jnosql.diana.document;
import org.jnosql.diana.api.Settings;
import org.jnosql.diana.api.document.*;
import org.jnosql.diana.mongodb.document.MongoDBDocumentConfiguration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.jnosql.diana.api.document.query.DocumentQueryBuilder.delete;
import static org.jnosql.diana.api.document.query.DocumentQueryBuilder.select;
public class DocumentApp {
private static final String DB_NAME = "my-db";
private static final String DOCUMENT_COLLECTION = "books";
public static final String KEY_NAME = "_id";
DocumentConfiguration configuration = new MongoDBDocumentConfiguration();
public static void main(String... args) throws Exception {
MongoDbInit.startMongoDb();
DocumentApp app = new DocumentApp();
app.process();
MongoDbInit.stopMongoDb();
}
public void process() {
Map<String, Object> map = new HashMap<>();
map.put("mongodb-server-host-1", "localhost:27017");
try (DocumentCollectionManagerFactory managerFactory = configuration.get(Settings.of(map));
DocumentCollectionManager manager = managerFactory.get(DB_NAME);) {
DocumentEntity documentEntity = DocumentEntity.of(DOCUMENT_COLLECTION);
documentEntity.add(Document.of(KEY_NAME, "100"));
documentEntity.add(Document.of("name", "JNoSQL in Action"));
documentEntity.add(Document.of("pages", 620));
//CREATE
DocumentEntity saved = manager.insert(documentEntity);
//READ
DocumentQuery query = select().from(DOCUMENT_COLLECTION).where(KEY_NAME).eq("100").build();
List<DocumentEntity> entities = manager.select(query);
System.out.println(entities.get(0));
//UPDATE
saved.add(Document.of("author", "baeldung"));
DocumentEntity updated = manager.update(saved);
System.out.println(updated);
//DELETE
DocumentDeleteQuery deleteQuery = delete().from(DOCUMENT_COLLECTION).where(KEY_NAME).eq("100").build();
manager.delete(deleteQuery);
List<DocumentEntity> documentEntityList = manager.select(select().from(DOCUMENT_COLLECTION).build());
System.out.println(documentEntityList);
}
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.jnosql.diana.document;
import de.flapdoodle.embed.mongo.MongodExecutable;
import de.flapdoodle.embed.mongo.MongodProcess;
import de.flapdoodle.embed.mongo.MongodStarter;
import de.flapdoodle.embed.mongo.config.MongodConfigBuilder;
import de.flapdoodle.embed.mongo.config.Net;
import de.flapdoodle.embed.mongo.distribution.Version;
import de.flapdoodle.embed.process.runtime.Network;
import java.io.IOException;
public abstract class MongoDbInit {
private static final MongodStarter starter = MongodStarter.getDefaultInstance();
private static MongodExecutable _mongodExe;
private static MongodProcess _mongod;
public static void startMongoDb() throws IOException {
_mongodExe = starter.prepare(new MongodConfigBuilder()
.version(Version.Main.DEVELOPMENT)
.net(new Net("localhost", 27017, Network.localhostIsIPv6()))
.build());
_mongod = _mongodExe.start();
}
public static void stopMongoDb(){
_mongod.stop();
_mongodExe.stop();
}
}

View File

@ -0,0 +1,63 @@
package com.baeldung.jnosql.diana.key;
import java.io.Serializable;
public class Book implements Serializable {
private String isbn;
private String name;
private String author;
private int pages;
public Book() {
}
public Book(String isbn, String name, String author, int pages) {
this.isbn = isbn;
this.name = name;
this.author = author;
this.pages = pages;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getPages() {
return pages;
}
public void setPages(int pages) {
this.pages = pages;
}
@Override
public String toString() {
return "Book{" +
"isbn='" + isbn + '\'' +
", name='" + name + '\'' +
", author='" + author + '\'' +
", pages=" + pages +
'}';
}
}

View File

@ -0,0 +1,33 @@
package com.baeldung.jnosql.diana.key;
import com.hazelcast.core.Hazelcast;
import org.jnosql.diana.api.Value;
import org.jnosql.diana.api.key.BucketManager;
import org.jnosql.diana.api.key.BucketManagerFactory;
import org.jnosql.diana.api.key.KeyValueConfiguration;
import org.jnosql.diana.api.key.KeyValueEntity;
import org.jnosql.diana.hazelcast.key.HazelcastKeyValueConfiguration;
import java.util.Optional;
public class KeyValueApp {
private static final String BUCKET_NAME = "books";
public static void main(String... args) throws Exception {
KeyValueConfiguration configuration = new HazelcastKeyValueConfiguration();
try (BucketManagerFactory managerFactory = configuration.get()) {
BucketManager manager = managerFactory.getBucketManager(BUCKET_NAME);
Book book = new Book("12345", "JNoSQL in Action", "baeldung", 420);
KeyValueEntity keyValueEntity = KeyValueEntity.of(book.getIsbn(), book);
manager.put(keyValueEntity);
Optional<Value> optionalValue = manager.get("12345");
Value value = optionalValue.get();
Book savedBook = value.get(Book.class);
System.out.println(savedBook);
}
Hazelcast.shutdownAll();
}
}

View File

@ -0,0 +1,5 @@
cassandra-host-1=localhost
cassandra-port=9142
#cassandra-threads-number=2
cassandra-query-1=CREATE KEYSPACE IF NOT EXISTS myKeySpace WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 3};
cassandra-query-2=CREATE COLUMNFAMILY IF NOT EXISTS myKeySpace.books (id bigint PRIMARY KEY, name text);

View File

@ -0,0 +1 @@
hazelcast-instanceName=hazelcast

View File

@ -0,0 +1,2 @@
#Define Host and Port
mongodb-server-host-1=localhost:27017

23
jnosql/pom.xml Normal file
View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<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.baeldung.jnosql</groupId>
<artifactId>jnosql</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<jnosql.version>0.0.5</jnosql.version>
</properties>
<modules>
<module>jnosql-diana</module>
<module>jnosql-artemis</module>
</modules>
</project>

View File

@ -545,6 +545,7 @@
<module>antlr</module> <module>antlr</module>
<module>maven-archetype</module> <module>maven-archetype</module>
<module>apache-meecrowave</module> <module>apache-meecrowave</module>
<module>jnosql</module>
</modules> </modules>
</profile> </profile>