<?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.13 Data Retrieval</h1>
<h2 id="overview">Overview</h2>
<p>This section describes the data acquisition of time-series container of GridDB in this chapter.</p>
<p><br/></p>
<h2 id="data-acquisition">Data Acquisition</h2>
<p>The following are methods to fetch and read TimeSeries containers:</p>
<h3>Specified time</h3>
<p>Time to get the data (= low) of the series container, you will need to specify the time is Rouki of time series container.</p>
<strong>List.1 Acquire Data for a Specified Time</strong> (TimeSeriesRetrieve.java)
<pre class="prettyprint linenums:99">
// Specify Time
InstrumentLog log = logTs.get(format.parse("2016/07/02 12:00"));
System.out.println("get by Time");
System.out.println(String.format("%s\t%-20s\t%-10s", log.timestamp, log.weatherStationId,
		log.temperture));
</pre>
<ul>
<li>Line 100: Use <code>TimeSeries.get (Date)</code> to get rows with a specified time stamp.</li>
</ul>
<p>Execution results are as follows.</p>
<strong>List.2 Results</strong>
<pre class="prettyprint">
get by Time
Sat Jul 02 12:00:00 EDT 2016 weather_station_1 80.0
</pre>
<p><br/></p>
<h3>Specified range</h3>
<p>Time series container will be able to get to specify the range to be retrieved.</p>
<strong>List.3 Aquire rows within a Range </strong> (TimeSeriesRetrieve.java)
<pre class="prettyprint linenums:117">
// Specify Time Range
System.out.println("get by Time Range");
Date start = format.parse("2016/07/02 9:00");
Date end = TimestampUtils.add(start, 6, TimeUnit.HOUR);

Query&lt;InstrumentLog&gt; query = logTs.query(start, end);
// fetch row
RowSet&lt;InstrumentLog&gt; rowSet = query.fetch();
while (rowSet.hasNext()) {
InstrumentLog log = rowSet.next();
System.out.println("Timestamp\t\t\tWeatherStation ID\tTemperture\tLive Image");
System.out.println(String.format("%s\t%-20s\t%-10s\t%s", log.timestamp,
		log.weatherStationId, log.temperture));
}
</pre>
<ul>
<li>Line 119-120: Generate the start and end time for the range.</li>
<li>Line 122: Use <code>TimeSeries.query (Date, Date)</code> method with start and end times for the range of data to fetch.</li>
<li>Line 124-126: Fetch rows.</li>
</ul>
<p>Execution results are as follows.</p>
<strong>List.4 Results</strong>
<pre class="prettyprint">
get by Time Range
Sat Jul 02 09:00:00 EDT 2016 weather_station_1 75.0
Sat Jul 02 12:00:00 EDT 2016 weather_station_1 80.0
Sat Jul 02 15:00:00 EDT 2016 weather_station_1 75.0
</pre>
<p><br/></p>
<h3 id="gets-the-specified-time-on-the-basis">Relative Time</h3>
<p>You can fetch rows with timestamps are earlier or later than specified timestamp with the <code>TimeOperator</code> enum.</p>
<p><strong>Table 1 TimeOperator Enum</strong></p>
<div markdown="1" class="griddb">
<p><div>

<table class="table-dl-connector"> 
 
<tr><td> Acquisition method </td><td> Description </td></tr>
<tr><td> TimeOperator.NEXT </td><td> Returns the oldest among the Rows whose timestamp are identical with or later than the specified time.
</td></tr>
<tr><td> TimeOperator.NEXT_ONLY </td><td> Returns the oldest among the Rows whose timestamp are later than the specified time.
</td></tr>
<tr><td> TimeOperator.PREVIOUS </td><td> Returns the newest among the Rows whose timestamp are identical with or earlier than the specified time.
</td></tr>
<tr><td> TimeOperator.PREVIOUS_ONLY </td><td> Returns the newest among the Rows whose timestamp are earlier than the specified time.
</td></tr>

</table>
<p><br/></p>
<strong>Fetching Rows with a Relative Time stamp</strong> (TimeSeriesRetrieve.java)
<pre class="prettyprint linenums:143">
// Specify the next time, including a specified time
InstrumentLog log = logTs.get(format.parse("2016/07/02 12:00"), TimeOperator.NEXT);
System.out.println("get by Next Time");
System.out.println(String.format("%s\t%-20s\t%-10s", log.timestamp, log.weatherStationId,
		log.temperture));

// Specify the next time
log = logTs.get(format.parse("2016/07/02 12:00"), TimeOperator.NEXT_ONLY);
System.out.println("get by NextOnly Time");
System.out.println(String.format("%s\t%-20s\t%-10s", log.timestamp, log.weatherStationId,
		log.temperture));

// Specify the previous time, including a specified time
log = logTs.get(format.parse("2016/07/02 12:00"), TimeOperator.PREVIOUS);
System.out.println("get by Previous Time");
System.out.println(String.format("%s\t%-20s\t%-10s", log.timestamp, log.weatherStationId,
		log.temperture));

// Specify the previous time
log = logTs.get(format.parse("2016/07/02 12:00"), TimeOperator.PREVIOUS_ONLY);
System.out.println("get by PreviousOnly Time");
System.out.println(String.format("%s\t%-20s\t%-10s", log.timestamp, log.weatherStationId,
		log.temperture));
</pre>
<ul>
<li>Line 144: you get to specify the <code>TimeOperator.NEXT</code>.</li>
<li>Line 150: you get to specify the <code>TimeOperator.NEXT_ONLY</code>.</li>
<li>Line 156: you get to specify the <code>TimeOperator.PREVIOUS</code>.</li>
<li>Line 162: you get to specify the <code>TimeOperator.PREVIOUS_ONLY</code>.</li>
</ul>
<p>Serving as a reference time has designated all <code>&quot; 2016/07/02 12:00 &quot;</code>.</p>
<p>Execution results are as follows.</p>
<strong>List.6 Results</strong> that the constant time was on the basis of
<pre class="prettyprint">
get by Next Time
Sat Jul 02 12:00:00 EDT 2016 weather_station_1 80.0
get by NextOnly Time
Sat Jul 02 15:00:00 EDT 2016 weather_station_1 75.0
get by Previous Time
Sat Jul 02 12:00:00 EDT 2016 weather_station_1 80.0
get by PreviousOnly Time
Sat Jul 02 09:00:00 EDT 2016 weather_station_1 75.0
</pre>
<p><br/></p>
<h3 id="aggregate">Aggregatation</h3>
<p>Time series container is able to aggregate the data for the specified time period.</p>
<strong>List.7 Aggregate Average</strong>
<pre class="prettyprint linenums:178">
// Average Temperature
Date start = format.parse("2016/07/01 12:00");
Date end = format.parse("2016/07/02 9:00");
AggregationResult aggrResult =
		logTs.aggregate(start, end, "temperture", Aggregation.AVERAGE);
System.out.println("Average Temperature:" + aggrResult.getDouble() + "\n");
</pre>
<ul>
<li>181-182 line: <code>TimeSeries.aggregate (Date, Date, String, Aggregation)</code> method You have a summary to find the average value of the temperature.</li>
</ul>
<p>Execution results are as follows.</p>
<strong>List.8 Aggregate Results</strong>
<pre>
Average Temperature: 67.5
</pre>
<p>The different types of Aggregation methods are as follows:</p>
<p><strong>Table 2 Aggregation types</strong></p>
<div markdown="1" class="griddb">
<table class="table-dl-connector">
<tr><td> Aggregation method </td><td> Description </td></tr>
<tr><td> Aggregation.AVERAGE </td><td> Obtain the average value. </td></tr>
<tr><td> Aggregation.COUNT </td><td> Obtain the number of samples. </td></tr>
<tr><td> Aggregation.MAXIMUM </td><td> Obtain the maximum value. </td></tr>
<tr><td> Aggregation.MINIMUM </td><td> Obtain the minimum value. </td></tr>
<tr><td> Aggregation.STANDARD_DEVIATION </td><td> Obtain the standard deviation. </td></tr>
<tr><td> Aggregation.TOTAL </td><td> Obtain the total value (sum). </td></tr>
<tr><td> Aggregation.VARIANCE </td><td> Obtain the variance within the rows. </td></tr>
<tr><td> Aggregation.WEIGHTED_AVERAGE </td><td> Obtain the weighted average. </td></tr>
</table>
<p>For more information, please refer to the <a href="/en/docs/GridDB_API_Reference.html">GridDB API Reference</a></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/timeseries-retrieve.zip">timeseries-retrieve.zip</a></p>
<p><br/></p>
</div>
</div>
</div>
</div>
</div>

<!-- / main -->
<?php get_footer(); ?>
