This repository has been archived by the owner on Apr 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
library client
Konstantin Sobolev edited this page Jun 12, 2017
·
3 revisions
This section shows how to generate and use Epigraph client.
Lets start by creating new module: library-service
. Here's full 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>examples-library-client</artifactId>
<parent>
<groupId>ws.epigraph.examples</groupId>
<artifactId>examples-library</artifactId>
<version>0.0.2-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>examples-library-schema</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>ws.epigraph</groupId>
<artifactId>epigraph-java-http-client</artifactId>
<version>0.0.2-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>ws.epigraph</groupId>
<artifactId>epigraph-java-maven-plugin</artifactId>
<configuration><client/></configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>ws.epigraph.examples.library.LibraryClient</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
Important things:
- Dependency on library schema is obvious
-
epigraph-java-http-client
is needed by generated client code -
<client/>
configuration instructs Epigraph plugin to generate the client
Don't forget to add <module>library-client</module>
to the parent pom.
Now we can generate the client:
mvn -pl library-client generate-sources
This produces BooksClient
class.
Lets use it. Here's a simple application to demonstrate how to call all supported operations. Notice that projections are passed as strings for now, this will be improved in the future.
package ws.epigraph.examples.library;
import org.apache.http.HttpHost;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import ws.epigraph.examples.library.resources.books.client.BooksClient;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
public class LibraryClient {
public static final int PORT = 8888;
public static final String HOST = "localhost";
public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
try (CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault()) {
httpClient.start();
BooksClient client = new BooksClient(new HttpHost(HOST, PORT), httpClient);
// create a book
BookId_List createdIds = client.create(
null, // no input projection
BookRecord_List.create()
.add(BookRecord.create()
.setAuthor(Author.create().setId(AuthorId.create(1L)))
.setTitle("Quantum Physics for Dummies")
.setText(Text.create()
.setPlain(PlainText.create(
"One of the central problems of quantum mechanics is to calculate the energy levels of a " +
"system. The energy operator, called the Hamiltonian, abbreviated H, gives you the " +
"total energy.")
)
)
),
"*" // output projection: get all list elements
).get();// it's a Future, so we have to get()
BookId createdId = createdIds.datums().iterator().next(); // not checking for errors to keep things simple
// search
BookId_BookRecord_Map booksByAllan = client.readSearchByAuthor(";author={firstName:'Allan'}[](title)").get();
System.out.println("Books by Allan:");
for (BookRecord bookRecord : booksByAllan.datums().values()) {
System.out.println(bookRecord.getTitle());
}
// update
client.update(
null, // no update projection
BookId_BookRecord_Map.create() // change created book's author id to 2
.put(createdId, BookRecord.create().setAuthor(Author.create().setId(AuthorId.create(2L)))),
"" // not interested in output
);
// read
BookId_BookRecord_Map booksMap =
client.read("[" + createdId + "](+author:+record(+firstName,+lastName))").get(); // '+' is 'required'
BookRecord bookRecord = booksMap.datums().get(createdId.toImmutable());
AuthorRecord authorRecord = bookRecord.getAuthor().getRecord();
System.out.println("\nNew author: " + authorRecord.getFirstName() + " " + authorRecord.getLastName());
// delete
client.delete("[" + createdId + "]", "");
}
}
}
First start the server:
mvn -pl library-service exec:java
Then our example (in another terminal):
mvn -pl library-client exec:java
Output should be:
Books by Allan:
The Gold Bug
Quantum Physics for Dummies
New author: Arthur Doyle