blob: d914bd2fac22c404ab30ed66104eb9520bd2dae8 [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; Support for User Defined Functions 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 Maxon100cb802017-04-24 18:48:07 -070022
Ian Maxon100cb802017-04-24 18:48:07 -070023
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="./" 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: 2017-04-24</li>
46
47
48
49 <li id="projectVersion" class="pull-right">Version: 0.9.1</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">Get Started - Installation</li>
68
69 <li>
70
71 <a href="ncservice.html" title="Option 1: using NCService">
72 <i class="none"></i>
73 Option 1: using NCService</a>
74 </li>
75
76 <li>
77
78 <a href="ansible.html" title="Option 2: using Ansible">
79 <i class="none"></i>
80 Option 2: using Ansible</a>
81 </li>
82
83 <li>
84
85 <a href="aws.html" title="Option 3: using Amazon Web Services">
86 <i class="none"></i>
87 Option 3: using Amazon Web Services</a>
88 </li>
89
90 <li>
91
92 <a href="yarn.html" title="Option 4: using YARN">
93 <i class="none"></i>
94 Option 4: using YARN</a>
95 </li>
96
97 <li>
98
99 <a href="install.html" title="Option 5: using Managix (deprecated)">
100 <i class="none"></i>
101 Option 5: using Managix (deprecated)</a>
102 </li>
103 <li class="nav-header">AsterixDB Primer</li>
104
105 <li>
106
107 <a href="sqlpp/primer-sqlpp.html" title="Option 1: using SQL++">
108 <i class="none"></i>
109 Option 1: using SQL++</a>
110 </li>
111
112 <li>
113
114 <a href="aql/primer.html" title="Option 2: using AQL">
115 <i class="none"></i>
116 Option 2: using AQL</a>
117 </li>
118 <li class="nav-header">Data Model</li>
119
120 <li>
121
122 <a href="datamodel.html" title="The Asterix Data Model">
123 <i class="none"></i>
124 The Asterix Data Model</a>
125 </li>
126 <li class="nav-header">Queries - SQL++</li>
127
128 <li>
129
130 <a href="sqlpp/manual.html" title="The SQL++ Query Language">
131 <i class="none"></i>
132 The SQL++ Query Language</a>
133 </li>
134
135 <li>
136
137 <a href="sqlpp/builtins.html" title="Builtin Functions">
138 <i class="none"></i>
139 Builtin Functions</a>
140 </li>
141 <li class="nav-header">Queries - AQL</li>
142
143 <li>
144
145 <a href="aql/manual.html" title="The Asterix Query Language (AQL)">
146 <i class="none"></i>
147 The Asterix Query Language (AQL)</a>
148 </li>
149
150 <li>
151
152 <a href="aql/builtins.html" title="Builtin Functions">
153 <i class="none"></i>
154 Builtin Functions</a>
155 </li>
156 <li class="nav-header">API/SDK</li>
157
158 <li>
159
160 <a href="api.html" title="HTTP API">
161 <i class="none"></i>
162 HTTP API</a>
163 </li>
164
165 <li>
166
167 <a href="csv.html" title="CSV Output">
168 <i class="none"></i>
169 CSV Output</a>
170 </li>
171 <li class="nav-header">Advanced Features</li>
172
173 <li>
174
175 <a href="aql/fulltext.html" title="Support of Full-text Queries">
176 <i class="none"></i>
177 Support of Full-text Queries</a>
178 </li>
179
180 <li>
181
182 <a href="aql/externaldata.html" title="Accessing External Data">
183 <i class="none"></i>
184 Accessing External Data</a>
185 </li>
186
187 <li>
188
189 <a href="feeds/tutorial.html" title="Support for Data Ingestion">
190 <i class="none"></i>
191 Support for Data Ingestion</a>
192 </li>
193
194 <li class="active">
195
196 <a href="#"><i class="none"></i>User Defined Functions</a>
197 </li>
198
199 <li>
200
201 <a href="aql/filters.html" title="Filter-Based LSM Index Acceleration">
202 <i class="none"></i>
203 Filter-Based LSM Index Acceleration</a>
204 </li>
205
206 <li>
207
208 <a href="aql/similarity.html" title="Support of Similarity Queries">
209 <i class="none"></i>
210 Support of Similarity Queries</a>
211 </li>
212 </ul>
213
214
215
216 <hr class="divider" />
217
218 <div id="poweredBy">
219 <div class="clear"></div>
220 <div class="clear"></div>
221 <div class="clear"></div>
222 <a href="./" title="AsterixDB" class="builtBy">
223 <img class="builtBy" alt="AsterixDB" src="images/asterixlogo.png" />
224 </a>
225 </div>
226 </div>
227 </div>
228
229
230 <div id="bodyColumn" class="span9" >
231
232 <!-- ! Licensed to the Apache Software Foundation (ASF) under one
233 ! or more contributor license agreements. See the NOTICE file
234 ! distributed with this work for additional information
235 ! regarding copyright ownership. The ASF licenses this file
236 ! to you under the Apache License, Version 2.0 (the
237 ! "License"); you may not use this file except in compliance
238 ! with the License. You may obtain a copy of the License at
239 !
240 ! http://www.apache.org/licenses/LICENSE-2.0
241 !
242 ! Unless required by applicable law or agreed to in writing,
243 ! software distributed under the License is distributed on an
244 ! "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
245 ! KIND, either express or implied. See the License for the
246 ! specific language governing permissions and limitations
247 ! under the License.
248 ! --><h1>Support for User Defined Functions in AsterixDB</h1>
249<div class="section">
250<h2><a name="Table_of_Contents"></a><a name="atoc" id="#toc">Table of Contents</a></h2>
251
252<ul>
253
254<li><a href="#PreprocessingCollectedData">Using UDF to preprocess feed-collected data</a></li>
255
256<li><a href="#WritingAnExternalUDF">Writing an External UDF</a></li>
257
258<li><a href="#CreatingAnAsterixDBLibrary">Creating an AsterixDB Library</a></li>
259
260<li><a href="#installingUDF">Installing an AsterixDB Library</a></li>
261</ul>
262<p>In this document, we describe the support for implementing, using, and installing user-defined functions (UDF) in AsterixDB. We will explain how we can use UDFs to preprocess, e.g., data collected using feeds (see the <a href="feeds/tutorial.html">feeds tutorial</a>).</p>
263<div class="section">
264<h3><a name="Installing_an_AsterixDB_Library"></a><a name="installingUDF">Installing an AsterixDB Library</a></h3>
265<p>We assume you have followed the <a href="../install.html">installation instructions</a> to set up a running AsterixDB instance. Let us refer your AsterixDB instance by the name &#x201c;my_asterix&#x201d;.</p>
266
267<ul>
268
269<li>
270<p>Step 1: Stop the AsterixDB instance if it is in the ACTIVE state.</p>
271
272<div class="source">
273<div class="source">
274<pre>$ managix stop -n my_asterix
275</pre></div></div></li>
276
277<li>
278<p>Step 2: Install the library using Managix install command. Just to illustrate, we use the help command to look up the syntax</p>
279
280<div class="source">
281<div class="source">
282<pre>$ managix help -cmd install
283Installs a library to an asterix instance.
284Options
285n Name of Asterix Instance
286d Name of the dataverse under which the library will be installed
287l Name of the library
288p Path to library zip bundle
289</pre></div></div></li>
290</ul>
291<p>Above is a sample output and explains the usage and the required parameters. Each library has a name and is installed under a dataverse. Recall that we had created a dataverse by the name - &#x201c;feeds&#x201d; prior to creating our datatypes and dataset. We shall name our library - &#x201c;testlib&#x201d;.</p>
292<p>We assume you have a library zip bundle that needs to be installed. To install the library, use the Managix install command. An example is shown below.</p>
293
294<div class="source">
295<div class="source">
296<pre> $ managix install -n my_asterix -d feeds -l testlib -p extlibs/asterix-external-data-0.8.7-binary-assembly.zip
297</pre></div></div>
298<p>You should see the following message:</p>
299
300<div class="source">
301<div class="source">
302<pre> INFO: Installed library testlib
303</pre></div></div>
304<p>We shall next start our AsterixDB instance using the start command as shown below.</p>
305
306<div class="source">
307<div class="source">
308<pre> $ managix start -n my_asterix
309</pre></div></div>
310<p>You may now use the AsterixDB library in AQL statements and queries. To look at the installed artifacts, you may execute the following query at the AsterixDB web-console.</p>
311
312<div class="source">
313<div class="source">
314<pre> for $x in dataset Metadata.Function
315 return $x
316
317 for $x in dataset Metadata.Library
318 return $x
319</pre></div></div>
320<p>Our library is now installed and is ready to be used.</p></div></div>
321<div class="section">
322<h2><a name="Preprocessing_Collected_Data"></a><a name="PreprocessingCollectedData" id="PreprocessingCollectedData">Preprocessing Collected Data</a></h2>
323<p>In the following we assume that you already created the <tt>TwitterFeed</tt> and its corresponding data types and dataset following the instruction explained in the <a href="feeds/tutorial.html">feeds tutorial</a>.</p>
324<p>A feed definition may optionally include the specification of a user-defined function that is to be applied to each feed object prior to persistence. Examples of pre-processing might include adding attributes, filtering out objects, sampling, sentiment analysis, feature extraction, etc. We can express a UDF, which can be defined in AQL or in a programming language such as Java, to perform such pre-processing. An AQL UDF is a good fit when pre-processing a object requires the result of a query (join or aggregate) over data contained in AsterixDB datasets. More sophisticated processing such as sentiment analysis of text is better handled by providing a Java UDF. A Java UDF has an initialization phase that allows the UDF to access any resources it may need to initialize itself prior to being used in a data flow. It is assumed by the AsterixDB compiler to be stateless and thus usable as an embarrassingly parallel black box. In contrast, the AsterixDB compiler can reason about an AQL UDF and involve the use of indexes during its invocation.</p>
325<p>We consider an example transformation of a raw tweet into its lightweight version called <tt>ProcessedTweet</tt>, which is defined next.</p>
326
327<div class="source">
328<div class="source">
329<pre> use dataverse feeds;
330
331 create type ProcessedTweet if not exists as open {
332 id: string,
333 user_name:string,
334 location:point,
335 created_at:string,
336 message_text:string,
337 country: string,
338 topics: {{string}}
339 };
340
341 create dataset ProcessedTweets(ProcessedTweet)
342 primary key id;
343</pre></div></div>
344<p>The processing required in transforming a collected tweet to its lighter version of type <tt>ProcessedTweet</tt> involves extracting the topics or hash-tags (if any) in a tweet and collecting them in the referred &#x201c;topics&#x201d; attribute for the tweet. Additionally, the latitude and longitude values (doubles) are combined into the spatial point type. Note that spatial data types are considered as first-class citizens that come with the support for creating indexes. Next we show a revised version of our example TwitterFeed that involves the use of a UDF. We assume that the UDF that contains the transformation logic into a &#x201c;ProcessedTweet&#x201d; is available as a Java UDF inside an AsterixDB library named &#x2018;testlib&#x2019;. We defer the writing of a Java UDF and its installation as part of an AsterixDB library to a later section of this document.</p>
345
346<div class="source">
347<div class="source">
348<pre> use dataverse feeds;
349
350 create feed ProcessedTwitterFeed if not exists
351 using &quot;push_twitter&quot;
352 ((&quot;type-name&quot;=&quot;Tweet&quot;),
353 (&quot;consumer.key&quot;=&quot;************&quot;),
354 (&quot;consumer.secret&quot;=&quot;**************&quot;),
355 (&quot;access.token&quot;=&quot;**********&quot;),
356 (&quot;access.token.secret&quot;=&quot;*************&quot;))
357
358 apply function testlib#addHashTagsInPlace;
359</pre></div></div>
360<p>Note that a feed adaptor and a UDF act as pluggable components. These contribute towards providing a generic &#x201c;plug-and-play&#x201d; model where custom implementations can be provided to cater to specific requirements.</p>
361<div class="section">
362<div class="section">
363<h4><a name="Building_a_Cascade_Network_of_Feeds"></a>Building a Cascade Network of Feeds</h4>
364<p>Multiple high-level applications may wish to consume the data ingested from a data feed. Each such application might perceive the feed in a different way and require the arriving data to be processed and/or persisted differently. Building a separate flow of data from the external source for each application is wasteful of resources as the pre-processing or transformations required by each application might overlap and could be done together in an incremental fashion to avoid redundancy. A single flow of data from the external source could provide data for multiple applications. To achieve this, we introduce the notion of primary and secondary feeds in AsterixDB.</p>
365<p>A feed in AsterixDB is considered to be a primary feed if it gets its data from an external data source. The objects contained in a feed (subsequent to any pre-processing) are directed to a designated AsterixDB dataset. Alternatively or additionally, these objects can be used to derive other feeds known as secondary feeds. A secondary feed is similar to its parent feed in every other aspect; it can have an associated UDF to allow for any subsequent processing, can be persisted into a dataset, and/or can be made to derive other secondary feeds to form a cascade network. A primary feed and a dependent secondary feed form a hierarchy. As an example, we next show an example AQL statement that redefines the previous feed &#x201c;ProcessedTwitterFeed&#x201d; in terms of their respective parent feed (TwitterFeed).</p>
366
367<div class="source">
368<div class="source">
369<pre> use dataverse feeds;
370
371 drop feed ProcessedTwitterFeed if exists;
372
373 create secondary feed ProcessedTwitterFeed from feed TwitterFeed
374 apply function testlib#addHashTags;
375
376 connect feed ProcessedTwitterFeed to dataset ProcessedTweets;
377</pre></div></div>
378<p>The <tt>addHashTags</tt> function is already provided in the example UDF.To see what objects are being inserted into the dataset, we can perform a simple dataset scan after allowing a few moments for the feed to start ingesting data:</p>
379
380<div class="source">
381<div class="source">
382<pre> use dataverse feeds;
383
384 for $i in dataset ProcessedTweets limit 10 return $i;
385</pre></div></div>
386<p>For an example of how to write a Java UDF from scratch, the source for the example UDF that has been used in this tutorial is available <a class="externalLink" href="https://github.com/apache/asterixdb/tree/master/asterixdb/asterix-external-data/src/test/java/org/apache/asterix/external/library">here</a></p></div></div></div>
387<div class="section">
388<h2><a name="Unstalling_an_AsterixDB_Library"></a><a name="installingUDF">Unstalling an AsterixDB Library</a></h2>
389<p>To uninstall a library, use the Managix uninstall command as follows:</p>
390
391<div class="source">
392<div class="source">
393<pre> $ managix stop -n my_asterix
394
395 $ managix uninstall -n my_asterix -d feeds -l testlib
396</pre></div></div></div>
397 </div>
398 </div>
399 </div>
400
401 <hr/>
402
403 <footer>
404 <div class="container-fluid">
405 <div class="row span12">Copyright &copy; 2017
406 <a href="https://www.apache.org/">The Apache Software Foundation</a>.
407 All Rights Reserved.
408
409 </div>
410
411 <?xml version="1.0" encoding="UTF-8"?>
412<div class="row-fluid">Apache AsterixDB, AsterixDB, Apache, the Apache
413 feather logo, and the Apache AsterixDB project logo are either
414 registered trademarks or trademarks of The Apache Software
415 Foundation in the United States and other countries.
416 All other marks mentioned may be trademarks or registered
417 trademarks of their respective owners.</div>
418
419
420 </div>
421 </footer>
422 </body>
423</html>