<?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">

<h1 class="title">5.1.8 Collection Registration</h1>
<h2 id="overview">Overview</h2>
<p>This chapter describes registering data in a GridDB collection.</p>
<p><br/></p>
<h2 id="data-to-be-registered">Data to be registered</h2>
<p>Create registration data in a CSV file with the contents as follows:</p>
<ul>
<li>Measuring instrument CSV file
<ul>
<li>File name
<ul>
<li><code>Weather_station.csv</code></li>
</ul></li>
<li>Retention data
<ul>
<li>Row 1: measuring instrument ID</li>
<li>Row 2: The name</li>
<li>Row 3: installation coordinates (latitude)</li>
<li>Row 4: installation coordinates (longitude)</li>
<li>Row 5: camera presence</li>
</ul></li>
</ul></li>
</ul>
<br/> <strong>List.1 Data File</strong> (weather_station.csv)
<pre class="prettyprint linenums">
1,Hokkaido-Sapporo,43.06417,141.34694,true
2,Aomori-Aomori-City,40.82444,140.74,false
3,Iwate-Morioka,39.70361,141.1525,true
4,Miyagi-Sendai,38.26889,140.87194,false
5,Akita-Akita,39.71861,140.1025,true
(Snip)
</pre>
<h2 id="data-registration">Data registration</h2>
<p>The following code demonstrates registering data:</p>
<strong>List.2 Simple Data Registration</strong> (CollectionRegister.java)
<pre class="prettyprint linenums:25">
// Read WeatherStation data from csv
List <WeatherStation> weatherStationList=wsLogic.readCsv ();
// Register Collection
wsLogic.registerCollection (store, weatherStationList);
</pre>
<ul>
<li>L.26: Read the CSV to get a list of measuring instruments.</li>
<li>L.28: Register the list of measuring instruments in a GridDB collection.</li>
</ul>
<br/> <strong> List.3 Read CSV</strong> (WeatherStationLogic.java)
<pre class="prettyprint linenums:25">
public List&lt;WeatherStation&gt; readCsv() throws IOException {
    List&lt;WeatherStation&gt; weatherStationList = new ArrayList&lt;WeatherStation&gt;();
    // Read CSV file
    CSVReader reader = new CSVReader(new FileReader("data/weather_station.csv"));

    try {
        String[] line = null;
        WeatherStation weatherStation = null;

        // Read all line of CSV
        while ((line = reader.readNext()) != null) {
            // Set the value to Row of WeatherStation
            weatherStation = new WeatherStation();
            weatherStation.id = line[0];
            weatherStation.name = line[1];
            weatherStation.latitude = Double.valueOf(line[2]);
            weatherStation.longitude = Double.valueOf(line[3]);
            weatherStation.hasCamera = Boolean.valueOf(line[4]);

            // Add Collection
            weatherStationList.add(weatherStation);
        }
    } finally {
        // Close CSV
        reader.close();
    }
    return weatherStationList;
}
</pre>
<h3 id="csv-read">Read CSV</h3>
<ul>
<li>L.31: Read a CSV file <code>weather_station.csv</code> that contains the measuring instrument's data. The operation of reading the CSV file uses the <code>CSVReader</code> class of which is an open source software <a href="http://opencsv.sourceforge.net/" target="_blank" class="openwindow">opencsv</a>.</li>
<li>L.38-45: Read the contents of the CSV and set the values to an instance of <code>WeatherStation</code>.</li>
</ul>
<p><br/></p>
<h3 id="collection-registration">Register Collection</h3>
<strong> List.4 Register Data</strong> (WeatherStationLogic.java)
<pre class="prettyprint linenums:61">
public void registerCollection(GridStore store, List&lt;WeatherStation&gt; weatherStationList)
        throws GSException {
    // Get Collection
    Collection&lt;String, WeatherStation&gt; weatherStationCol =
            store.getCollection("weather_station", WeatherStation.class);
    try {
        // Disable Auto Commit
        weatherStationCol.setAutoCommit(false);

        boolean isSuccess = true;

        for (WeatherStation weatherStation : weatherStationList) {
            if (weatherStation != null) {
                // Add Collection
                weatherStationCol.put(weatherStation.id, weatherStation);
            } else {
                // if Row Data is null, abort register
                weatherStationCol.abort();
                System.out.println("Register of the Collection is aborted.");
                isSuccess = false;
                break;
            }
        }

        // Commit only when the registration was successful all
        if (isSuccess) {
            // Commit
            weatherStationCol.commit();
        }
    } finally {
        // Close Connection
        weatherStationCol.close();
    }
}
</pre>
<h3 id="container-acquisition">Get Container</h3>
<ul>
<li>L.67-68: Get the subject of container to be registered.</li>
</ul>
<p><br/></p>
<h3 id="auto-commit">Auto-commit</h3>
<ul>
<li>L.71: Disable auto-commit.</li>
</ul>
<p>Auto-commit is set by <code>Collection.setAutoCommit ()</code> method. Auto-commit by default in GridDB is enabled.<br>
When you register the data collectively, to disable auto-commit makes a reduction in the number of times of the inquiry, therefore increasing the throughput is expectable.</p>
<h3 id="data-added">Add Data</h3>
<ul>
<li>L.78: Add the value read from CSV to the collection.</li>
</ul>
<p>In this code auto-commit is disabled, but If auto-commit is enabled, the data will be registered here.</p>
<h3 id="commits">Commit</h3>
<ul>
<li>L.91: Commit to a collection.</li>
</ul>
<p>Commit is executed by <code>Collection.commit ()</code> method. If auto-commit is disabled, the data is registered here.</p>
<h3 id="roll-back">Roll back</h3>
<ul>
<li>L.81: Execute rollback to collection.</li>
</ul>
<p>Rollback is executed by <code>Collection.abort ()</code> method.<br>
Rollback must be executed only when an exception occurs other than <code>GSException</code> or problems are detected. Otherwise operation of the pre-commit will be canceled if an exception <code>GSException</code> occurs when auto-commit is disabled.<br>
In this sample, if the data of the measuring instrument read from the CSV is NULL, execute rollback assuming that a problem occurs.</p>
<p><br/></p>
<h2 id="complete-source-code">Complete source code</h2>
<p>Complete source code used in this sample can be downloaded from the following.</p>
<p>Download: <a href="img/collection-register.zip">collection-register.zip</a></p>
<p><br/></p>
</div>
</div>
</div>
</div>
</div>
<!-- / main -->

<?php get_footer(); ?>
