blob: 3e0c503382a57fb4851240c041f30fddee0b60f6 [file] [log] [blame]
Ian Maxon41b806c2019-03-07 15:58:20 -08001<!DOCTYPE html>
2<!--
3 | Generated by Apache Maven Doxia Site Renderer 1.8.1 from src/site/markdown/feeds/tutorial.md at 2019-03-07
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="20190307" />
11 <meta http-equiv="Content-Language" content="en" />
12 <title>AsterixDB &#x2013; Support for Data Ingestion 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: 2019-03-07</li>
30 <li id="projectVersion" class="pull-right">Version: 0.9.4.1</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="Option 1: using SQL++"><span class="none"></span>Option 1: using SQL++</a></li>
44 <li><a href="../aql/primer.html" title="Option 2: using AQL"><span class="none"></span>Option 2: using AQL</a></li>
45 <li class="nav-header">Data Model</li>
46 <li><a href="../datamodel.html" title="The Asterix Data Model"><span class="none"></span>The Asterix Data Model</a></li>
47 <li class="nav-header">Queries - SQL++</li>
48 <li><a href="../sqlpp/manual.html" title="The SQL++ Query Language"><span class="none"></span>The SQL++ Query Language</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">Queries - AQL</li>
51 <li><a href="../aql/manual.html" title="The Asterix Query Language (AQL)"><span class="none"></span>The Asterix Query Language (AQL)</a></li>
52 <li><a href="../aql/builtins.html" title="Builtin Functions"><span class="none"></span>Builtin Functions</a></li>
53 <li class="nav-header">API/SDK</li>
54 <li><a href="../api.html" title="HTTP API"><span class="none"></span>HTTP API</a></li>
55 <li><a href="../csv.html" title="CSV Output"><span class="none"></span>CSV Output</a></li>
56 <li class="nav-header">Advanced Features</li>
57 <li><a href="../aql/fulltext.html" title="Support of Full-text Queries"><span class="none"></span>Support of Full-text Queries</a></li>
58 <li><a href="../aql/externaldata.html" title="Accessing External Data"><span class="none"></span>Accessing External Data</a></li>
59 <li class="active"><a href="#"><span class="none"></span>Support for Data Ingestion</a></li>
60 <li><a href="../udf.html" title="User Defined Functions"><span class="none"></span>User Defined Functions</a></li>
61 <li><a href="../aql/filters.html" title="Filter-Based LSM Index Acceleration"><span class="none"></span>Filter-Based LSM Index Acceleration</a></li>
62 <li><a href="../aql/similarity.html" title="Support of Similarity Queries"><span class="none"></span>Support of Similarity Queries</a></li>
63</ul>
64 <hr />
65 <div id="poweredBy">
66 <div class="clear"></div>
67 <div class="clear"></div>
68 <div class="clear"></div>
69 <div class="clear"></div>
70<a href=".././" title="AsterixDB" class="builtBy"><img class="builtBy" alt="AsterixDB" src="../images/asterixlogo.png" /></a>
71 </div>
72 </div>
73 </div>
74 <div id="bodyColumn" class="span10" >
75<!--
76 ! Licensed to the Apache Software Foundation (ASF) under one
77 ! or more contributor license agreements. See the NOTICE file
78 ! distributed with this work for additional information
79 ! regarding copyright ownership. The ASF licenses this file
80 ! to you under the Apache License, Version 2.0 (the
81 ! "License"); you may not use this file except in compliance
82 ! with the License. You may obtain a copy of the License at
83 !
84 ! http://www.apache.org/licenses/LICENSE-2.0
85 !
86 ! Unless required by applicable law or agreed to in writing,
87 ! software distributed under the License is distributed on an
88 ! "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
89 ! KIND, either express or implied. See the License for the
90 ! specific language governing permissions and limitations
91 ! under the License.
92 !-->
93<h1>Support for Data Ingestion in AsterixDB</h1>
94<div class="section">
95<h2><a name="Table_of_Contents"></a><a name="atoc" id="#toc">Table of Contents</a></h2>
96<ul>
97
98<li><a href="#Introduction">Introduction</a></li>
99<li><a href="#FeedAdapters">Feed Adapters</a></li>
100<li><a href="#FeedPolicies">Feed Policies</a></li>
101</ul></div>
102<div class="section">
103<h2><a name="Introduction">Introduction</a></h2>
104<p>In this document, we describe the support for data ingestion in AsterixDB. Data feeds are a new mechanism for having continuous data arrive into a BDMS from external sources and incrementally populate a persisted dataset and associated indexes. We add a new BDMS architectural component, called a data feed, that makes a Big Data system the caretaker for functionality that used to live outside, and we show how it improves users&#x2019; lives and system performance.</p></div>
105<div class="section">
106<h2><a name="Feed_Adapters"></a><a name="FeedAdapters">Feed Adapters</a></h2>
107<p>The functionality of establishing a connection with a data source and receiving, parsing and translating its data into ADM objects (for storage inside AsterixDB) is contained in a feed adapter. A feed adapter is an implementation of an interface and its details are specific to a given data source. An adapter may optionally be given parameters to configure its runtime behavior. Depending upon the data transfer protocol/APIs offered by the data source, a feed adapter may operate in a push or a pull mode. Push mode involves just one initial request by the adapter to the data source for setting up the connection. Once a connection is authorized, the data source &#x201c;pushes&#x201d; data to the adapter without any subsequent requests by the adapter. In contrast, when operating in a pull mode, the adapter makes a separate request each time to receive data. AsterixDB currently provides built-in adapters for several popular data sources such as Twitter and RSS feeds. AsterixDB additionally provides a generic socket-based adapter that can be used to ingest data that is directed at a prescribed socket.</p>
108<p>In this tutorial, we shall describe building two example data ingestion pipelines that cover the popular scenarios of ingesting data from (a) Twitter (b) RSS (c) Socket Feed source.</p>
109<div class="section">
110<div class="section">
111<h4><a name="Ingesting_Twitter_Stream"></a>Ingesting Twitter Stream</h4>
112<p>We shall use the built-in push-based Twitter adapter. As a pre-requisite, we must define a Tweet using the AsterixDB Data Model (ADM) and the query language SQL++. Given below are the type definitions in SQL++ that create a Tweet datatype which is representative of a real tweet as obtained from Twitter.</p>
113
114<div>
115<div>
116<pre class="source"> drop dataverse feeds if exists;
117
118 create dataverse feeds;
119 use feeds;
120
121 create type TwitterUser as closed {
122 screen_name: string,
123 lang: string,
124 friends_count: int32,
125 statuses_count: int32
126 };
127
128 create type Tweet as open {
129 id: int64,
130 user: TwitterUser
131 };
132
133 create dataset Tweets (Tweet) primary key id;
134</pre></div></div>
135
136<p>We also create a dataset that we shall use to persist the tweets in AsterixDB. Next we make use of the <tt>create feed</tt> SQL++ statement to define our example data feed.</p>
137<div class="section">
138<h5><a name="Using_the_.E2.80.9Cpush_twitter.E2.80.9D_feed_adapter"></a>Using the &#x201c;push_twitter&#x201d; feed adapter</h5>
139<p>The &#x201c;push_twitter&#x201d; adapter requires setting up an application account with Twitter. To retrieve tweets, Twitter requires registering an application. Registration involves providing a name and a brief description for the application. Each application has associated OAuth authentication credentials that include OAuth keys and tokens. Accessing the Twitter API requires providing the following.</p>
140<ol style="list-style-type: decimal">
141
142<li>Consumer Key (API Key)</li>
143<li>Consumer Secret (API Secret)</li>
144<li>Access Token</li>
145<li>Access Token Secret</li>
146</ol>
147<p>The &#x201c;push_twitter&#x201d; adapter takes as configuration the above mentioned parameters. End users are required to obtain the above authentication credentials prior to using the &#x201c;push_twitter&#x201d; adapter. For further information on obtaining OAuth keys and tokens and registering an application with Twitter, please visit <a class="externalLink" href="http://apps.twitter.com">http://apps.twitter.com</a></p>
148<p>Given below is an example SQL++ statement that creates a feed called &#x201c;TwitterFeed&#x201d; by using the &#x201c;push_twitter&#x201d; adapter.</p>
149
150<div>
151<div>
152<pre class="source"> use feeds;
153
154 create feed TwitterFeed with {
155 &quot;adapter-name&quot;: &quot;push_twitter&quot;,
156 &quot;type-name&quot;: &quot;Tweet&quot;,
157 &quot;format&quot;: &quot;twitter-status&quot;,
158 &quot;consumer.key&quot;: &quot;************&quot;,
159 &quot;consumer.secret&quot;: &quot;************&quot;,
160 &quot;access.token&quot;: &quot;**********&quot;,
161 &quot;access.token.secret&quot;: &quot;*************&quot;
162 };
163</pre></div></div>
164
165<p>It is required that the above authentication parameters are provided valid. Note that the <tt>create feed</tt> statement does not initiate the flow of data from Twitter into the AsterixDB instance. Instead, the <tt>create feed</tt> statement only results in registering the feed with the instance. The flow of data along a feed is initiated when it is connected to a target dataset using the connect feed statement and activated using the start feed statement.</p>
166<p>The Twitter adapter also supports several Twitter streaming APIs as follow:</p>
167<ol style="list-style-type: decimal">
168
169<li>Track filter <tt>&quot;keywords&quot;: &quot;AsterixDB, Apache&quot;</tt></li>
170<li>Locations filter <tt>&quot;locations&quot;: &quot;-29.7, 79.2, 36.7, 72.0; -124.848974,-66.885444, 24.396308, 49.384358&quot;</tt></li>
171<li>Language filter <tt>&quot;language&quot;: &quot;en&quot;</tt></li>
172<li>Filter level <tt>&quot;filter-level&quot;: &quot;low&quot;</tt></li>
173</ol>
174<p>An example of Twitter adapter tracking tweets with keyword &#x201c;news&#x201d; can be described using following ddl:</p>
175
176<div>
177<div>
178<pre class="source"> use feeds;
179
180 create feed TwitterFeed with {
181 &quot;adapter-name&quot;: &quot;push_twitter&quot;,
182 &quot;type-name&quot;: &quot;Tweet&quot;,
183 &quot;format&quot;: &quot;twitter-status&quot;,
184 &quot;consumer.key&quot;: &quot;************&quot;,
185 &quot;consumer.secret&quot;: &quot;************&quot;,
186 &quot;access.token&quot;: &quot;**********&quot;,
187 &quot;access.token.secret&quot;: &quot;*************&quot;,
188 &quot;keywords&quot;: &quot;news&quot;
189 };
190</pre></div></div>
191
192<p>For more details about these APIs, please visit <a class="externalLink" href="https://dev.twitter.com/streaming/overview/request-parameters">https://dev.twitter.com/streaming/overview/request-parameters</a></p></div></div>
193<div class="section">
194<h4><a name="Lifecycle_of_a_Feed"></a>Lifecycle of a Feed</h4>
195<p>A feed is a logical artifact that is brought to life (i.e., its data flow is initiated) only when it is activated using the <tt>start feed</tt> statement. Before we active a feed, we need to designate the dataset where the data to be persisted using <tt>connect feed</tt> statement. Subsequent to a <tt>connect feed</tt> statement, the feed is said to be in the connected state. After that, <tt>start feed</tt> statement will activate the feed, and start the dataflow from feed to its connected dataset. Multiple feeds can simultaneously be connected to a dataset such that the contents of the dataset represent the union of the connected feeds. Also one feed can be simultaneously connected to multiple target datasets.</p>
196
197<div>
198<div>
199<pre class="source"> use feeds;
200
201 connect feed TwitterFeed to dataset Tweets;
202
203 start feed TwitterFeed;
204</pre></div></div>
205
206<p>The <tt>connect feed</tt> statement above directs AsterixDB to persist the data from <tt>TwitterFeed</tt> feed into the <tt>Tweets</tt> dataset. The <tt>start feed</tt> statement will activate the feed and start the dataflow. If it is required (by the high-level application) to also retain the raw tweets obtained from Twitter, the end user may additionally choose to connect TwitterFeed to a different dataset.</p>
207<p>Let the feed run for a minute, then run the following query to see the latest tweets that are stored into the data set.</p>
208
209<div>
210<div>
211<pre class="source"> use feeds;
212
213 select * from Tweets limit 10;
214</pre></div></div>
215
216<p>The dataflow of data from a feed can be terminated explicitly by <tt>stop feed</tt> statement.</p>
217
218<div>
219<div>
220<pre class="source"> use feeds;
221
222 stop feed TwitterFeed;
223</pre></div></div>
224
225<p>The <tt>disconnnect statement</tt> can be used to disconnect the feed from certain dataset.</p>
226
227<div>
228<div>
229<pre class="source"> use feeds;
230
231 disconnect feed TwitterFeed from dataset Tweets;
232</pre></div></div>
233</div></div>
234<div class="section">
235<h3><a name="Ingesting_with_Other_Adapters"></a>Ingesting with Other Adapters</h3>
236<p>AsterixDB has several builtin feed adapters for data ingestion. User can also implement their own adapters and plug them into AsterixDB. Here we introduce <tt>socket_adapter</tt> and <tt>localfs</tt> feed adapter that cover most of the common application scenarios.</p>
237<div class="section">
238<div class="section">
239<h5><a name="Using_the_.E2.80.9Csocket_adapter.E2.80.9D_feed_adapter"></a>Using the &#x201c;socket_adapter&#x201d; feed adapter</h5>
240<p><tt>socket_adapter</tt> feed opens a web socket on the given node which allows user to push data into AsterixDB directly. Here is an example:</p>
241
242<div>
243<div>
244<pre class="source"> drop dataverse feeds if exists;
245 create dataverse feeds;
246 use feeds;
247
248 create type TestDataType as open {
249 screenName: string
250 };
251
252 create dataset TestDataset(TestDataType) primary key screenName;
253
254 create feed TestSocketFeed with {
255 &quot;adapter-name&quot;: &quot;socket_adapter&quot;,
256 &quot;sockets&quot;: &quot;127.0.0.1:10001&quot;,
257 &quot;address-type&quot;: &quot;IP&quot;,
258 &quot;type-name&quot;: &quot;TestDataType&quot;,
259 &quot;format&quot;: &quot;adm&quot;
260 };
261
262 connect feed TestSocketFeed to dataset TestDataset;
263
264 use feeds;
265 start feed TestSocketFeed;
266</pre></div></div>
267
268<p>The above statements create a socket feed which is listening to &#x201c;10001&#x201d; port of the host machine. This feed accepts data records in &#x201c;adm&#x201d; format. As an example, you can download the sample dataset <a href="../data/chu.adm">Chirp Users</a> and push them line by line into the socket feed using any socket client you like. Following is a socket client example in Python:</p>
269
270<div>
271<div>
272<pre class="source"> from socket import socket
273
274 ip = '127.0.0.1'
275 port1 = 10001
276 filePath = 'chu.adm'
277
278 sock1 = socket()
279 sock1.connect((ip, port1))
280
281 with open(filePath) as inputData:
282 for line in inputData:
283 sock1.sendall(line)
284 sock1.close()
285</pre></div></div>
286</div></div>
287<div class="section">
288<h4><a name="Using_the_.E2.80.9Clocalfs.E2.80.9D_feed_adapter"></a>Using the &#x201c;localfs&#x201d; feed adapter</h4>
289<p><tt>localfs</tt> adapter enables data ingestion from local file system. It allows user to feed data records on local disk into a dataset. A DDL example for creating a <tt>localfs</tt> feed is given as follow:</p>
290
291<div>
292<div>
293<pre class="source"> use feeds;
294
295 create type TestDataType as open {
296 screenName: string
297 };
298
299 create dataset TestDataset(TestDataType) primary key screenName;
300
301 create feed TestFileFeed with {
302 &quot;adapter-name&quot;: &quot;localfs&quot;,
303 &quot;type-name&quot;: &quot;TestDataType&quot;,
304 &quot;path&quot;: &quot;HOSTNAME://LOCAL_FILE_PATH&quot;,
305 &quot;format&quot;: &quot;adm&quot;
306 };
307
308 connect feed TestFileFeed to dataset TestDataset;
309
310 start feed TestFileFeed;
311</pre></div></div>
312
313<p>Similar to previous examples, we need to define the datatype and dataset this feed uses. The &#x201c;path&#x201d; parameter refers to the local data file that we want to ingest data from. <tt>HOSTNAME</tt> can either be the IP address or node name of the machine which holds the file. <tt>LOCAL_FILE_PATH</tt> indicates the absolute path to the file on that machine. Similarly to <tt>socket_adapter</tt>, this feed takes <tt>adm</tt> formatted data records.</p></div></div>
314<div class="section">
315<h3><a name="Datatype_for_feed_and_target_dataset"></a>Datatype for feed and target dataset</h3>
316<p>The &#x201c;type-name&#x201d; parameter in create feed statement defines the <tt>datatype</tt> of the datasource. In most use cases, feed will have the same <tt>datatype</tt> as the target dataset. However, if we want to perform certain preprocess before the data records gets into the target dataset (append autogenerated key, apply user defined functions, etc.), we will need to define the datatypes for feed and dataset separately.</p>
317<div class="section">
318<h4><a name="Ingestion_with_autogenerated_key"></a>Ingestion with autogenerated key</h4>
319<p>AsterixDB supports using autogenerated uuid as the primary key for dataset. When we use this feature, we will need to define a datatype with the primary key field, and specify that field to be autogenerated when creating the dataset. Use that same datatype in feed definition will cause a type discrepancy since there is no such field in the datasource. Thus, we will need to define two separate datatypes for feed and dataset:</p>
320
321<div>
322<div>
323<pre class="source"> use feeds;
324
325 create type DBLPFeedType as closed {
326 dblpid: string,
327 title: string,
328 authors: string,
329 misc: string
330 }
331
332 create type DBLPDataSetType as open {
333 id: uuid,
334 dblpid: string,
335 title: string,
336 authors: string,
337 misc: string
338 }
339 create dataset DBLPDataset(DBLPDataSetType) primary key id autogenerated;
340
341 create feed DBLPFeed with {
342 &quot;adapter-name&quot;: &quot;socket_adapter&quot;,
343 &quot;sockets&quot;: &quot;127.0.0.1:10001&quot;,
344 &quot;address-type&quot;: &quot;IP&quot;,
345 &quot;type-name&quot;: &quot;DBLPFeedType&quot;,
346 &quot;format&quot;: &quot;adm&quot;
347 };
348
349 connect feed DBLPFeed to dataset DBLPDataset;
350
351 start feed DBLPFeed;
352</pre></div></div>
353</div></div></div>
354<div class="section">
355<h2><a name="Policies_for_Feed_Ingestion"></a><a name="FeedPolicies">Policies for Feed Ingestion</a></h2>
356<p>Multiple feeds may be concurrently operational on an AsterixDB cluster, each competing for resources (CPU cycles, network bandwidth, disk IO) to maintain pace with their respective data sources. As a data management system, AsterixDB is able to manage a set of concurrent feeds and make dynamic decisions related to the allocation of resources, resolving resource bottlenecks and the handling of failures. Each feed has its own set of constraints, influenced largely by the nature of its data source and the applications that intend to consume and process the ingested data. Consider an application that intends to discover the trending topics on Twitter by analyzing tweets that are being processed. Losing a few tweets may be acceptable. In contrast, when ingesting from a data source that provides a click-stream of ad clicks, losing data would translate to a loss of revenue for an application that tracks revenue by charging advertisers per click.</p>
357<p>AsterixDB allows a data feed to have an associated ingestion policy that is expressed as a collection of parameters and associated values. An ingestion policy dictates the runtime behavior of the feed in response to resource bottlenecks and failures. AsterixDB provides a set of policies that help customize the system&#x2019;s runtime behavior when handling excess objects.</p>
358<div class="section">
359<div class="section">
360<h4><a name="Policies"></a>Policies</h4>
361<ul>
362
363<li>
364
365<p><i>Spill</i>: Objects that cannot be processed by an operator for lack of resources (referred to as excess objects hereafter) should be persisted to the local disk for deferred processing.</p>
366</li>
367<li>
368
369<p><i>Discard</i>: Excess objects should be discarded.</p>
370</li>
371</ul>
372<p>Note that the end user may choose to form a custom policy. For example, it is possible in AsterixDB to create a custom policy that spills excess objects to disk and subsequently resorts to throttling if the spillage crosses a configured threshold. In all cases, the desired ingestion policy is specified as part of the <tt>connect feed</tt> statement or else the &#x201c;Basic&#x201d; policy will be chosen as the default.</p>
373
374<div>
375<div>
376<pre class="source"> use feeds;
377
378 connect feed TwitterFeed to dataset Tweets using policy Basic;
379</pre></div></div></div></div></div>
380 </div>
381 </div>
382 </div>
383 <hr/>
384 <footer>
385 <div class="container-fluid">
386 <div class="row-fluid">
387<div class="row-fluid">Apache AsterixDB, AsterixDB, Apache, the Apache
388 feather logo, and the Apache AsterixDB project logo are either
389 registered trademarks or trademarks of The Apache Software
390 Foundation in the United States and other countries.
391 All other marks mentioned may be trademarks or registered
392 trademarks of their respective owners.
393 </div>
394 </div>
395 </div>
396 </footer>
397 </body>
398</html>