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