2.4.1 Your First Java Application
If you prefer videos:
You've got GridDB installed using the Quickstart blogpost and now it's time to start developing your first Java application. This blog post will take you through the basics:
First, the required import statements:
import com.toshiba.mwcloud.gs.Collection; import com.toshiba.mwcloud.gs.GSException; import com.toshiba.mwcloud.gs.GridStore; import com.toshiba.mwcloud.gs.GridStoreFactory; import com.toshiba.mwcloud.gs.Query; import com.toshiba.mwcloud.gs.RowKey; import com.toshiba.mwcloud.gs.RowSet;
The container schema is defined as a static Class in Java.
static class Person { @RowKey String name; int age; } static class HeartRate { @RowKey Date ts; int heartRate; String activity; }
To connect to GridDB, you create a Properties instance with the particulars of your GridDB installation.
Properties props = new Properties(); props.setProperty("notificationAddress", "239.0.0.1"); props.setProperty("notificationPort", "31999"); props.setProperty("clusterName", "defaultCluster"); props.setProperty("user", "admin"); props.setProperty("password", "admin"); GridStore store = GridStoreFactory.getInstance().getGridStore(props);
To perform queries or write records, you first need to get the container:
Collection<String, Person> people = store.putCollection("PEOPLE", Person.class);
Querying is similar to SQL/JDBC drivers.
Query<Person< query = col.query("select * where name = 'John'"); RowSet<Person> rs = query.fetch(false); while (rs.hasNext()) { // Update the searched Row Person person1 = rs.next(); System.out.println("Name: "+ person1.name +" Age: "+ person1.age) }
Writing is simple...
HeartRate hr = new HeartRate(); hr.ts = new Date(); hr.heartrate = 60; hr.activity = "resting"; TimeSeries<HeartRate> heartRate = store.putTimeSeries("HR_"+person1.name, HeartRate.class); heartRate.put(hr);
Updating is simple:
Query<Person> query = col.query("select * where name = 'John'"); RowSet<Person> rs = query.fetch(true); while (rs.hasNext()) { // Update the searched Row Person person1 = rs.next(); person1.age++; rs.update(person1); }
To compile use the following snippet:
$ cd gsSample/
$ export CLASSPATH=$CLASSPATH:/usr/share/java/gridstore.jar
$ javac Sample1.java
$ cd .. && java gsSample/Sample1
Alternatively, you can check out how to use maven with GridDB here.