<?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.12 Data Registration</h1>
<h2 id="overview">Overview</h2>
<p>This section describes the data registration for TimeSeries containers</p>
<p><br/></p>
<h2 id="data-to-be-registered">Sample Data</h2>
<p>Similar to collection sample, data to be registered is initially stored in a CSV file. Data must be registered in order to later read. The contents of the CSV file (Instrument_log.csv) is as follows:</p>
<ul>
<li>Data held</li>
<li>Measuring instrument ID</li>
<li>Timestamp</li>
<li>Temperature</li>
<li>Live image file path</li>
</ul>
<br/> <strong>List.1 Measurement Log Contents</strong> (instrument_log.csv)
<pre class="prettyprint linenums">
weather_station_1,2016/7/1 0: 00,50, liveimage1.jpg
weather_station_2,2016/7/1 0: 00,50,
weather_station_3,2016/7/1 0: 00,50, liveimage2.jpg
weather_station_4,2016/7/1 0: 00,50,
weather_station_5,2016/7/1 0: 00,50, liveimage1.jpg
(Snip)
</pre>
<h2 id="data-registration">Data Registration Process</h2>
<p>The following snippet demonstrates data registration.</p>
<strong>List.2 Register TimeSeries Container</strong> (TimeSeriesRegister.java)
<pre class="prettyprint linenums:31">
// Read InstrumentLog data from csv
List&lt;InstrumentLog&gt; logList = logLogic.readCsv();
// Register TimeSeries Container
logLogic.registerTimeSeriesContainer(store, logList);
</pre>
<ul>
<li>L.32: Process the CSV into a list of Instrument Logs.</li>
<li>L.34: Register the parsed data in GridDB.</li>
</ul>
<br/> <strong>List.3 Read Measurement Log</strong> (InstrumentLogLogic.java)
<pre class="prettyprint linenums:61">
public List&lt;InstrumentLog&gt; readCsv()
    throws IOException, ParseException, SerialException, SQLException {
    // Read CSV file
    List&lt;InstrumentLog&gt; logList = new ArrayList&lt;InstrumentLog&gt;();
    SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH: mm", Locale.US);
    String[] line = null;
    CSVReader reader = new CSVReader(new FileReader("data/instrument_log.csv"));

    try {
        while ((line = reader.readNext()) != null) {
            InstrumentLog log = new InstrumentLog();
            log.weatherStationId = line[0];
            log.timestamp = format.parse(line[1]);
            log.temperture = Float.valueOf(line[2]);
    
            // Write Blob data
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            String filePath = line[3];
            if ((filePath != null) &amp;&amp; (!filePath.isEmpty())) {
                File imageFile = new File(filePath);
                if (imageFile.exists() &amp;&amp; imageFile.isFile()) {
                    InputStream inputStream = null;
                    try {
                        inputStream = new BufferedInputStream(new FileInputStream(imageFile));
                        byte[] buff = new byte[1024];
                        while ((inputStream.read(buff)) != -1) {
                            outputStream.write(buff);
                        }
                    } finally {
                        if (inputStream != null) {
                            inputStream.close ();
                        }
                    }
                }
            }
            log.liveImage = new SerialBlob(outputStream.toByteArray());
    
            logList.add(log);
        }

    } finally {
        reader.close();
    }
    return logList;
}
</pre>
<h3 id="csv-read">Read CSV</h3>
<ul>
<li>L.67: Loading a CSV file <code>instrument_log.csv</code> with the data of the measurement log.</li>
<li>L.70-74: read the contents of the CSV, it is set to an instance of <code>InstrumentLog</code>.</li>
<li>L.77-95: read the file of the CSV of the live image file path, is writing the data to the Blob data.</li>
</ul>
<br/> <strong>List.4 TimeSeries Container Registration Process</strong> (InstrumentLogLogic.java)
<pre class="prettyprint linenums:117">
public void registerTimeSeriesContainer(GridStore store, List&lt;InstrumentLog&gt; logList)
        throws GSException {
    for (InstrumentLog log : logList) {
        // Retrieve TimeSeries Container
        TimeSeries&lt;InstrumentLog&gt; logTs =
                store.putTimeSeries(log.weatherStationId, InstrumentLog.class);

        // Disable Auto Commit
        logTs.setAutoCommit(false);

        // Specify Time
        logTs.put(log.timestamp, log);

        // Specify Current Time
        // Comment out because the result would change on every execution
        // ts.append(log);

        // Instrument log timestamp is every 6 hours.
        // When the interpolation of 3 hours prior time to the log,
        // the temperature of the intermediate is registered
        Date medianTime = TimestampUtils.add(log.timestamp, -3, TimeUnit.HOUR);
        // Creating a TimeSeries Container when interpolating the values of temperature
        InstrumentLog medianLog = logTs.interpolate(medianTime, "temperture");
        // If there is no log, a NULL is returned before the interpolated time
        if (medianLog != null) {
            logTs.put(medianTime, medianLog);
        }

        // Commit
        logTs.commit();
    }
}
</pre>
<p>To register the data to the time series container in one of the following ways.</p>
<h3 id="registered-in-the-specified-time">Specified time</h3>
<ul>
<li>L.128: <code>TimeSeries.put (Date, Class)</code> method uses the time specified as the key when the data is registered. </li>
</ul>
<h3 id="currently-registered-at-the-time">Current time</h3>
<ul>
<li>L.132: <code>TimeSeries.append (Class)</code> method uses the current time as the key. However, it has been commented out so that execution result is always constant.</li>
</ul>
<h3 id="registered-in-the-interpolated-time">Interpolated time</h3>
<ul>
<li>L.137: <code>TimestampUtils.add (Date, int, TimeUnit)</code> specifies a time 3 hours prior to the time of the time of measurement log.</li>
<li>L.139: <code>TimeSeries.interpolate (Date, String)</code> generates a linear interpolation of the value corresponding to the specified time. This means, for example, when the temperature is  70°F at 6 and 80°F at 12, the interpolated value would be 75°F if 9 was specified.  To obtain a linear interpolation value must have adjacent keys or the same timestamp must have been previously registered.</li>
<li>L.142: <code>TimeSeries.put (Date, Class) and is registered with the data that is linear interpolation with</code> method.</li>
</ul>
<p><br/></p>
<p>The following topics are described in more detail in the <a href="5-1-8_collection-register.php">Collection Registration</a> chapter:</p>
<ul>
<li>Auto-commit</li>
<li>Commit</li>
<li>Roll Back</li>
</ul>
<p><br/></p>
<h3 id="complete-source-code">Complete source code</h3>
<p>Complete source code used in this sample can be downloaded from the following.</p>
<p>Download: <a href="img/timeseries-register.zip">timeseries-register.zip</a></p>
<p><br/></p>
</div>
</div>
</div>
</div>
</div>

<!-- / main -->

<?php get_footer(); ?>
