<?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.20 Multi-Query</h1>

<h2 id="what-is-multi-query">What is Multi-Query</h2>
<p>Note: The concept of Multi-Query is described in our <a href="/en/docs/GridDB_TechnicalReference.pdf">GridDB_TechnicalReference (Section 4.7.2) </a></p>
<h2 id="create-query">Create query</h2>
<strong>List.1 Create query</strong>(MultiQuery.java)
<pre class="prettyprint linenums:x">
private static List&lt;Query&lt;?&gt;&gt; createQueries(TimeSeries&lt;InstrumentLog&gt; logTs)
		throws ParseException, GSException {
// Set TimeSeries conditions
Date start = TimestampUtils.getFormat().parse("2016-07-01T06:00:00Z");
Date end = TimestampUtils.getFormat().parse("2016-07-01T18:00:00Z");

List&lt;Query&lt;?&gt;&gt; queries = new ArrayList&lt;&gt;();

// Get Max Temperture
String maxTempertureTql = String.format(
		"SELECT MAX(temperture) WHERE"
				+ " TIMESTAMP('%s') &lt; timestamp AND timestamp &lt; TIMESTAMP('%s')",
		TimestampUtils.format(start), TimestampUtils.format(end));
Query&lt;AggregationResult&gt; maxTempertureQuery =
		logTs.query(maxTempertureTql, AggregationResult.class);
queries.add(maxTempertureQuery);

// Get Min Temperture
String minTempertureTql = String.format(
		"SELECT MIN(temperture) WHERE"
				+ " TIMESTAMP('%s') &lt; timestamp AND timestamp &lt; TIMESTAMP('%s')",
		TimestampUtils.format(start), TimestampUtils.format(end));
Query&lt;AggregationResult&gt; minTempertureQuery =
		logTs.query(minTempertureTql, AggregationResult.class);
queries.add(minTempertureQuery);

// Get Average
String avgTempertureTql = String.format(
		"SELECT AVG(temperture) WHERE"
				+ " TIMESTAMP('%s') &lt; timestamp AND timestamp &lt; TIMESTAMP('%s')",
		TimestampUtils.format(start), TimestampUtils.format(end));
Query&lt;AggregationResult&gt; avgTempertureQuery =
		logTs.query(avgTempertureTql, AggregationResult.class);
queries.add(avgTempertureQuery);

// Retrieve by time range
Query&lt;InstrumentLog&gt; timeRangeQuery = logTs.query(start, end);
queries.add(timeRangeQuery);

return queries;
}
</pre>
<h2 id="multi-query-execution">Multi-Query execution</h2>
<strong>List.2 Multi-Query execution</strong>(MultiQuery.java)
<pre class="prettyprint linenums:x">
// Create Connection
store = gridLogic.createGridStore();

// Get InstrumentLog
TimeSeries&lt;InstrumentLog&gt; logTs =
		store.getTimeSeries("weather_station_1", InstrumentLog.class);

// Create query list
List&lt;Query&lt;?&gt;&gt; queries = createQueries(logTs);

// Execute Multi Query
store.fetchAll(queries);
</pre>
<h2 id="result-of-multi-query-execution">Result of Multi-Query execution</h2>
<strong>List.3 Obtain of Multi-Query results</strong>(MultiQuery.java)
<pre class="prettyprint linenums:x">
// Retrieve reulsts
for (Query&lt;?&gt; query : queries) {
RowSet&lt;?&gt; rowSet = query.getRowSet();
while (rowSet.hasNext()) {
		Object rowObj = rowSet.next();
		if (rowObj instanceof AggregationResult) {
		// When retrieve AggregationResult
		AggregationResult aggregation = (AggregationResult) rowObj;
		System.out.println("AggregationResult:" + aggregation.getDouble());

		} else if (rowObj instanceof InstrumentLog) {
		// When retrieve InstrumentLog
		InstrumentLog log = (InstrumentLog) rowObj;
		System.out.println(String.format("%s\t%-20s\t%-10s", log.timestamp,
				log.weatherStationId, log.temperture));

		} else {
		// Do not reach in this sample
		System.out.println(rowObj);
		}
}
}
</pre>
<strong>List.4 Result of Multi-Get execution</strong>
<pre class="prettyprint linenums:x">
AggregationResult:70.0
AggregationResult:50.0
AggregationResult:60.0
Fri Jul 01 15:00:00 EDT 2016    weather_station_1       75.0
Fri Jul 01 18:00:00 EDT 2016    weather_station_1       70.0
Fri Jul 01 21:00:00 EDT 2016    weather_station_1       60.0
Sat Jul 02 00:00:00 EDT 2016    weather_station_1       50.0
Sat Jul 02 03:00:00 EDT 2016    weather_station_1       60.0
AggregationResult:70.0
AggregationResult:50.0
AggregationResult:60.0
Fri Jul 01 15:00:00 EDT 2016    weather_station_2       75.0
Fri Jul 01 18:00:00 EDT 2016    weather_station_2       70.0
Fri Jul 01 21:00:00 EDT 2016    weather_station_2       60.0
Sat Jul 02 00:00:00 EDT 2016    weather_station_2       50.0
Sat Jul 02 03:00:00 EDT 2016    weather_station_2       60.0
</pre>
<h2 id="source-code">Source Code</h2>
<p>Complete source code used in this sample can be downloaded from the following.</p>
<p><a href="img/multi-query.zip">Download:multi-query.zip</a></p>
<p><br/></p>

</div>
</div>
</div>
</div>
</div>
<!-- / main -->

<?php get_footer(); ?>
