blob: dafc345667099a10d04d6dddc5a40b0acf6b2f04 [file] [log] [blame]
Ian Maxone2b799e2015-11-24 18:20:03 -08001<!DOCTYPE html>
2<!--
3 | Generated by Apache Maven Doxia at 2015-11-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="20151124" />
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="http://asterixdb.apache.org/" 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: 2015-11-24</li>
52
53
54
55 <li id="projectVersion" class="pull-right">Version: 0.8.7-incubating</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">Documentation</li>
74
75 <li>
76
77 <a href="install.html" title="Installing and Managing AsterixDB using Managix">
78 <i class="none"></i>
79 Installing and Managing AsterixDB using Managix</a>
80 </li>
81
82 <li>
83
84 <a href="yarn.html" title="Deploying AsterixDB using YARN">
85 <i class="none"></i>
86 Deploying AsterixDB using YARN</a>
87 </li>
88
89 <li>
90
91 <a href="aql/primer.html" title="AsterixDB 101: An ADM and AQL Primer">
92 <i class="none"></i>
93 AsterixDB 101: An ADM and AQL Primer</a>
94 </li>
95
96 <li>
97
98 <a href="aql/primer-sql-like.html" title="AsterixDB 101: An ADM and AQL Primer (For SQL Fans)">
99 <i class="none"></i>
100 AsterixDB 101: An ADM and AQL Primer (For SQL Fans)</a>
101 </li>
102
103 <li>
104
105 <a href="aql/js-sdk.html" title="AsterixDB Javascript SDK">
106 <i class="none"></i>
107 AsterixDB Javascript SDK</a>
108 </li>
109
110 <li>
111
112 <a href="aql/datamodel.html" title="Asterix Data Model (ADM)">
113 <i class="none"></i>
114 Asterix Data Model (ADM)</a>
115 </li>
116
117 <li>
118
119 <a href="aql/manual.html" title="Asterix Query Language (AQL)">
120 <i class="none"></i>
121 Asterix Query Language (AQL)</a>
122 </li>
123
124 <li>
125
126 <a href="aql/functions.html" title="AQL Functions">
127 <i class="none"></i>
128 AQL Functions</a>
129 </li>
130
131 <li>
132
133 <a href="aql/allens.html" title="AQL Allen's Relations Functions">
134 <i class="none"></i>
135 AQL Allen's Relations Functions</a>
136 </li>
137
138 <li>
139
140 <a href="aql/similarity.html" title="AQL Support of Similarity Queries">
141 <i class="none"></i>
142 AQL Support of Similarity Queries</a>
143 </li>
144
145 <li>
146
147 <a href="aql/externaldata.html" title="Accessing External Data">
148 <i class="none"></i>
149 Accessing External Data</a>
150 </li>
151
152 <li>
153
154 <a href="feeds/tutorial.html" title="Support for Data Ingestion in AsterixDB">
155 <i class="none"></i>
156 Support for Data Ingestion in AsterixDB</a>
157 </li>
158
159 <li>
160
161 <a href="udf.html" title="Support for User Defined Functions in AsterixDB">
162 <i class="none"></i>
163 Support for User Defined Functions in AsterixDB</a>
164 </li>
165
166 <li>
167
168 <a href="aql/filters.html" title="Filter-Based LSM Index Acceleration">
169 <i class="none"></i>
170 Filter-Based LSM Index Acceleration</a>
171 </li>
172
173 <li>
174
175 <a href="api.html" title="HTTP API to AsterixDB">
176 <i class="none"></i>
177 HTTP API to AsterixDB</a>
178 </li>
179 </ul>
180
181
182
183 <hr class="divider" />
184
185 <div id="poweredBy">
186 <div class="clear"></div>
187 <div class="clear"></div>
188 <div class="clear"></div>
189 <a href="https://code.google.com/p/hyracks/" title="Hyracks" class="builtBy">
190 <img class="builtBy" alt="Hyracks" src="images/hyrax_ts.png" />
191 </a>
192 </div>
193 </div>
194 </div>
195
196
197 <div id="bodyColumn" class="span9" >
198
199 <!-- ! Licensed to the Apache Software Foundation (ASF) under one
200 ! or more contributor license agreements. See the NOTICE file
201 ! distributed with this work for additional information
202 ! regarding copyright ownership. The ASF licenses this file
203 ! to you under the Apache License, Version 2.0 (the
204 ! "License"); you may not use this file except in compliance
205 ! with the License. You may obtain a copy of the License at
206 !
207 ! http://www.apache.org/licenses/LICENSE-2.0
208 !
209 ! Unless required by applicable law or agreed to in writing,
210 ! software distributed under the License is distributed on an
211 ! "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
212 ! KIND, either express or implied. See the License for the
213 ! specific language governing permissions and limitations
214 ! under the License.
215 ! --><h1>CSV Support in AsterixDB</h1>
216<div class="section">
217<h2><a name="Introduction_-_Defining_a_datatype_for_CSV"></a>Introduction - Defining a datatype for CSV</h2>
218<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 record 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>
219<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 records or lists, as CSV has no way to represent nested data structures. All fields in the record 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>
220<p>For the purposes of this document, we will use the following dataverse and datatype definitions:</p>
221
222<div class="source">
223<div class="source">
224<pre>drop dataverse csv if exists;
225create dataverse csv;
226use dataverse csv;
227
228create type &quot;csv_type&quot; as closed {
229 &quot;id&quot;: int32,
230 &quot;money&quot;: float,
231 &quot;name&quot;: string
232};
233
234create dataset &quot;csv_set&quot; (&quot;csv_type&quot;) primary key &quot;id&quot;;
235</pre></div></div>
236<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>
237<div class="section">
238<h2><a name="CSV_Input"></a>CSV Input</h2>
239<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>
240<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>
241
242<div class="source">
243<div class="source">
244<pre>use dataverse csv;
245
246load dataset &quot;csv_set&quot; using localfs
247((&quot;path&quot;=&quot;127.0.0.1:///tmp/my_sample.csv&quot;),
248 (&quot;format&quot;=&quot;delimited-text&quot;));
249</pre></div></div>
250<p>So, if the file <tt>/tmp/my_sample.csv</tt> contained</p>
251
252<div class="source">
253<div class="source">
254<pre>1,18.50,&quot;Peter Krabnitz&quot;
2552,74.50,&quot;Jesse Stevens&quot;
256</pre></div></div>
257<p>then the preceding query would load it into the dataset <tt>csv_set</tt>.</p>
258<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>
259
260<div class="source">
261<div class="source">
262<pre>load dataset &quot;csv_set&quot; using localfs
263((&quot;path&quot;=&quot;127.0.0.1:///tmp/my_header_sample.csv&quot;),
264 (&quot;format&quot;=&quot;delimited-text&quot;),
265 (&quot;header&quot;=&quot;true&quot;));
266</pre></div></div>
267<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>
268<div class="section">
269<h2><a name="CSV_Output"></a>CSV Output</h2>
270<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 record type which defines the schema of your CSV, and request that Asterix use the CSV output format.</p>
271<div class="section">
272<div class="section">
273<h4><a name="Output_Record_Type"></a>Output Record Type</h4>
274<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>
275
276<ol style="list-style-type: decimal">
277
278<li>Each instance must be of a record type.</li>
279
280<li>Each instance must be of the <i>same</i> record type.</li>
281
282<li>The record type must conform to the content and type restrictions mentioned in the introduction.</li>
283</ol>
284<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 record 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>
285<p>For example, the following request will ensure that all result instances are cast to the <tt>csv_type</tt> type declared earlier:</p>
286
287<div class="source">
288<div class="source">
289<pre>use dataverse csv;
290set output-record-type &quot;csv_type&quot;;
291
292for $n in dataset &quot;csv_set&quot; return $n;
293</pre></div></div>
294<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>
295<p>Two notes about <tt>output-record-type</tt>:</p>
296
297<ol style="list-style-type: decimal">
298
299<li>This feature is not strictly related to CSV; it may be used with any output formats (in which case, any record datatype may be specified, not subject to the limitations specified in the introduction of this page).</li>
300
301<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>
302</ol></div>
303<div class="section">
304<h4><a name="Request_the_CSV_Output_Format"></a>Request the CSV Output Format</h4>
305<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>
306
307<ul>
308
309<li>
310<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>
311
312<li>
313<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>
314</ul>
315<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>
316
317<div class="source">
318<div class="source">
319<pre>curl -G &quot;http://localhost:19002/query&quot; \
320 --data-urlencode 'output=CSV' \
321 --data-urlencode 'query=use dataverse csv;
322 set output-record-type &quot;csv_type&quot;;
323 for $n in dataset csv_set return $n;'
324</pre></div></div>
325<p>Alternately, the same query using the <tt>Accept</tt> header:</p>
326
327<div class="source">
328<div class="source">
329<pre>curl -G -H &quot;Accept: text/csv&quot; &quot;http://localhost:19002/query&quot; \
330 --data-urlencode 'query=use dataverse csv;
331 set output-record-type &quot;csv_type&quot;;
332 for $n in dataset csv_set return $n;'
333</pre></div></div>
334<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>
335
336<div class="source">
337<div class="source">
338<pre>import java.net.HttpURLConnection;
339import java.net.URL;
340import java.net.URLEncoder;
341import java.io.BufferedReader;
342import java.io.InputStream;
343import java.io.InputStreamReader;
344
345public class AsterixExample {
346 public static void main(String[] args) throws Exception {
347 String query = &quot;use dataverse csv; &quot; +
348 &quot;set output-record-type \&quot;csv_type\&quot;;&quot; +
349 &quot;for $n in dataset csv_set return $n&quot;;
350 URL asterix = new URL(&quot;http://localhost:19002/query?query=&quot; +
351 URLEncoder.encode(query, &quot;UTF-8&quot;));
352 HttpURLConnection conn = (HttpURLConnection) asterix.openConnection();
353 conn.setRequestProperty(&quot;Accept&quot;, &quot;text/csv&quot;);
354 BufferedReader result = new BufferedReader
355 (new InputStreamReader(conn.getInputStream()));
356 String line;
357 while ((line = result.readLine()) != null) {
358 System.out.println(line);
359 }
360 result.close();
361 }
362}
363</pre></div></div>
364<p>For either of the above examples, the output would be:</p>
365
366<div class="source">
367<div class="source">
368<pre>1,18.5,&quot;Peter Krabnitz&quot;
3692,74.5,&quot;Jesse Stevens&quot;
370</pre></div></div>
371<p>assuming you had already run the previous examples to create the dataverse and populate the dataset.</p></div>
372<div class="section">
373<h4><a name="Outputting_CSV_with_a_Header"></a>Outputting CSV with a Header</h4>
374<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>
375
376<ul>
377
378<li>
379<p>By passing the HTTP query parameter &#x201c;header&#x201d; with the value &#x201c;present&#x201d;</p></li>
380
381<li>
382<p>By specifying the MIME type {{text/csv; header=present}} in your HTTP Accept: header. This is consistent with RFC 4180.</p></li>
383</ul></div>
384<div class="section">
385<h4><a name="Issues_with_open_datatypes_and_optional_fields"></a>Issues with open datatypes and optional fields</h4>
386<p>As mentioned earlier, CSV is a rigid format. It cannot express records with different numbers of fields, which ADM allows through both open datatypes and optional fields.</p>
387<p>If your output record 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>
388<p>If your output record 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>
389<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 record types. AsterixDB allows to use an open record type only to support cases where the type already exists for other parts of your application.</p></div></div></div>
390 </div>
391 </div>
392 </div>
393
394 <hr/>
395
396 <footer>
397 <div class="container-fluid">
398 <div class="row span12">Copyright &copy; 2015
399 <a href="http://www.apache.org/">The Apache Software Foundation</a>.
400 All Rights Reserved.
401
402 </div>
403
404 <?xml version="1.0" encoding="UTF-8"?>
405<div class="row-fluid">Apache AsterixDB, AsterixDB, Apache, the Apache
406 feather logo, and the Apache AsterixDB project logo are either
407 registered trademarks or trademarks of The Apache Software
408 Foundation in the United States and other countries.
409 All other marks mentioned may be trademarks or registered
410 trademarks of their respective owners.</div>
411
412
413 </div>
414 </footer>
415 </body>
416</html>