blob: 67b8ce62760d6585566086bcd410645803a0ea6a [file] [log] [blame]
Ian Maxonbf2c56b2017-01-24 14:14:49 -08001<!DOCTYPE html>
2<!--
Ian Maxond5b11d82017-01-25 10:48:05 -08003 | Generated by Apache Maven Doxia at 2017-01-25
Ian Maxonbf2c56b2017-01-24 14:14:49 -08004 | 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" />
Ian Maxond5b11d82017-01-25 10:48:05 -080010 <meta name="Date-Revision-yyyymmdd" content="20170125" />
Ian Maxonbf2c56b2017-01-24 14:14:49 -080011 <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
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="./" 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
Ian Maxond5b11d82017-01-25 10:48:05 -080051 <li id="publishDate">Last Published: 2017-01-25</li>
Ian Maxonbf2c56b2017-01-24 14:14:49 -080052
53
54
55 <li id="projectVersion" class="pull-right">Version: 0.9.0</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">Get Started - Installation</li>
74
75 <li>
76
77 <a href="ncservice.html" title="Option 1: using NCService">
78 <i class="none"></i>
79 Option 1: using NCService</a>
80 </li>
81
82 <li>
83
84 <a href="install.html" title="Option 2: using Managix">
85 <i class="none"></i>
86 Option 2: using Managix</a>
87 </li>
88
89 <li>
90
91 <a href="yarn.html" title="Option 3: using YARN">
92 <i class="none"></i>
93 Option 3: using YARN</a>
94 </li>
95 <li class="nav-header">AsterixDB Primer</li>
96
97 <li>
98
99 <a href="sqlpp/primer-sqlpp.html" title="Option 1: using SQL++">
100 <i class="none"></i>
101 Option 1: using SQL++</a>
102 </li>
103
104 <li>
105
106 <a href="aql/primer.html" title="Option 2: using AQL">
107 <i class="none"></i>
108 Option 2: using AQL</a>
109 </li>
110 <li class="nav-header">Data Model</li>
111
112 <li>
113
114 <a href="datamodel.html" title="The Asterix Data Model">
115 <i class="none"></i>
116 The Asterix Data Model</a>
117 </li>
118 <li class="nav-header">Queries - SQL++</li>
119
120 <li>
121
122 <a href="sqlpp/manual.html" title="The SQL++ Query Language">
123 <i class="none"></i>
124 The SQL++ Query Language</a>
125 </li>
126
127 <li>
128
129 <a href="sqlpp/builtins.html" title="Builtin Functions">
130 <i class="none"></i>
131 Builtin Functions</a>
132 </li>
133 <li class="nav-header">Queries - AQL</li>
134
135 <li>
136
137 <a href="aql/manual.html" title="The Asterix Query Language (AQL)">
138 <i class="none"></i>
139 The Asterix Query Language (AQL)</a>
140 </li>
141
142 <li>
143
144 <a href="aql/builtins.html" title="Builtin Functions">
145 <i class="none"></i>
146 Builtin Functions</a>
147 </li>
148 <li class="nav-header">Advanced Features</li>
149
150 <li>
151
152 <a href="aql/similarity.html" title="Support of Similarity Queries">
153 <i class="none"></i>
154 Support of Similarity Queries</a>
155 </li>
156
157 <li>
158
159 <a href="aql/fulltext.html" title="Support of Full-text Queries">
160 <i class="none"></i>
161 Support of Full-text Queries</a>
162 </li>
163
164 <li>
165
166 <a href="aql/externaldata.html" title="Accessing External Data">
167 <i class="none"></i>
168 Accessing External Data</a>
169 </li>
170
171 <li>
172
173 <a href="feeds/tutorial.html" title="Support for Data Ingestion">
174 <i class="none"></i>
175 Support for Data Ingestion</a>
176 </li>
177
178 <li class="active">
179
180 <a href="#"><i class="none"></i>User Defined Functions</a>
181 </li>
182
183 <li>
184
185 <a href="aql/filters.html" title="Filter-Based LSM Index Acceleration">
186 <i class="none"></i>
187 Filter-Based LSM Index Acceleration</a>
188 </li>
189 <li class="nav-header">API/SDK</li>
190
191 <li>
192
193 <a href="api.html" title="HTTP API">
194 <i class="none"></i>
195 HTTP API</a>
196 </li>
197 </ul>
198
199
200
201 <hr class="divider" />
202
203 <div id="poweredBy">
204 <div class="clear"></div>
205 <div class="clear"></div>
206 <div class="clear"></div>
207 <a href="./" title="AsterixDB" class="builtBy">
208 <img class="builtBy" alt="AsterixDB" src="images/asterixlogo.png" />
209 </a>
210 </div>
211 </div>
212 </div>
213
214
215 <div id="bodyColumn" class="span9" >
216
217 <!-- ! Licensed to the Apache Software Foundation (ASF) under one
218 ! or more contributor license agreements. See the NOTICE file
219 ! distributed with this work for additional information
220 ! regarding copyright ownership. The ASF licenses this file
221 ! to you under the Apache License, Version 2.0 (the
222 ! "License"); you may not use this file except in compliance
223 ! with the License. You may obtain a copy of the License at
224 !
225 ! http://www.apache.org/licenses/LICENSE-2.0
226 !
227 ! Unless required by applicable law or agreed to in writing,
228 ! software distributed under the License is distributed on an
229 ! "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
230 ! KIND, either express or implied. See the License for the
231 ! specific language governing permissions and limitations
232 ! under the License.
233 ! --><h1>Support for User Defined Functions in AsterixDB</h1>
234<div class="section">
235<h2><a name="Table_of_Contents"></a><a name="atoc" id="#toc">Table of Contents</a></h2>
236
237<ul>
238
239<li><a href="#PreprocessingCollectedData">Using UDF to preprocess feed-collected data</a></li>
240
241<li><a href="#WritingAnExternalUDF">Writing an External UDF</a></li>
242
243<li><a href="#CreatingAnAsterixDBLibrary">Creating an AsterixDB Library</a></li>
244
245<li><a href="#installingUDF">Installing an AsterixDB Library</a></li>
246</ul>
247<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>
248<div class="section">
249<h3><a name="Installing_an_AsterixDB_Library"></a><a name="installingUDF">Installing an AsterixDB Library</a></h3>
250<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>
251
252<ul>
253
254<li>
255<p>Step 1: Stop the AsterixDB instance if it is in the ACTIVE state.</p>
256
257<div class="source">
258<div class="source">
259<pre>$ managix stop -n my_asterix
260</pre></div></div></li>
261
262<li>
263<p>Step 2: Install the library using Managix install command. Just to illustrate, we use the help command to look up the syntax</p>
264
265<div class="source">
266<div class="source">
267<pre>$ managix help -cmd install
268Installs a library to an asterix instance.
269Options
270n Name of Asterix Instance
271d Name of the dataverse under which the library will be installed
272l Name of the library
273p Path to library zip bundle
274</pre></div></div></li>
275</ul>
276<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>
277<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>
278
279<div class="source">
280<div class="source">
281<pre> $ managix install -n my_asterix -d feeds -l testlib -p extlibs/asterix-external-data-0.8.7-binary-assembly.zip
282</pre></div></div>
283<p>You should see the following message:</p>
284
285<div class="source">
286<div class="source">
287<pre> INFO: Installed library testlib
288</pre></div></div>
289<p>We shall next start our AsterixDB instance using the start command as shown below.</p>
290
291<div class="source">
292<div class="source">
293<pre> $ managix start -n my_asterix
294</pre></div></div>
295<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>
296
297<div class="source">
298<div class="source">
299<pre> for $x in dataset Metadata.Function
300 return $x
301
302 for $x in dataset Metadata.Library
303 return $x
304</pre></div></div>
305<p>Our library is now installed and is ready to be used.</p></div></div>
306<div class="section">
307<h2><a name="Preprocessing_Collected_Data"></a><a name="PreprocessingCollectedData" id="PreprocessingCollectedData">Preprocessing Collected Data</a></h2>
308<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>
309<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>
310<p>We consider an example transformation of a raw tweet into its lightweight version called <tt>ProcessedTweet</tt>, which is defined next.</p>
311
312<div class="source">
313<div class="source">
314<pre> use dataverse feeds;
315
316 create type ProcessedTweet if not exists as open {
317 id: string,
318 user_name:string,
319 location:point,
320 created_at:string,
321 message_text:string,
322 country: string,
323 topics: {{string}}
324 };
325
326 create dataset ProcessedTweets(ProcessedTweet)
327 primary key id;
328</pre></div></div>
329<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>
330
331<div class="source">
332<div class="source">
333<pre> use dataverse feeds;
334
335 create feed ProcessedTwitterFeed if not exists
336 using &quot;push_twitter&quot;
337 ((&quot;type-name&quot;=&quot;Tweet&quot;),
338 (&quot;consumer.key&quot;=&quot;************&quot;),
339 (&quot;consumer.secret&quot;=&quot;**************&quot;),
340 (&quot;access.token&quot;=&quot;**********&quot;),
341 (&quot;access.token.secret&quot;=&quot;*************&quot;))
342
343 apply function testlib#addHashTagsInPlace;
344</pre></div></div>
345<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>
346<div class="section">
347<div class="section">
348<h4><a name="Building_a_Cascade_Network_of_Feeds"></a>Building a Cascade Network of Feeds</h4>
349<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>
350<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>
351
352<div class="source">
353<div class="source">
354<pre> use dataverse feeds;
355
356 drop feed ProcessedTwitterFeed if exists;
357
358 create secondary feed ProcessedTwitterFeed from feed TwitterFeed
359 apply function testlib#addHashTags;
360
361 connect feed ProcessedTwitterFeed to dataset ProcessedTweets;
362</pre></div></div>
363<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>
364
365<div class="source">
366<div class="source">
367<pre> use dataverse feeds;
368
369 for $i in dataset ProcessedTweets limit 10 return $i;
370</pre></div></div>
371<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>
372<div class="section">
373<h2><a name="Unstalling_an_AsterixDB_Library"></a><a name="installingUDF">Unstalling an AsterixDB Library</a></h2>
374<p>To uninstall a library, use the Managix uninstall command as follows:</p>
375
376<div class="source">
377<div class="source">
378<pre> $ managix stop -n my_asterix
379
380 $ managix uninstall -n my_asterix -d feeds -l testlib
381</pre></div></div></div>
382 </div>
383 </div>
384 </div>
385
386 <hr/>
387
388 <footer>
389 <div class="container-fluid">
390 <div class="row span12">Copyright &copy; 2017
391 <a href="https://www.apache.org/">The Apache Software Foundation</a>.
392 All Rights Reserved.
393
394 </div>
395
396 <?xml version="1.0" encoding="UTF-8"?>
397<div class="row-fluid">Apache AsterixDB, AsterixDB, Apache, the Apache
398 feather logo, and the Apache AsterixDB project logo are either
399 registered trademarks or trademarks of The Apache Software
400 Foundation in the United States and other countries.
401 All other marks mentioned may be trademarks or registered
402 trademarks of their respective owners.</div>
403
404
405 </div>
406 </footer>
407 </body>
408</html>