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