Build Your Java Application
Follow the instructions in this section to build a Java application that uses OJAI and accesses MapR Database JSON tables.
1. Prepare the Directory Structure
This application uses the JSON tables that are mapped to the filesystem and uses the POSIX permission model (read/write for users and groups), and more. In this tutorial we do not use any advanced security features. To read more about security, see Performing File System Operations on MapR Database Tables.
apps
directory, and add a blog
folder
(used by the Java REST Sample
application).ssh mapr@maprdemo
cd /mapr/demo.mapr.com/
ls -la
chmod 777 apps
mkdir apps/blog
chmod 777 apps/blog
2. MapR Database and Java: Overview
The OJAI 101 repository contains the full application that shows the core operations of MapR Database and OJAI.
-Dmapr.home.dir=/opt/mapr
If you used a different name for the
folder in opt
, use that name.
Table table = MapRDB.createTable("/apps/user_profiles");
Document doc = MapRDB.newDocument() .set("firstName", "John") .set("lastName", "Doe") .set("age", 50);
{ "firstName" : "John", "lastName" : "Doe", "age" : 50 }
insert()
method. You just
need to set the Document ID (or
rowkey):table.insert("jdoe", doc);
The Document ID is unique
within a table. If you try to insert a document using an existing key, a
DocumentExistsException
will be raised. You can also use the
insertOrReplace
method; with this method, if the document with the ID
exists, it will be replaced.
Document doc2 = table.findById("jdoe"); System.out.println( doc2 ); System.out.println( doc2.getString("firstName") +":"+ doc2.getInt("age") );
DocumentMutation mutation = MapRDB.newMutation() .increment("age", 1) .set("interests", Arrays.asList("sports", "movies")); table.update("jdoe", mutation);
As
you can see, not only can you update an existing attribute, such as the age increment, but
you can also modify the structure of the document. For example, we added the
interests
field to the document.
QueryCondition condition = MapRDB.newCondition() .is("age", Condition.Op.GREATER, 50) .build(); DocumentStream rs = table.find(condition); Iterator<Document> itrs = rs.iterator(); while (itrs.hasNext()) { System.out.println( itrs.next() ); } rs.close();
table.delete("jdoe");
MapRDB.deleteTable("/apps/user_profiles");
You have now learned the basics of OJAI and you can see how to store, retrieve, and delete a document from a MapR Database table.
You can now start building your own application or look at the different sample applications available.
3. MapR Database and Java: Sample Application
Clone the following git repository: https://github.com/mapr-demos/maprdb-ojai-101
Look at the com.mapr.db.samples.basic.Ex01SimpleCRUD.run() method.
This method calls three other methods:
createDocuments()
, which shows different ways of creating documentsqueryDocuments()
, which shows different ways of querying documentsupdateDocuments()
, which shows different ways of updating documents
mvn clean package
mvn exec:java -Dexec.mainClass="com.mapr.db.samples.basic.Ex01SimpleCRUD"
You can use the MapR Database Shell described at the end of this document to query JSON documents, including the one created by running the sample application.
4. MapR Database and Java: REST API Sample
Clone the following git repository: https://github.com/mapr-demos/maprdb-ojai-rest-sample
This web application exposes a MapR Database document using a REST API (using JAX-RS and Swagger).
mvn clean package
mvn exec:java -Dexec.mainClass="com.mapr.db.samples.rest.Main"
Look at com.mapr.db.samples.rest.Main.
Start the application and go to: http://localhost:8080