blob: c48b993f243e04a16d76490e5c1b15a25525dc46 [file] [log] [blame]
Ian Maxon49d15b22020-12-06 16:23:00 -08001<!DOCTYPE html>
2<!--
3 | Generated by Apache Maven Doxia Site Renderer 1.8.1 from src/site/markdown/csv.md at 2020-12-06
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="20201206" />
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.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: 2020-12-06</li>
30 <li id="projectVersion" class="pull-right">Version: 0.9.6-SNAPSHOT</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 class="active"><a href="#"><span class="none"></span>CSV Output</a></li>
53 <li class="nav-header">Advanced Features</li>
54 <li><a href="aql/externaldata.html" title="Accessing External Data"><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="interval_join.html" title="Support of Interval Joins"><span class="none"></span>Support of Interval Joins</a></li>
61 <li class="nav-header">Deprecated</li>
62 <li><a href="aql/primer.html" title="AsterixDB Primer: Using AQL"><span class="none"></span>AsterixDB Primer: Using AQL</a></li>
63 <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>
64 <li><a href="aql/builtins.html" title="Queries: Builtin Functions (AQL)"><span class="none"></span>Queries: Builtin Functions (AQL)</a></li>
65</ul>
66 <hr />
67 <div id="poweredBy">
68 <div class="clear"></div>
69 <div class="clear"></div>
70 <div class="clear"></div>
71 <div class="clear"></div>
72<a href="./" title="AsterixDB" class="builtBy"><img class="builtBy" alt="AsterixDB" src="images/asterixlogo.png" /></a>
73 </div>
74 </div>
75 </div>
76 <div id="bodyColumn" class="span10" >
77<!--
78 ! Licensed to the Apache Software Foundation (ASF) under one
79 ! or more contributor license agreements. See the NOTICE file
80 ! distributed with this work for additional information
81 ! regarding copyright ownership. The ASF licenses this file
82 ! to you under the Apache License, Version 2.0 (the
83 ! "License"); you may not use this file except in compliance
84 ! with the License. You may obtain a copy of the License at
85 !
86 ! http://www.apache.org/licenses/LICENSE-2.0
87 !
88 ! Unless required by applicable law or agreed to in writing,
89 ! software distributed under the License is distributed on an
90 ! "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
91 ! KIND, either express or implied. See the License for the
92 ! specific language governing permissions and limitations
93 ! under the License.
94 !-->
95<h1>CSV Support in AsterixDB</h1>
96<div class="section">
97<h2><a name="Introduction_-_Defining_a_datatype_for_CSV"></a>Introduction - Defining a datatype for CSV</h2>
98<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>
99<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>
100<p>For the purposes of this document, we will use the following dataverse and datatype definitions:</p>
101
102<div>
103<div>
104<pre class="source">drop dataverse csv if exists;
105create dataverse csv;
106use dataverse csv;
107
108create type &quot;csv_type&quot; as closed {
109 &quot;id&quot;: int32,
110 &quot;money&quot;: float,
111 &quot;name&quot;: string
112};
113
114create dataset &quot;csv_set&quot; (&quot;csv_type&quot;) primary key &quot;id&quot;;
115</pre></div></div>
116
117<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>
118<div class="section">
119<h2><a name="CSV_Input"></a>CSV Input</h2>
120<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>
121<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>
122
123<div>
124<div>
125<pre class="source">use dataverse csv;
126
127load dataset &quot;csv_set&quot; using localfs
128((&quot;path&quot;=&quot;127.0.0.1:///tmp/my_sample.csv&quot;),
129 (&quot;format&quot;=&quot;delimited-text&quot;));
130</pre></div></div>
131
132<p>So, if the file <tt>/tmp/my_sample.csv</tt> contained</p>
133
134<div>
135<div>
136<pre class="source">1,18.50,&quot;Peter Krabnitz&quot;
1372,74.50,&quot;Jesse Stevens&quot;
138</pre></div></div>
139
140<p>then the preceding query would load it into the dataset <tt>csv_set</tt>.</p>
141<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>
142
143<div>
144<div>
145<pre class="source">load dataset &quot;csv_set&quot; using localfs
146((&quot;path&quot;=&quot;127.0.0.1:///tmp/my_header_sample.csv&quot;),
147 (&quot;format&quot;=&quot;delimited-text&quot;),
148 (&quot;header&quot;=&quot;true&quot;));
149</pre></div></div>
150
151<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>
152<div class="section">
153<h2><a name="CSV_Output"></a>CSV Output</h2>
154<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>
155<div class="section">
156<div class="section">
157<h4><a name="Output_Object_Type"></a>Output Object Type</h4>
158<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>
159<ol style="list-style-type: decimal">
160
161<li>Each instance must be of a object type.</li>
162<li>Each instance must be of the <i>same</i> object type.</li>
163<li>The object type must conform to the content and type restrictions mentioned in the introduction.</li>
164</ol>
165<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>
166<p>For example, the following request will ensure that all result instances are cast to the <tt>csv_type</tt> type declared earlier:</p>
167
168<div>
169<div>
170<pre class="source">use dataverse csv;
171set output-record-type &quot;csv_type&quot;;
172
173for $n in dataset &quot;csv_set&quot; return $n;
174</pre></div></div>
175
176<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>
177<p>Two notes about <tt>output-record-type</tt>:</p>
178<ol style="list-style-type: decimal">
179
180<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>
181<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>
182</ol></div>
183<div class="section">
184<h4><a name="Request_the_CSV_Output_Format"></a>Request the CSV Output Format</h4>
185<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>
186<ul>
187
188<li>
189
190<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>
191</li>
192<li>
193
194<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>
195</li>
196</ul>
197<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>
198
199<div>
200<div>
201<pre class="source">curl -G &quot;http://localhost:19002/query&quot; \
202 --data-urlencode 'output=CSV' \
203 --data-urlencode 'query=use dataverse csv;
204 set output-record-type &quot;csv_type&quot;;
205 for $n in dataset csv_set return $n;'
206</pre></div></div>
207
208<p>Alternately, the same query using the <tt>Accept</tt> header:</p>
209
210<div>
211<div>
212<pre class="source">curl -G -H &quot;Accept: text/csv&quot; &quot;http://localhost:19002/query&quot; \
213 --data-urlencode 'query=use dataverse csv;
214 set output-record-type &quot;csv_type&quot;;
215 for $n in dataset csv_set return $n;'
216</pre></div></div>
217
218<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>
219
220<div>
221<div>
222<pre class="source">import java.net.HttpURLConnection;
223import java.net.URL;
224import java.net.URLEncoder;
225import java.io.BufferedReader;
226import java.io.InputStream;
227import java.io.InputStreamReader;
228
229public class AsterixExample {
230 public static void main(String[] args) throws Exception {
231 String query = &quot;use dataverse csv; &quot; +
232 &quot;set output-record-type \&quot;csv_type\&quot;;&quot; +
233 &quot;for $n in dataset csv_set return $n&quot;;
234 URL asterix = new URL(&quot;http://localhost:19002/query?query=&quot; +
235 URLEncoder.encode(query, &quot;UTF-8&quot;));
236 HttpURLConnection conn = (HttpURLConnection) asterix.openConnection();
237 conn.setRequestProperty(&quot;Accept&quot;, &quot;text/csv&quot;);
238 BufferedReader result = new BufferedReader
239 (new InputStreamReader(conn.getInputStream()));
240 String line;
241 while ((line = result.readLine()) != null) {
242 System.out.println(line);
243 }
244 result.close();
245 }
246}
247</pre></div></div>
248
249<p>For either of the above examples, the output would be:</p>
250
251<div>
252<div>
253<pre class="source">1,18.5,&quot;Peter Krabnitz&quot;
2542,74.5,&quot;Jesse Stevens&quot;
255</pre></div></div>
256
257<p>assuming you had already run the previous examples to create the dataverse and populate the dataset.</p></div>
258<div class="section">
259<h4><a name="Outputting_CSV_with_a_Header"></a>Outputting CSV with a Header</h4>
260<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>
261<ul>
262
263<li>
264
265<p>By passing the HTTP query parameter &#x201c;header&#x201d; with the value &#x201c;present&#x201d;</p>
266</li>
267<li>
268
269<p>By specifying the MIME type {{text/csv; header=present}} in your HTTP Accept: header. This is consistent with RFC 4180.</p>
270</li>
271</ul></div>
272<div class="section">
273<h4><a name="Issues_with_open_datatypes_and_optional_fields"></a>Issues with open datatypes and optional fields</h4>
274<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>
275<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>
276<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>
277<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>
278 </div>
279 </div>
280 </div>
281 <hr/>
282 <footer>
283 <div class="container-fluid">
284 <div class="row-fluid">
285<div class="row-fluid">Apache AsterixDB, AsterixDB, Apache, the Apache
286 feather logo, and the Apache AsterixDB project logo are either
287 registered trademarks or trademarks of The Apache Software
288 Foundation in the United States and other countries.
289 All other marks mentioned may be trademarks or registered
290 trademarks of their respective owners.
291 </div>
292 </div>
293 </div>
294 </footer>
295 </body>
296</html>