blob: 0aff26d562a52498ed903c9c1cc3374b66daa5be [file] [log] [blame]
Ian Maxonbf8620b2024-04-01 16:09:18 -07001<!DOCTYPE html>
2<!--
3 | Generated by Apache Maven Doxia Site Renderer 1.8.1 from src/site/markdown/aql/externaldata.md at 2024-04-01
4 | Rendered using Apache Maven Fluido Skin 1.7
5-->
6<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
7 <head>
8 <meta charset="UTF-8" />
9 <meta name="viewport" content="width=device-width, initial-scale=1.0" />
10 <meta name="Date-Revision-yyyymmdd" content="20240401" />
11 <meta http-equiv="Content-Language" content="en" />
12 <title>AsterixDB &#x2013; Accessing External Data in AsterixDB</title>
13 <link rel="stylesheet" href="../css/apache-maven-fluido-1.7.min.css" />
14 <link rel="stylesheet" href="../css/site.css" />
15 <link rel="stylesheet" href="../css/print.css" media="print" />
16 <script type="text/javascript" src="../js/apache-maven-fluido-1.7.min.js"></script>
17
18 </head>
19 <body class="topBarDisabled">
20 <div class="container-fluid">
21 <div id="banner">
22 <div class="pull-left"><a href=".././" id="bannerLeft"><img src="../images/asterixlogo.png" alt="AsterixDB"/></a></div>
23 <div class="pull-right"></div>
24 <div class="clear"><hr/></div>
25 </div>
26
27 <div id="breadcrumbs">
28 <ul class="breadcrumb">
29 <li id="publishDate">Last Published: 2024-04-01</li>
30 <li id="projectVersion" class="pull-right">Version: 0.9.9</li>
31 <li class="pull-right"><a href="../index.html" title="Documentation Home">Documentation Home</a></li>
32 </ul>
33 </div>
34 <div class="row-fluid">
35 <div id="leftColumn" class="span2">
36 <div class="well sidebar-nav">
37 <ul class="nav nav-list">
38 <li class="nav-header">Get Started - Installation</li>
39 <li><a href="../ncservice.html" title="Option 1: using NCService"><span class="none"></span>Option 1: using NCService</a></li>
40 <li><a href="../ansible.html" title="Option 2: using Ansible"><span class="none"></span>Option 2: using Ansible</a></li>
41 <li><a href="../aws.html" title="Option 3: using Amazon Web Services"><span class="none"></span>Option 3: using Amazon Web Services</a></li>
42 <li class="nav-header">AsterixDB Primer</li>
43 <li><a href="../sqlpp/primer-sqlpp.html" title="Using SQL++"><span class="none"></span>Using SQL++</a></li>
44 <li class="nav-header">Data Model</li>
45 <li><a href="../datamodel.html" title="The Asterix Data Model"><span class="none"></span>The Asterix Data Model</a></li>
46 <li class="nav-header">Queries</li>
47 <li><a href="../sqlpp/manual.html" title="The SQL++ Query Language"><span class="none"></span>The SQL++ Query Language</a></li>
48 <li><a href="../SQLPP.html" title="Raw SQL++ Grammar"><span class="none"></span>Raw SQL++ Grammar</a></li>
49 <li><a href="../sqlpp/builtins.html" title="Builtin Functions"><span class="none"></span>Builtin Functions</a></li>
50 <li class="nav-header">API/SDK</li>
51 <li><a href="../api.html" title="HTTP API"><span class="none"></span>HTTP API</a></li>
52 <li><a href="../csv.html" title="CSV Output"><span class="none"></span>CSV Output</a></li>
53 <li class="nav-header">Advanced Features</li>
54 <li class="active"><a href="#"><span class="none"></span>Accessing External Data</a></li>
55 <li><a href="../feeds.html" title="Data Ingestion with Feeds"><span class="none"></span>Data Ingestion with Feeds</a></li>
56 <li><a href="../udf.html" title="User Defined Functions"><span class="none"></span>User Defined Functions</a></li>
57 <li><a href="../sqlpp/filters.html" title="Filter-Based LSM Index Acceleration"><span class="none"></span>Filter-Based LSM Index Acceleration</a></li>
58 <li><a href="../sqlpp/fulltext.html" title="Support of Full-text Queries"><span class="none"></span>Support of Full-text Queries</a></li>
59 <li><a href="../sqlpp/similarity.html" title="Support of Similarity Queries"><span class="none"></span>Support of Similarity Queries</a></li>
60 <li><a href="../geo/quickstart.html" title="GIS Support Overview"><span class="none"></span>GIS Support Overview</a></li>
61 <li><a href="../geo/functions.html" title="GIS Functions"><span class="none"></span>GIS Functions</a></li>
62 <li><a href="../interval_join.html" title="Support of Interval Joins"><span class="none"></span>Support of Interval Joins</a></li>
63 <li><a href="../spatial_join.html" title="Support of Spatial Joins"><span class="none"></span>Support of Spatial Joins</a></li>
64 <li><a href="../sqlpp/arrayindex.html" title="Support of Array Indexes"><span class="none"></span>Support of Array Indexes</a></li>
65 <li class="nav-header">Deprecated</li>
66 <li><a href="../aql/primer.html" title="AsterixDB Primer: Using AQL"><span class="none"></span>AsterixDB Primer: Using AQL</a></li>
67 <li><a href="../aql/manual.html" title="Queries: The Asterix Query Language (AQL)"><span class="none"></span>Queries: The Asterix Query Language (AQL)</a></li>
68 <li><a href="../aql/builtins.html" title="Queries: Builtin Functions (AQL)"><span class="none"></span>Queries: Builtin Functions (AQL)</a></li>
69</ul>
70 <hr />
71 <div id="poweredBy">
72 <div class="clear"></div>
73 <div class="clear"></div>
74 <div class="clear"></div>
75 <div class="clear"></div>
76<a href=".././" title="AsterixDB" class="builtBy"><img class="builtBy" alt="AsterixDB" src="../images/asterixlogo.png" /></a>
77 </div>
78 </div>
79 </div>
80 <div id="bodyColumn" class="span10" >
81<!--
82 ! Licensed to the Apache Software Foundation (ASF) under one
83 ! or more contributor license agreements. See the NOTICE file
84 ! distributed with this work for additional information
85 ! regarding copyright ownership. The ASF licenses this file
86 ! to you under the Apache License, Version 2.0 (the
87 ! "License"); you may not use this file except in compliance
88 ! with the License. You may obtain a copy of the License at
89 !
90 ! http://www.apache.org/licenses/LICENSE-2.0
91 !
92 ! Unless required by applicable law or agreed to in writing,
93 ! software distributed under the License is distributed on an
94 ! "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
95 ! KIND, either express or implied. See the License for the
96 ! specific language governing permissions and limitations
97 ! under the License.
98 !-->
99<h1>Accessing External Data in AsterixDB</h1>
100<div class="section">
101<h2><a name="Table_of_Contents"></a><a name="toc" id="toc">Table of Contents</a></h2>
102<ul>
103
104<li><a href="#Introduction">Introduction</a></li>
105<li><a href="#IntroductionAdapterForAnExternalDataset">Adapter for an External Dataset</a></li>
106<li><a href="#BuiltinAdapters">Builtin Adapters</a></li>
107<li><a href="#IntroductionCreatingAnExternalDataset">Creating an External Dataset</a></li>
108<li><a href="#WritingQueriesAgainstAnExternalDataset">Writing Queries against an External Dataset</a></li>
109<li><a href="#BuildingIndexesOverExternalDatasets">Building Indexes over External Datasets</a></li>
110<li><a href="#ExternalDataSnapshot">External Data Snapshots</a></li>
111<li><a href="#FAQ">Frequently Asked Questions</a></li>
112</ul></div>
113<div class="section">
114<h2><a name="Introduction_.5BBack_to_TOC.5D"></a><a name="Introduction" id="Introduction">Introduction</a> <font size="4"><a href="#toc">[Back to TOC]</a></font></h2>
115<p>Data that needs to be processed by AsterixDB could be residing outside AsterixDB storage. Examples include data files on a distributed file system such as HDFS or on the local file system of a machine that is part of an AsterixDB cluster. For AsterixDB to process such data, an end-user may create a regular dataset in AsterixDB (a.k.a. an internal dataset) and load the dataset with the data. AsterixDB also supports &#x2018;&#x2018;external datasets&#x2019;&#x2019; so that it is not necessary to &#x201c;load&#x201d; all data prior to using it. This also avoids creating multiple copies of data and the need to keep the copies in sync.</p>
116<div class="section">
117<h3><a name="Adapter_for_an_External_Dataset_.5BBack_to_TOC.5D"></a><a name="IntroductionAdapterForAnExternalDataset" id="IntroductionAdapterForAnExternalDataset">Adapter for an External Dataset</a> <font size="4"><a href="#toc">[Back to TOC]</a></font></h3>
118<p>External data is accessed using wrappers (adapters in AsterixDB) that abstract away the mechanism of connecting with an external service, receiving its data and transforming the data into ADM objects that are understood by AsterixDB. AsterixDB comes with built-in adapters for common storage systems such as HDFS or the local file system.</p></div>
119<div class="section">
120<h3><a name="Builtin_Adapters_.5BBack_to_TOC.5D"></a><a name="BuiltinAdapters" id="BuiltinAdapters">Builtin Adapters</a> <font size="4"><a href="#toc">[Back to TOC]</a></font></h3>
121<p>AsterixDB offers a set of builtin adapters that can be used to query external data or for loading data into an internal dataset using a load statement or a data feed. Each adapter requires specifying the <tt>format</tt> of the data in order to be able to parse objects correctly. Using adapters with feeds, the parameter <tt>output-type</tt> must also be specified.</p>
122<p>Following is a listing of existing built-in adapters and their configuration parameters:</p>
123<ol style="list-style-type: decimal">
124
125<li><b><i>localfs</i></b>: used for reading data stored in a local filesystem in one or more of the node controllers
126<ul>
127
128<li><tt>path</tt>: A fully qualified path of the form <tt>host://absolute_path</tt>. Comma separated list if there are multiple directories or files</li>
129<li><tt>expression</tt>: A <a class="externalLink" href="https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html">regular expression</a> to match and filter against file names</li>
130</ul>
131</li>
132<li><b><i>hdfs</i></b>: used for reading data stored in an HDFS instance
133<ul>
134
135<li><tt>path</tt>: A fully qualified path of the form <tt>host://absolute_path</tt>. Comma separated list if there are multiple directories or files</li>
136<li><tt>expression</tt>: A <a class="externalLink" href="https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html">regular expression</a> to match and filter against file names</li>
137<li><tt>input-format</tt>: A fully qualified name or an alias for a class of HDFS input format</li>
138<li><tt>hdfs</tt>: The HDFS name node URL</li>
139</ul>
140</li>
141<li><b><i>socket</i></b>: used for listening to connections that sends data streams through one or more sockets
142<ul>
143
144<li><tt>sockets</tt>: comma separated list of sockets to listen to</li>
145<li><tt>address-type</tt>: either IP if the list uses IP addresses, or NC if the list uses NC names</li>
146</ul>
147</li>
148<li><b><i>socket_client</i></b>: used for connecting to one or more sockets and reading data streams
149<ul>
150
151<li><tt>sockets</tt>: comma separated list of sockets to connect to</li>
152</ul>
153</li>
154<li><b><i>twitter_push</i></b>: used for establishing a connection and subscribing to a twitter feed
155<ul>
156
157<li><tt>consumer.key</tt>: access parameter provided by twitter OAuth</li>
158<li><tt>consumer.secret</tt>: access parameter provided by twitter OAuth</li>
159<li><tt>access.token</tt>: access parameter provided by twitter OAuth</li>
160<li><tt>access.token.secret</tt>: access parameter provided by twitter OAuth</li>
161</ul>
162</li>
163<li><b><i>twitter_pull</i></b>: used for polling a twitter feed for tweets based on a configurable frequency
164<ul>
165
166<li><tt>consumer.key</tt>: access parameter provided by twitter OAuth</li>
167<li><tt>consumer.secret</tt>: access parameter provided by twitter OAuth</li>
168<li><tt>access.token</tt>: access parameter provided by twitter OAuth</li>
169<li><tt>access.token.secret</tt>: access parameter provided by twitter OAuth</li>
170<li><tt>query</tt>: twitter query string</li>
171<li><tt>interval</tt>: poll interval in seconds</li>
172</ul>
173</li>
174<li><b><i>rss</i></b>: used for reading RSS feed
175<ul>
176
177<li><tt>url</tt>: a comma separated list of RSS urls</li>
178</ul>
179</li>
180</ol></div>
181<div class="section">
182<h3><a name="Creating_an_External_Dataset_.5BBack_to_TOC.5D"></a><a name="IntroductionCreatingAnExternalDataset" id="IntroductionCreatingAnExternalDataset">Creating an External Dataset</a> <font size="4"><a href="#toc">[Back to TOC]</a></font></h3>
183<p>As an example we consider the Lineitem dataset from the <a class="externalLink" href="http://www.openlinksw.com/dataspace/doc/dav/wiki/Main/VOSTPCHLinkedData/tpch.sql">TPCH schema</a>. We assume that you have successfully created an AsterixDB instance following the instructions at <a href="../install.html">Installing AsterixDB Using Managix</a>. <i>For constructing an example, we assume a single machine setup..</i></p>
184<p>Similar to a regular dataset, an external dataset has an associated datatype. We shall first create the datatype associated with each object in Lineitem data. Paste the following in the query textbox on the webpage at <a class="externalLink" href="http://127.0.0.1:19001">http://127.0.0.1:19001</a> and hit &#x2018;Execute&#x2019;.</p>
185
186<div>
187<div>
188<pre class="source"> create dataverse ExternalFileDemo;
189 use dataverse ExternalFileDemo;
190
191 create type LineitemType as closed {
192 l_orderkey:int32,
193 l_partkey: int32,
194 l_suppkey: int32,
195 l_linenumber: int32,
196 l_quantity: double,
197 l_extendedprice: double,
198 l_discount: double,
199 l_tax: double,
200 l_returnflag: string,
201 l_linestatus: string,
202 l_shipdate: string,
203 l_commitdate: string,
204 l_receiptdate: string,
205 l_shipinstruct: string,
206 l_shipmode: string,
207 l_comment: string}
208</pre></div></div>
209
210<p>Here, we describe two scenarios.</p>
211<div class="section">
212<h4><a name="a1.29_Data_file_resides_on_the_local_file_system_of_a_host"></a>1) Data file resides on the local file system of a host</h4>
213<p>Prerequisite: The host is a part of the ASTERIX cluster.</p>
214<p>Earlier, we assumed a single machine ASTERIX setup. To satisfy the prerequisite, log-in to the machine running ASTERIX.</p>
215<ul>
216
217<li>Download the <a href="../data/lineitem.tbl">data file</a> to an appropriate location. We denote this location by SOURCE_PATH.</li>
218</ul>
219<p>ASTERIX provides a built-in adapter for data residing on the local file system. The adapter is referred by its alias- &#x2018;localfs&#x2019;. We create an external dataset named Lineitem and use the &#x2018;localfs&#x2019; adapter.</p>
220
221<div>
222<div>
223<pre class="source"> create external dataset Lineitem(LineitemType)
224 using localfs
225</pre></div></div>
226
227<p>Above, the definition is not complete as we need to provide a set of parameters that are specific to the source file.</p>
228
229<table border="0" class="table table-striped">
230
231<tr class="a">
232
233<td> Parameter </td>
234
235<td> Description </td>
236</tr>
237
238<tr class="b">
239
240<td> path </td>
241
242<td> A fully qualified path of the form <tt>host://&lt;absolute path&gt;</tt>.
243 Use a comma separated list if there are multiple files.
244 E.g. <tt>host1://&lt;absolute path&gt;</tt>, <tt>host2://&lt;absolute path&gt;</tt> and so forth. </td>
245</tr>
246
247<tr class="a">
248
249<td> format </td>
250
251<td> The format for the content. Use 'adm' for data in ADM (ASTERIX Data Model) or <a class="externalLink" href="http://www.json.org/">JSON</a> format. Use 'delimited-text' if fields are separated by a delimiting character (eg., CSV). </td></tr>
252
253<tr class="b">
254
255<td>delimiter</td>
256
257<td>The delimiting character in the source file if format is 'delimited text'</td>
258</tr>
259</table>
260
261<p>As we are using a single single machine ASTERIX instance, we use 127.0.0.1 as host in the path parameter. We <i>complete the create dataset statement</i> as follows.</p>
262
263<div>
264<div>
265<pre class="source"> use dataverse ExternalFileDemo;
266
267 create external dataset Lineitem(LineitemType)
268 using localfs
269 ((&quot;path&quot;=&quot;127.0.0.1://SOURCE_PATH&quot;),
270 (&quot;format&quot;=&quot;delimited-text&quot;),
271 (&quot;delimiter&quot;=&quot;|&quot;));
272</pre></div></div>
273
274<p>Please substitute SOURCE_PATH with the absolute path to the source file on the local file system.</p></div>
275<div class="section">
276<h4><a name="Common_source_of_error"></a>Common source of error</h4>
277<p>An incorrect value for the path parameter will give the following exception message when the dataset is used in a query.</p>
278
279<div>
280<div>
281<pre class="source"> org.apache.hyracks.algebricks.common.exceptions.AlgebricksException: org.apache.hyracks.api.exceptions.HyracksDataException: org.apache.hyracks.api.exceptions.HyracksDataException: Job failed.
282</pre></div></div>
283
284<p>Verify the correctness of the path parameter provided to the localfs adapter. Note that the path parameter must be an absolute path to the data file. For e.g. if you saved your file in your home directory (assume it to be /home/joe), then the path value should be</p>
285
286<div>
287<div>
288<pre class="source"> 127.0.0.1:///home/joe/lineitem.tbl.
289</pre></div></div>
290
291<p>In your web-browser, navigate to 127.0.0.1:19001 and paste the above to the query text box. Finally hit &#x2018;Execute&#x2019;.</p>
292<p>Next we move over to the the section <a href="#Writing_Queries_against_an_External_Dataset">Writing Queries against an External Dataset</a> and try a sample query against the external dataset.</p></div>
293<div class="section">
294<h4><a name="a2.29_Data_file_resides_on_an_HDFS_instance"></a>2) Data file resides on an HDFS instance</h4>
295<p>rerequisite: It is required that the Namenode and HDFS Datanodes are reachable from the hosts that form the AsterixDB cluster. AsterixDB provides a built-in adapter for data residing on HDFS. The HDFS adapter can be referred (in AQL) by its alias - &#x2018;hdfs&#x2019;. We can create an external dataset named Lineitem and associate the HDFS adapter with it as follows;</p>
296
297<div>
298<div>
299<pre class="source"> create external dataset Lineitem(LineitemType)
300 using hdfs((&#x201c;hdfs&#x201d;:&#x201d;hdfs://localhost:54310&#x201d;),(&#x201c;path&#x201d;:&#x201d;/asterix/Lineitem.tbl&#x201d;),...,(&#x201c;input- format&#x201d;:&#x201d;rc-format&#x201d;));
301</pre></div></div>
302
303<p>The expected parameters are described below:</p>
304
305<table border="0" class="table table-striped">
306
307<tr class="a">
308
309<td> Parameter </td>
310
311<td> Description </td>
312</tr>
313
314<tr class="b">
315
316<td> hdfs </td>
317
318<td> The HDFS URL </td>
319</tr>
320
321<tr class="a">
322
323<td> path </td>
324
325<td> The absolute path to the source HDFS file or directory. Use a comma separated list if there are multiple files or directories. </td></tr>
326
327<tr class="b">
328
329<td> input-format </td>
330
331<td> The associated input format. Use 'text-input-format' for text files , 'sequence-input-format' for hadoop sequence files, 'rc-input-format' for Hadoop Object Columnar files, or a fully qualified name of an implementation of org.apache.hadoop.mapred.InputFormat. </td>
332</tr>
333
334<tr class="a">
335
336<td> format </td>
337
338<td> The format of the input content. Use 'adm' for text data in ADM (ASTERIX Data Model) or <a class="externalLink" href="http://www.json.org/">JSON</a> format, 'delimited-text' for text delimited data that has fields separated by a delimiting character, 'binary' for other data.</td>
339</tr>
340
341<tr class="b">
342
343<td> delimiter </td>
344
345<td> The delimiting character in the source file if format is 'delimited text' </td>
346</tr>
347
348<tr class="a">
349
350<td> parser </td>
351
352<td> The parser used to parse HDFS objects if the format is 'binary'. Use 'hive- parser' for data deserialized by a Hive Serde (AsterixDB can understand deserialized Hive objects) or a fully qualified class name of user- implemented parser that implements the interface org.apache.asterix.external.input.InputParser. </td>
353</tr>
354
355<tr class="b">
356
357<td> hive-serde </td>
358
359<td> The Hive serde is used to deserialize HDFS objects if format is binary and the parser is hive-parser. Use a fully qualified name of a class implementation of org.apache.hadoop.hive.serde2.SerDe. </td>
360</tr>
361
362<tr class="a">
363
364<td> local-socket-path </td>
365
366<td> The UNIX domain socket path if local short-circuit reads are enabled in the HDFS instance</td>
367</tr>
368</table>
369
370<p><i>Difference between &#x2018;input-format&#x2019; and &#x2018;format&#x2019;</i></p>
371<p><i>input-format</i>: Files stored under HDFS have an associated storage format. For example, TextInputFormat represents plain text files. SequenceFileInputFormat indicates binary compressed files. RCFileInputFormat corresponds to objects stored in a object columnar fashion. The parameter &#x2018;input-format&#x2019; is used to distinguish between these and other HDFS input formats.</p>
372<p><i>format</i>: The parameter &#x2018;format&#x2019; refers to the type of the data contained in the file. For example, data contained in a file could be in json or ADM format, could be in delimited-text with fields separated by a delimiting character or could be in binary format.</p>
373<p>As an example. consider the <a href="../data/lineitem.tbl">data file</a>. The file is a text file with each line representing a object. The fields in each object are separated by the &#x2018;|&#x2019; character.</p>
374<p>We assume the HDFS URL to be <a class="externalLink" href="hdfs://localhost:54310">hdfs://localhost:54310</a>. We further assume that the example data file is copied to HDFS at a path denoted by &#x201c;/asterix/Lineitem.tbl&#x201d;.</p>
375<p>The complete set of parameters for our example file are as follows. ((&#x201c;hdfs&#x201d;=&#x201c;<a class="externalLink" href="hdfs://localhost:54310”,(“path”=“/asterix/Lineitem.tbl”),(“input-format”=“text-">hdfs://localhost:54310&#x201d;,(&#x201c;path&#x201d;=&#x201c;/asterix/Lineitem.tbl&#x201d;),(&#x201c;input-format&#x201d;=&#x201c;text-</a> input-format&#x201d;),(&#x201c;format&#x201d;=&#x201c;delimited-text&#x201d;),(&#x201c;delimiter&#x201d;=&#x201c;|&#x201d;))</p></div>
376<div class="section">
377<h4><a name="Using_the_Hive_Parser"></a>Using the Hive Parser</h4>
378<p>if a user wants to create an external dataset that uses hive-parser to parse HDFS objects, it is important that the datatype associated with the dataset matches the actual data in the Hive table for the correct initialization of the Hive SerDe. Here is the conversion from the supported Hive data types to AsterixDB data types:</p>
379
380<table border="0" class="table table-striped">
381
382<tr class="a">
383
384<td> Hive </td>
385
386<td> AsterixDB </td>
387</tr>
388
389<tr class="b">
390
391<td>BOOLEAN</td>
392
393<td>Boolean</td>
394</tr>
395
396<tr class="a">
397
398<td>BYTE(TINY INT)</td>
399
400<td>Int8</td>
401</tr>
402
403<tr class="b">
404
405<td>DOUBLE</td>
406
407<td>Double</td>
408</tr>
409
410<tr class="a">
411
412<td>FLOAT</td>
413
414<td>Float</td>
415</tr>
416
417<tr class="b">
418
419<td>INT</td>
420
421<td>Int32</td>
422</tr>
423
424<tr class="a">
425
426<td>LONG(BIG INT)</td>
427
428<td>Int64</td>
429</tr>
430
431<tr class="b">
432
433<td>SHORT(SMALL INT)</td>
434
435<td>Int16</td>
436</tr>
437
438<tr class="a">
439
440<td>STRING</td>
441
442<td>String</td>
443</tr>
444
445<tr class="b">
446
447<td>TIMESTAMP</td>
448
449<td>Datetime</td>
450</tr>
451
452<tr class="a">
453
454<td>DATE</td>
455
456<td>Date</td>
457</tr>
458
459<tr class="b">
460
461<td>STRUCT</td>
462
463<td>Nested Object</td>
464</tr>
465
466<tr class="a">
467
468<td>LIST</td>
469
470<td>OrderedList or UnorderedList</td>
471</tr>
472</table>
473</div>
474<div class="section">
475<h4><a name="Examples_of_dataset_definitions_for_external_datasets"></a>Examples of dataset definitions for external datasets</h4>
476<p><i>Example 1</i>: We can modify the create external dataset statement as follows:</p>
477
478<div>
479<div>
480<pre class="source"> create external dataset Lineitem('LineitemType)
481 using hdfs((&quot;hdfs&quot;=&quot;hdfs://localhost:54310&quot;),(&quot;path&quot;=&quot;/asterix/Lineitem.tbl&quot;),(&quot;input-format&quot;=&quot;text- input-format&quot;),(&quot;format&quot;=&quot;delimited-text&quot;),(&quot;delimiter&quot;=&quot;|&quot;));
482</pre></div></div>
483
484<p><i>Example 2</i>: Here, we create an external dataset of lineitem objects stored in sequence files that has content in ADM format:</p>
485
486<div>
487<div>
488<pre class="source"> create external dataset Lineitem('LineitemType)
489 using hdfs((&quot;hdfs&quot;=&quot;hdfs://localhost:54310&quot;),(&quot;path&quot;=&quot;/asterix/SequenceLineitem.tbl&quot;),(&quot;input- format&quot;=&quot;sequence-input-format&quot;),(&quot;format&quot;=&quot;adm&quot;));
490</pre></div></div>
491
492<p><i>Example 3</i>: Here, we create an external dataset of lineitem objects stored in object-columnar files that has content in binary format parsed using hive-parser with hive ColumnarSerde:</p>
493
494<div>
495<div>
496<pre class="source"> create external dataset Lineitem('LineitemType)
497 using hdfs((&quot;hdfs&quot;=&quot;hdfs://localhost:54310&quot;),(&quot;path&quot;=&quot;/asterix/RCLineitem.tbl&quot;),(&quot;input-format&quot;=&quot;rc-input-format&quot;),(&quot;format&quot;=&quot;binary&quot;),(&quot;parser&quot;=&quot;hive-parser&quot;),(&quot;hive- serde&quot;=&quot;org.apache.hadoop.hive.serde2.columnar.ColumnarSerde&quot;));
498</pre></div></div>
499</div></div></div>
500<div class="section">
501<h2><a name="Writing_Queries_against_an_External_Dataset_.5BBack_to_TOC.5D"></a><a name="WritingQueriesAgainstAnExternalDataset" id="WritingQueriesAgainstAnExternalDataset">Writing Queries against an External Dataset</a> <font size="4"><a href="#toc">[Back to TOC]</a></font></h2>
502<p>You may write AQL queries against an external dataset in exactly the same way that queries are written against internal datasets. The following is an example of an AQL query that applies a filter and returns an ordered result.</p>
503
504<div>
505<div>
506<pre class="source"> use dataverse ExternalFileDemo;
507
508 for $c in dataset('Lineitem')
509 where $c.l_orderkey &lt;= 3
510 order by $c.l_orderkey, $c.l_linenumber
511 return $c
512</pre></div></div>
513</div>
514<div class="section">
515<h2><a name="Building_Indexes_over_External_Datasets_.5BBack_to_TOC.5D"></a><a name="BuildingIndexesOverExternalDatasets" id="BuildingIndexesOverExternalDatasets">Building Indexes over External Datasets</a> <font size="4"><a href="#toc">[Back to TOC]</a></font></h2>
516<p>AsterixDB supports building B-Tree and R-Tree indexes over static data stored in the Hadoop Distributed File System. To create an index, first create an external dataset over the data as follows</p>
517
518<div>
519<div>
520<pre class="source"> create external dataset Lineitem(LineitemType)
521 using hdfs((&quot;hdfs&quot;=&quot;hdfs://localhost:54310&quot;),(&quot;path&quot;=&quot;/asterix/Lineitem.tbl&quot;),(&quot;input-format&quot;=&quot;text-input- format&quot;),(&quot;format&quot;=&quot;delimited-text&quot;),(&quot;delimiter&quot;=&quot;|&quot;));
522</pre></div></div>
523
524<p>You can then create a B-Tree index on this dataset instance as if the dataset was internally stored as follows:</p>
525
526<div>
527<div>
528<pre class="source"> create index PartkeyIdx on Lineitem(l_partkey);
529</pre></div></div>
530
531<p>You could also create an R-Tree index as follows:</p>
532
533<div>
534<div>
535<pre class="source"> &#xfffc;create index IndexName on DatasetName(attribute-name) type rtree;
536</pre></div></div>
537
538<p>After building the indexes, the AsterixDB query compiler can use them to access the dataset and answer queries in a more cost effective manner. AsterixDB can read all HDFS input formats, but indexes over external datasets can currently be built only for HDFS datasets with &#x2018;text-input-format&#x2019;, &#x2018;sequence-input-format&#x2019; or &#x2018;rc-input-format&#x2019;.</p></div>
539<div class="section">
540<h2><a name="External_Data_Snapshots_.5BBack_to_TOC.5D"></a><a name="ExternalDataSnapshots" id="ExternalDataSnapshots">External Data Snapshots</a> <font size="4"><a href="#toc">[Back to TOC]</a></font></h2>
541<p>An external data snapshot represents the status of a dataset&#x2019;s files in HDFS at a point in time. Upon creating the first index over an external dataset, AsterixDB captures and stores a snapshot of the dataset in HDFS. Only objects present at the snapshot capture time are indexed, and any additional indexes created afterwards will only contain data that was present at the snapshot capture time thus preserving consistency across all indexes of a dataset. To update all indexes of an external dataset and advance the snapshot time to be the present time, a user can use the refresh external dataset command as follows:</p>
542
543<div>
544<div>
545<pre class="source"> refresh external dataset DatasetName;
546</pre></div></div>
547
548<p>After a refresh operation commits, all of the dataset&#x2019;s indexes will reflect the status of the data as of the new snapshot capture time.</p></div>
549<div class="section">
550<h2><a name="Frequently_Asked_Questions_.5BBack_to_TOC.5D"></a><a name="FAQ" id="FAQ">Frequently Asked Questions</a> <font size="4"><a href="#toc">[Back to TOC]</a></font></h2>
551<p>Q. I added data to my dataset in HDFS, Will the dataset indexes in AsterixDB be updated automatically?</p>
552<p>A. No, you must use the refresh external dataset statement to make the indexes aware of any changes in the dataset files in HDFS.</p>
553<p>Q. Why doesn&#x2019;t AsterixDB update external indexes automatically?</p>
554<p>A. Since external data is managed by other users/systems with mechanisms that are system dependent, AsterixDB has no way of knowing exactly when data is added or deleted in HDFS, so the responsibility of refreshing indexes are left to the user. A user can use internal datasets for which AsterixDB manages the data and its indexes.</p>
555<p>Q. I created an index over an external dataset and then added some data to my HDFS dataset. Will a query that uses the index return different results from a query that doesn&#x2019;t use the index?</p>
556<p>A. No, queries&#x2019; results are access path independent and the stored snapshot is used to determines which data are going to be included when processing queries.</p>
557<p>Q. I created an index over an external dataset and then deleted some of my dataset&#x2019;s files in HDFS, Will indexed data access still return the objects in deleted files?</p>
558<p>A. No. When AsterixDB accesses external data, with or without the use of indexes, it only access files present in the file system at runtime.</p>
559<p>Q. I submitted a refresh command on a an external dataset and a failure occurred, What has happened to my indexes?</p>
560<p>A. External Indexes Refreshes are treated as a single transaction. In case of a failure, a rollback occurs and indexes are restored to their previous state. An error message with the cause of failure is returned to the user.</p>
561<p>Q. I was trying to refresh an external dataset while some queries were accessing the data using index access method. Will the queries be affected by the refresh operation?</p>
562<p>A. Queries have access to external dataset indexes state at the time where the queries are submitted. A query that was submitted before a refresh commits will only access data under the snapshot taken before the refresh; queries that are submitted after the refresh commits will access data under the snapshot taken after the refresh.</p>
563<p>Q. What happens when I try to create an additional index while a refresh operation is in progress or vice versa?</p>
564<p>A. The create index operation will wait until the refresh commits or aborts and then the index will be built according to the external data snapshot at the end of the refresh operation. Creating indexes and refreshing datasets are mutually exclusive operations and will not be run in parallel. Multiple indexes can be created in parallel, but not multiple refresh operations.</p></div>
565 </div>
566 </div>
567 </div>
568 <hr/>
569 <footer>
570 <div class="container-fluid">
571 <div class="row-fluid">
572<div class="row-fluid">Apache AsterixDB, AsterixDB, Apache, the Apache
573 feather logo, and the Apache AsterixDB project logo are either
574 registered trademarks or trademarks of The Apache Software
575 Foundation in the United States and other countries.
576 All other marks mentioned may be trademarks or registered
577 trademarks of their respective owners.
578 </div>
579 </div>
580 </div>
581 </footer>
582 </body>
583</html>