blob: c10e1af8644b3b237b94d529d6b29f8600f20328 [file] [log] [blame]
Ian Maxon100cb802017-04-24 18:48:07 -07001<!DOCTYPE html>
2<!--
3 | Generated by Apache Maven Doxia at 2017-04-24
4 | Rendered using Apache Maven Fluido Skin 1.3.0
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="20170424" />
11 <meta http-equiv="Content-Language" content="en" />
12 <title>AsterixDB &#x2013; CSV Support in AsterixDB</title>
13 <link rel="stylesheet" href="./css/apache-maven-fluido-1.3.0.min.css" />
14 <link rel="stylesheet" href="./css/site.css" />
15 <link rel="stylesheet" href="./css/print.css" media="print" />
16
17
18 <script type="text/javascript" src="./js/apache-maven-fluido-1.3.0.min.js"></script>
19
20
21
22<script>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
23 (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
24 m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
25 })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
26
27 ga('create', 'UA-41536543-1', 'uci.edu');
28 ga('send', 'pageview');</script>
29
30 </head>
31 <body class="topBarDisabled">
32
33
34
35
36 <div class="container-fluid">
37 <div id="banner">
38 <div class="pull-left">
39 <a href="./" id="bannerLeft">
40 <img src="images/asterixlogo.png" alt="AsterixDB"/>
41 </a>
42 </div>
43 <div class="pull-right"> </div>
44 <div class="clear"><hr/></div>
45 </div>
46
47 <div id="breadcrumbs">
48 <ul class="breadcrumb">
49
50
51 <li id="publishDate">Last Published: 2017-04-24</li>
52
53
54
55 <li id="projectVersion" class="pull-right">Version: 0.9.1</li>
56
57 <li class="divider pull-right">|</li>
58
59 <li class="pull-right"> <a href="index.html" title="Documentation Home">
60 Documentation Home</a>
61 </li>
62
63 </ul>
64 </div>
65
66
67 <div class="row-fluid">
68 <div id="leftColumn" class="span3">
69 <div class="well sidebar-nav">
70
71
72 <ul class="nav nav-list">
73 <li class="nav-header">Get Started - Installation</li>
74
75 <li>
76
77 <a href="ncservice.html" title="Option 1: using NCService">
78 <i class="none"></i>
79 Option 1: using NCService</a>
80 </li>
81
82 <li>
83
84 <a href="ansible.html" title="Option 2: using Ansible">
85 <i class="none"></i>
86 Option 2: using Ansible</a>
87 </li>
88
89 <li>
90
91 <a href="aws.html" title="Option 3: using Amazon Web Services">
92 <i class="none"></i>
93 Option 3: using Amazon Web Services</a>
94 </li>
95
96 <li>
97
98 <a href="yarn.html" title="Option 4: using YARN">
99 <i class="none"></i>
100 Option 4: using YARN</a>
101 </li>
102
103 <li>
104
105 <a href="install.html" title="Option 5: using Managix (deprecated)">
106 <i class="none"></i>
107 Option 5: using Managix (deprecated)</a>
108 </li>
109 <li class="nav-header">AsterixDB Primer</li>
110
111 <li>
112
113 <a href="sqlpp/primer-sqlpp.html" title="Option 1: using SQL++">
114 <i class="none"></i>
115 Option 1: using SQL++</a>
116 </li>
117
118 <li>
119
120 <a href="aql/primer.html" title="Option 2: using AQL">
121 <i class="none"></i>
122 Option 2: using AQL</a>
123 </li>
124 <li class="nav-header">Data Model</li>
125
126 <li>
127
128 <a href="datamodel.html" title="The Asterix Data Model">
129 <i class="none"></i>
130 The Asterix Data Model</a>
131 </li>
132 <li class="nav-header">Queries - SQL++</li>
133
134 <li>
135
136 <a href="sqlpp/manual.html" title="The SQL++ Query Language">
137 <i class="none"></i>
138 The SQL++ Query Language</a>
139 </li>
140
141 <li>
142
143 <a href="sqlpp/builtins.html" title="Builtin Functions">
144 <i class="none"></i>
145 Builtin Functions</a>
146 </li>
147 <li class="nav-header">Queries - AQL</li>
148
149 <li>
150
151 <a href="aql/manual.html" title="The Asterix Query Language (AQL)">
152 <i class="none"></i>
153 The Asterix Query Language (AQL)</a>
154 </li>
155
156 <li>
157
158 <a href="aql/builtins.html" title="Builtin Functions">
159 <i class="none"></i>
160 Builtin Functions</a>
161 </li>
162 <li class="nav-header">API/SDK</li>
163
164 <li>
165
166 <a href="api.html" title="HTTP API">
167 <i class="none"></i>
168 HTTP API</a>
169 </li>
170
171 <li class="active">
172
173 <a href="#"><i class="none"></i>CSV Output</a>
174 </li>
175 <li class="nav-header">Advanced Features</li>
176
177 <li>
178
179 <a href="aql/fulltext.html" title="Support of Full-text Queries">
180 <i class="none"></i>
181 Support of Full-text Queries</a>
182 </li>
183
184 <li>
185
186 <a href="aql/externaldata.html" title="Accessing External Data">
187 <i class="none"></i>
188 Accessing External Data</a>
189 </li>
190
191 <li>
192
193 <a href="feeds/tutorial.html" title="Support for Data Ingestion">
194 <i class="none"></i>
195 Support for Data Ingestion</a>
196 </li>
197
198 <li>
199
200 <a href="udf.html" title="User Defined Functions">
201 <i class="none"></i>
202 User Defined Functions</a>
203 </li>
204
205 <li>
206
207 <a href="aql/filters.html" title="Filter-Based LSM Index Acceleration">
208 <i class="none"></i>
209 Filter-Based LSM Index Acceleration</a>
210 </li>
211
212 <li>
213
214 <a href="aql/similarity.html" title="Support of Similarity Queries">
215 <i class="none"></i>
216 Support of Similarity Queries</a>
217 </li>
218 </ul>
219
220
221
222 <hr class="divider" />
223
224 <div id="poweredBy">
225 <div class="clear"></div>
226 <div class="clear"></div>
227 <div class="clear"></div>
228 <a href="./" title="AsterixDB" class="builtBy">
229 <img class="builtBy" alt="AsterixDB" src="images/asterixlogo.png" />
230 </a>
231 </div>
232 </div>
233 </div>
234
235
236 <div id="bodyColumn" class="span9" >
237
238 <!-- ! Licensed to the Apache Software Foundation (ASF) under one
239 ! or more contributor license agreements. See the NOTICE file
240 ! distributed with this work for additional information
241 ! regarding copyright ownership. The ASF licenses this file
242 ! to you under the Apache License, Version 2.0 (the
243 ! "License"); you may not use this file except in compliance
244 ! with the License. You may obtain a copy of the License at
245 !
246 ! http://www.apache.org/licenses/LICENSE-2.0
247 !
248 ! Unless required by applicable law or agreed to in writing,
249 ! software distributed under the License is distributed on an
250 ! "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
251 ! KIND, either express or implied. See the License for the
252 ! specific language governing permissions and limitations
253 ! under the License.
254 ! --><h1>CSV Support in AsterixDB</h1>
255<div class="section">
256<h2><a name="Introduction_-_Defining_a_datatype_for_CSV"></a>Introduction - Defining a datatype for CSV</h2>
257<p>AsterixDB supports the CSV format for both data input and query result output. In both cases, the structure of the CSV data must be defined using a named ADM object datatype. The CSV format, limitations, and MIME type are defined by <a class="externalLink" href="https://tools.ietf.org/html/rfc4180">RFC 4180</a>.</p>
258<p>CSV is not as expressive as the full Asterix Data Model, meaning that not all data which can be represented in ADM can also be represented as CSV. So the form of this datatype is limited. First, obviously it may not contain any nested objects or lists, as CSV has no way to represent nested data structures. All fields in the object type must be primitive. Second, the set of supported primitive types is limited to numerics (<tt>int8</tt>, <tt>int16</tt>, <tt>int32</tt>, <tt>int64</tt>, <tt>float</tt>, <tt>double</tt>) and <tt>string</tt>. On output, a few additional primitive types (<tt>boolean</tt>, datetime types) are supported and will be represented as strings.</p>
259<p>For the purposes of this document, we will use the following dataverse and datatype definitions:</p>
260
261<div class="source">
262<div class="source">
263<pre>drop dataverse csv if exists;
264create dataverse csv;
265use dataverse csv;
266
267create type &quot;csv_type&quot; as closed {
268 &quot;id&quot;: int32,
269 &quot;money&quot;: float,
270 &quot;name&quot;: string
271};
272
273create dataset &quot;csv_set&quot; (&quot;csv_type&quot;) primary key &quot;id&quot;;
274</pre></div></div>
275<p>Note: There is no explicit restriction against using an open datatype for CSV purposes, and you may have optional fields in the datatype (eg., <tt>id: int32?</tt>). However, the CSV format itself is rigid, so using either of these datatype features introduces possible failure modes on output which will be discussed below.</p></div>
276<div class="section">
277<h2><a name="CSV_Input"></a>CSV Input</h2>
278<p>CSV data may be loaded into a dataset using the normal &#x201c;load dataset&#x201d; mechanisms, utilizing the builtin &#x201c;delimited-text&#x201d; format. See <a href="aql/externaldata.html">Accessing External Data</a> for more details. Note that comma is the default value for the &#x201c;delimiter&#x201d; parameter, so it does not need to be explicitly specified.</p>
279<p>In this case, the datatype used to interpret the CSV data is the datatype associated with the dataset being loaded. So, to load a file that we have stored locally on the NC into our example dataset:</p>
280
281<div class="source">
282<div class="source">
283<pre>use dataverse csv;
284
285load dataset &quot;csv_set&quot; using localfs
286((&quot;path&quot;=&quot;127.0.0.1:///tmp/my_sample.csv&quot;),
287 (&quot;format&quot;=&quot;delimited-text&quot;));
288</pre></div></div>
289<p>So, if the file <tt>/tmp/my_sample.csv</tt> contained</p>
290
291<div class="source">
292<div class="source">
293<pre>1,18.50,&quot;Peter Krabnitz&quot;
2942,74.50,&quot;Jesse Stevens&quot;
295</pre></div></div>
296<p>then the preceding query would load it into the dataset <tt>csv_set</tt>.</p>
297<p>If your CSV file has a header (that is, the first line contains a set of field names, rather than actual data), you can instruct Asterix to ignore this header by adding the parameter <tt>&quot;header&quot;=&quot;true&quot;</tt>, eg.</p>
298
299<div class="source">
300<div class="source">
301<pre>load dataset &quot;csv_set&quot; using localfs
302((&quot;path&quot;=&quot;127.0.0.1:///tmp/my_header_sample.csv&quot;),
303 (&quot;format&quot;=&quot;delimited-text&quot;),
304 (&quot;header&quot;=&quot;true&quot;));
305</pre></div></div>
306<p>CSV data may also be loaded from HDFS; see <a href="aql/externaldata.html">Accessing External Data</a> for details. However please note that CSV files on HDFS cannot have headers. Attempting to specify &#x201c;header&#x201d;=&#x201c;true&#x201d; when reading from HDFS could result in non-header lines of data being skipped as well.</p></div>
307<div class="section">
308<h2><a name="CSV_Output"></a>CSV Output</h2>
309<p>Any query may be rendered as CSV when using AsterixDB&#x2019;s HTTP interface. To do so, there are two steps required: specify the object type which defines the schema of your CSV, and request that Asterix use the CSV output format.</p>
310<div class="section">
311<div class="section">
312<h4><a name="Output_Object_Type"></a>Output Object Type</h4>
313<p>Background: The result of any AQL query is an unordered list of <i>instances</i>, where each <i>instance</i> is an instance of an AQL datatype. When requesting CSV output, there are some restrictions on the legal datatypes in this unordered list due to the limited expressability of CSV:</p>
314
315<ol style="list-style-type: decimal">
316
317<li>Each instance must be of a object type.</li>
318
319<li>Each instance must be of the <i>same</i> object type.</li>
320
321<li>The object type must conform to the content and type restrictions mentioned in the introduction.</li>
322</ol>
323<p>While it would be possible to structure your query to cast all result instances to a given type, it is not necessary. AQL offers a built-in feature which will automatically cast all top-level instances in the result to a specified named ADM object type. To enable this feature, use a <tt>set</tt> statement prior to the query to set the parameter <tt>output-record-type</tt> to the name of an ADM type. This type must have already been defined in the current dataverse.</p>
324<p>For example, the following request will ensure that all result instances are cast to the <tt>csv_type</tt> type declared earlier:</p>
325
326<div class="source">
327<div class="source">
328<pre>use dataverse csv;
329set output-record-type &quot;csv_type&quot;;
330
331for $n in dataset &quot;csv_set&quot; return $n;
332</pre></div></div>
333<p>In this case the casting is redundant since by definition every value in <tt>csv_set</tt> is already of type <tt>csv_type</tt>. But consider a more complex query where the result values are created by joining fields from different underlying datasets, etc.</p>
334<p>Two notes about <tt>output-record-type</tt>:</p>
335
336<ol style="list-style-type: decimal">
337
338<li>This feature is not strictly related to CSV; it may be used with any output formats (in which case, any object datatype may be specified, not subject to the limitations specified in the introduction of this page).</li>
339
340<li>When the CSV output format is requested, <tt>output-record-type</tt> is in fact required, not optional. This is because the type is used to determine the field names for the CSV header and to ensure that the ordering of fields in the output is consistent (which is obviously vital for the CSV to make any sense).</li>
341</ol></div>
342<div class="section">
343<h4><a name="Request_the_CSV_Output_Format"></a>Request the CSV Output Format</h4>
344<p>When sending requests to the Asterix HTTP API, Asterix decides what format to use for rendering the results in one of two ways:</p>
345
346<ul>
347
348<li>
349<p>A HTTP query parameter named &#x201c;output&#x201d;, which must be set to one of the following values: <tt>JSON</tt>, <tt>CSV</tt>, or <tt>ADM</tt>.</p></li>
350
351<li>
352<p>Based on the <a class="externalLink" href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1"><tt>Accept</tt> HTTP header</a></p></li>
353</ul>
354<p>By default, Asterix will produce JSON output. To select CSV output, pass the parameter <tt>output=CSV</tt>, or set the <tt>Accept</tt> header on your request to the MIME type <tt>text/csv</tt>. The details of how to accomplish this will of course depend on what tools you are using to contact the HTTP API. Here is an example from a Unix shell prompt using the command-line utility &#x201c;curl&#x201d; and specifying the &quot;output query parameter:</p>
355
356<div class="source">
357<div class="source">
358<pre>curl -G &quot;http://localhost:19002/query&quot; \
359 --data-urlencode 'output=CSV' \
360 --data-urlencode 'query=use dataverse csv;
361 set output-record-type &quot;csv_type&quot;;
362 for $n in dataset csv_set return $n;'
363</pre></div></div>
364<p>Alternately, the same query using the <tt>Accept</tt> header:</p>
365
366<div class="source">
367<div class="source">
368<pre>curl -G -H &quot;Accept: text/csv&quot; &quot;http://localhost:19002/query&quot; \
369 --data-urlencode 'query=use dataverse csv;
370 set output-record-type &quot;csv_type&quot;;
371 for $n in dataset csv_set return $n;'
372</pre></div></div>
373<p>Similarly, a trivial Java program to execute the above sample query and selecting CSV output via the <tt>Accept</tt> header would be:</p>
374
375<div class="source">
376<div class="source">
377<pre>import java.net.HttpURLConnection;
378import java.net.URL;
379import java.net.URLEncoder;
380import java.io.BufferedReader;
381import java.io.InputStream;
382import java.io.InputStreamReader;
383
384public class AsterixExample {
385 public static void main(String[] args) throws Exception {
386 String query = &quot;use dataverse csv; &quot; +
387 &quot;set output-record-type \&quot;csv_type\&quot;;&quot; +
388 &quot;for $n in dataset csv_set return $n&quot;;
389 URL asterix = new URL(&quot;http://localhost:19002/query?query=&quot; +
390 URLEncoder.encode(query, &quot;UTF-8&quot;));
391 HttpURLConnection conn = (HttpURLConnection) asterix.openConnection();
392 conn.setRequestProperty(&quot;Accept&quot;, &quot;text/csv&quot;);
393 BufferedReader result = new BufferedReader
394 (new InputStreamReader(conn.getInputStream()));
395 String line;
396 while ((line = result.readLine()) != null) {
397 System.out.println(line);
398 }
399 result.close();
400 }
401}
402</pre></div></div>
403<p>For either of the above examples, the output would be:</p>
404
405<div class="source">
406<div class="source">
407<pre>1,18.5,&quot;Peter Krabnitz&quot;
4082,74.5,&quot;Jesse Stevens&quot;
409</pre></div></div>
410<p>assuming you had already run the previous examples to create the dataverse and populate the dataset.</p></div>
411<div class="section">
412<h4><a name="Outputting_CSV_with_a_Header"></a>Outputting CSV with a Header</h4>
413<p>By default, AsterixDB will produce CSV results with no header line. If you want a header, you may explicitly request it in one of two ways:</p>
414
415<ul>
416
417<li>
418<p>By passing the HTTP query parameter &#x201c;header&#x201d; with the value &#x201c;present&#x201d;</p></li>
419
420<li>
421<p>By specifying the MIME type {{text/csv; header=present}} in your HTTP Accept: header. This is consistent with RFC 4180.</p></li>
422</ul></div>
423<div class="section">
424<h4><a name="Issues_with_open_datatypes_and_optional_fields"></a>Issues with open datatypes and optional fields</h4>
425<p>As mentioned earlier, CSV is a rigid format. It cannot express objects with different numbers of fields, which ADM allows through both open datatypes and optional fields.</p>
426<p>If your output object type contains optional fields, this will not result in any errors. If the output data of a query does not contain values for an optional field, this will be represented in CSV as <tt>null</tt>.</p>
427<p>If your output object type is open, this will also not result in any errors. If the output data of a query contains any open fields, the corresponding rows in the resulting CSV will contain more comma-separated values than the others. On each such row, the data from the closed fields in the type will be output first in the normal order, followed by the data from the open fields in an arbitrary order.</p>
428<p>According to RFC 4180 this is not strictly valid CSV (Section 2, rule 4, &#x201c;Each line <i>should</i> contain the same number of fields throughout the file&#x201d;). Hence it will likely not be handled consistently by all CSV processors. Some may throw a parsing error. If you attempt to load this data into AsterixDB later using <tt>load dataset</tt>, the extra fields will be silently ignored. For this reason it is recommended that you use only closed datatypes as output object types. AsterixDB allows to use an open object type only to support cases where the type already exists for other parts of your application.</p></div></div></div>
429 </div>
430 </div>
431 </div>
432
433 <hr/>
434
435 <footer>
436 <div class="container-fluid">
437 <div class="row span12">Copyright &copy; 2017
438 <a href="https://www.apache.org/">The Apache Software Foundation</a>.
439 All Rights Reserved.
440
441 </div>
442
443 <?xml version="1.0" encoding="UTF-8"?>
444<div class="row-fluid">Apache AsterixDB, AsterixDB, Apache, the Apache
445 feather logo, and the Apache AsterixDB project logo are either
446 registered trademarks or trademarks of The Apache Software
447 Foundation in the United States and other countries.
448 All other marks mentioned may be trademarks or registered
449 trademarks of their respective owners.</div>
450
451
452 </div>
453 </footer>
454 </body>
455</html>