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