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