blob: 99e36d76db5cf2682a0df766a3a9e6a668f3f114 [file] [log] [blame]
<!DOCTYPE html>
<!--
| Generated by Apache Maven Doxia at 2018-02-09
| Rendered using Apache Maven Fluido Skin 1.3.0
-->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="Date-Revision-yyyymmdd" content="20180209" />
<meta http-equiv="Content-Language" content="en" />
<title>AsterixDB &#x2013; Support for User Defined Functions in AsterixDB</title>
<link rel="stylesheet" href="./css/apache-maven-fluido-1.3.0.min.css" />
<link rel="stylesheet" href="./css/site.css" />
<link rel="stylesheet" href="./css/print.css" media="print" />
<script type="text/javascript" src="./js/apache-maven-fluido-1.3.0.min.js"></script>
<script>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-41536543-1', 'uci.edu');
ga('send', 'pageview');</script>
</head>
<body class="topBarDisabled">
<div class="container-fluid">
<div id="banner">
<div class="pull-left">
<a href="./" id="bannerLeft">
<img src="images/asterixlogo.png" alt="AsterixDB"/>
</a>
</div>
<div class="pull-right"> </div>
<div class="clear"><hr/></div>
</div>
<div id="breadcrumbs">
<ul class="breadcrumb">
<li id="publishDate">Last Published: 2018-02-09</li>
<li id="projectVersion" class="pull-right">Version: 0.9.3</li>
<li class="divider pull-right">|</li>
<li class="pull-right"> <a href="index.html" title="Documentation Home">
Documentation Home</a>
</li>
</ul>
</div>
<div class="row-fluid">
<div id="leftColumn" class="span3">
<div class="well sidebar-nav">
<ul class="nav nav-list">
<li class="nav-header">Get Started - Installation</li>
<li>
<a href="ncservice.html" title="Option 1: using NCService">
<i class="none"></i>
Option 1: using NCService</a>
</li>
<li>
<a href="ansible.html" title="Option 2: using Ansible">
<i class="none"></i>
Option 2: using Ansible</a>
</li>
<li>
<a href="aws.html" title="Option 3: using Amazon Web Services">
<i class="none"></i>
Option 3: using Amazon Web Services</a>
</li>
<li>
<a href="yarn.html" title="Option 4: using YARN">
<i class="none"></i>
Option 4: using YARN</a>
</li>
<li>
<a href="install.html" title="Option 5: using Managix (deprecated)">
<i class="none"></i>
Option 5: using Managix (deprecated)</a>
</li>
<li class="nav-header">AsterixDB Primer</li>
<li>
<a href="sqlpp/primer-sqlpp.html" title="Option 1: using SQL++">
<i class="none"></i>
Option 1: using SQL++</a>
</li>
<li>
<a href="aql/primer.html" title="Option 2: using AQL">
<i class="none"></i>
Option 2: using AQL</a>
</li>
<li class="nav-header">Data Model</li>
<li>
<a href="datamodel.html" title="The Asterix Data Model">
<i class="none"></i>
The Asterix Data Model</a>
</li>
<li class="nav-header">Queries - SQL++</li>
<li>
<a href="sqlpp/manual.html" title="The SQL++ Query Language">
<i class="none"></i>
The SQL++ Query Language</a>
</li>
<li>
<a href="sqlpp/builtins.html" title="Builtin Functions">
<i class="none"></i>
Builtin Functions</a>
</li>
<li class="nav-header">Queries - AQL</li>
<li>
<a href="aql/manual.html" title="The Asterix Query Language (AQL)">
<i class="none"></i>
The Asterix Query Language (AQL)</a>
</li>
<li>
<a href="aql/builtins.html" title="Builtin Functions">
<i class="none"></i>
Builtin Functions</a>
</li>
<li class="nav-header">API/SDK</li>
<li>
<a href="api.html" title="HTTP API">
<i class="none"></i>
HTTP API</a>
</li>
<li>
<a href="csv.html" title="CSV Output">
<i class="none"></i>
CSV Output</a>
</li>
<li class="nav-header">Advanced Features</li>
<li>
<a href="aql/fulltext.html" title="Support of Full-text Queries">
<i class="none"></i>
Support of Full-text Queries</a>
</li>
<li>
<a href="aql/externaldata.html" title="Accessing External Data">
<i class="none"></i>
Accessing External Data</a>
</li>
<li>
<a href="feeds/tutorial.html" title="Support for Data Ingestion">
<i class="none"></i>
Support for Data Ingestion</a>
</li>
<li class="active">
<a href="#"><i class="none"></i>User Defined Functions</a>
</li>
<li>
<a href="aql/filters.html" title="Filter-Based LSM Index Acceleration">
<i class="none"></i>
Filter-Based LSM Index Acceleration</a>
</li>
<li>
<a href="aql/similarity.html" title="Support of Similarity Queries">
<i class="none"></i>
Support of Similarity Queries</a>
</li>
</ul>
<hr class="divider" />
<div id="poweredBy">
<div class="clear"></div>
<div class="clear"></div>
<div class="clear"></div>
<a href="./" title="AsterixDB" class="builtBy">
<img class="builtBy" alt="AsterixDB" src="images/asterixlogo.png" />
</a>
</div>
</div>
</div>
<div id="bodyColumn" class="span9" >
<!-- ! Licensed to the Apache Software Foundation (ASF) under one
! or more contributor license agreements. See the NOTICE file
! distributed with this work for additional information
! regarding copyright ownership. The ASF licenses this file
! to you under the Apache License, Version 2.0 (the
! "License"); you may not use this file except in compliance
! with the License. You may obtain a copy of the License at
!
! http://www.apache.org/licenses/LICENSE-2.0
!
! Unless required by applicable law or agreed to in writing,
! software distributed under the License is distributed on an
! "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
! KIND, either express or implied. See the License for the
! specific language governing permissions and limitations
! under the License.
! --><h1>Support for User Defined Functions in AsterixDB</h1>
<div class="section">
<h2><a name="Table_of_Contents"></a><a name="atoc" id="#toc">Table of Contents</a></h2>
<ul>
<li><a href="#PreprocessingCollectedData">Using UDF to preprocess feed-collected data</a></li>
<li><a href="#WritingAnExternalUDF">Writing an External UDF</a></li>
<li><a href="#CreatingAnAsterixDBLibrary">Creating an AsterixDB Library</a></li>
<li><a href="#installingUDF">Installing an AsterixDB Library</a></li>
</ul>
<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>
<div class="section">
<h3><a name="Installing_an_AsterixDB_Library"></a><a name="installingUDF">Installing an AsterixDB Library</a></h3>
<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>
<ul>
<li>
<p>Step 1: Stop the AsterixDB instance if it is in the ACTIVE state.</p>
<div class="source">
<div class="source">
<pre>$ managix stop -n my_asterix
</pre></div></div></li>
<li>
<p>Step 2: Install the library using Managix install command. Just to illustrate, we use the help command to look up the syntax</p>
<div class="source">
<div class="source">
<pre>$ managix help -cmd install
Installs a library to an asterix instance.
Options
n Name of Asterix Instance
d Name of the dataverse under which the library will be installed
l Name of the library
p Path to library zip bundle
</pre></div></div></li>
</ul>
<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>
<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>
<div class="source">
<div class="source">
<pre> $ managix install -n my_asterix -d feeds -l testlib -p extlibs/asterix-external-data-0.8.7-binary-assembly.zip
</pre></div></div>
<p>You should see the following message:</p>
<div class="source">
<div class="source">
<pre> INFO: Installed library testlib
</pre></div></div>
<p>We shall next start our AsterixDB instance using the start command as shown below.</p>
<div class="source">
<div class="source">
<pre> $ managix start -n my_asterix
</pre></div></div>
<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>
<div class="source">
<div class="source">
<pre> for $x in dataset Metadata.Function
return $x
for $x in dataset Metadata.Library
return $x
</pre></div></div>
<p>Our library is now installed and is ready to be used.</p></div></div>
<div class="section">
<h2><a name="Preprocessing_Collected_Data"></a><a name="PreprocessingCollectedData" id="PreprocessingCollectedData">Preprocessing Collected Data</a></h2>
<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>
<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>
<p>We consider an example transformation of a raw tweet into its lightweight version called <tt>ProcessedTweet</tt>, which is defined next.</p>
<div class="source">
<div class="source">
<pre> use dataverse feeds;
create type ProcessedTweet if not exists as open {
id: string,
user_name:string,
location:point,
created_at:string,
message_text:string,
country: string,
topics: {{string}}
};
create dataset ProcessedTweets(ProcessedTweet)
primary key id;
</pre></div></div>
<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>
<div class="source">
<div class="source">
<pre> use dataverse feeds;
create feed ProcessedTwitterFeed if not exists
using &quot;push_twitter&quot;
((&quot;type-name&quot;=&quot;Tweet&quot;),
(&quot;consumer.key&quot;=&quot;************&quot;),
(&quot;consumer.secret&quot;=&quot;**************&quot;),
(&quot;access.token&quot;=&quot;**********&quot;),
(&quot;access.token.secret&quot;=&quot;*************&quot;))
apply function testlib#addHashTagsInPlace;
</pre></div></div>
<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>
<div class="section">
<div class="section">
<h4><a name="Building_a_Cascade_Network_of_Feeds"></a>Building a Cascade Network of Feeds</h4>
<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>
<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>
<div class="source">
<div class="source">
<pre> use dataverse feeds;
drop feed ProcessedTwitterFeed if exists;
create secondary feed ProcessedTwitterFeed from feed TwitterFeed
apply function testlib#addHashTags;
connect feed ProcessedTwitterFeed to dataset ProcessedTweets;
</pre></div></div>
<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>
<div class="source">
<div class="source">
<pre> use dataverse feeds;
for $i in dataset ProcessedTweets limit 10 return $i;
</pre></div></div>
<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>
<div class="section">
<h2><a name="Unstalling_an_AsterixDB_Library"></a><a name="installingUDF">Unstalling an AsterixDB Library</a></h2>
<p>To uninstall a library, use the Managix uninstall command as follows:</p>
<div class="source">
<div class="source">
<pre> $ managix stop -n my_asterix
$ managix uninstall -n my_asterix -d feeds -l testlib
</pre></div></div></div>
</div>
</div>
</div>
<hr/>
<footer>
<div class="container-fluid">
<div class="row span12">Copyright &copy; 2018
<a href="https://www.apache.org/">The Apache Software Foundation</a>.
All Rights Reserved.
</div>
<?xml version="1.0" encoding="UTF-8"?>
<div class="row-fluid">Apache AsterixDB, AsterixDB, Apache, the Apache
feather logo, and the Apache AsterixDB project logo are either
registered trademarks or trademarks of The Apache Software
Foundation in the United States and other countries.
All other marks mentioned may be trademarks or registered
trademarks of their respective owners.</div>
</div>
</footer>
</body>
</html>