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