<?php require_once( dirname(dirname(dirname( __FILE__ ))) . '/wp-load.php' ); ?>
<!-- START of header -->
<?php get_header(); ?>
<!-- END of header -->

<!-- warapper -->

<div class="docs-content">
    <!-- START of page navigation -->
    <?php get_template_part( 'docs_navigation' ); ?>
    <!-- END of page navigation -->
    <!-- START of pusher -->
    <div class="docs-content-body">
        <div id="content" class="docs-content-body__inner">


            <div id="outline-container-1" class="outline-3">
            <h1 id="sec-1"><span class="section-number-3">2.4.1</span> Your First Java Application </h1>
            <div class="outline-text-3" id="text-1">

            <p> If you prefer videos:</p>

<div style="text-align: center; padding: 25px">
<iframe width="560" height="315" src="https://www.youtube.com/embed/TMQAZrRHFuE" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>

<p>You've got GridDB installed using the <a href="https://griddb.net/en/blog/griddb-quickstart/">Quickstart blogpost</a> and now it's time to start developing your first Java application. This blog post will take you through the basics:</p>

<p>First, the required import statements:</p>

<pre>
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;
</pre>

<p>The container schema is defined as a static Class in Java.</p>

<pre>
static class Person {
	@RowKey String name;
	int age;
}
static class HeartRate {
	@RowKey Date ts;
	int heartRate;
        String activity;
}
</pre>

<p>To connect to GridDB, you create a Properties instance with the particulars of your GridDB installation.</p>

<pre>
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);
</pre>

<p>To perform queries or write records, you first need to get the container:</p>

<pre>
Collection&lt;String, Person&gt; people = store.putCollection("PEOPLE", Person.class);
</pre>

<p>Querying is similar to SQL/JDBC drivers.</p>

<pre>
Query&lt;Person&lt; query = col.query("select * where name = 'John'");
RowSet&lt;Person&gt; rs = query.fetch(false);
while (rs.hasNext()) {
	// Update the searched Row
	Person person1 = rs.next();
        System.out.println("Name: "+ person1.name +" Age: "+ person1.age)
}
</pre>

<p>Writing is simple...</p>

<pre>
HeartRate hr = new HeartRate();
hr.ts = new Date();
hr.heartrate = 60;
hr.activity = "resting";
TimeSeries&lt;HeartRate&gt; heartRate = store.putTimeSeries("HR_"+person1.name, HeartRate.class);
heartRate.put(hr);
</pre>

<p>Updating is simple:</p>

<pre>
Query&lt;Person&gt; query = col.query("select * where name = 'John'");
RowSet&lt;Person&gt; rs = query.fetch(true);
while (rs.hasNext()) {
	// Update the searched Row
	Person person1 = rs.next();
        person1.age++;
        rs.update(person1);
}
</pre>

<p>To compile use the following snippet:</p>

<div class="clipboard">
<pre><code class="language-sh">
$ cd gsSample/
$ export CLASSPATH=$CLASSPATH:/usr/share/java/gridstore.jar
$ javac Sample1.java
$ cd .. && java gsSample/Sample1
</code>
</pre>
</div>

<p>Alternatively, you can check out how to use maven with GridDB <a href="https://griddb.net/en/blog/using-maven-to-develop-griddb-applications/">here</a>.</p>
            
        </div>
    </div>
</div>
<!-- / main -->

<?php get_footer(); ?>
