blob: 65bc7bbc9693521b854b3f95f39c59cd3fd339ec [file] [log] [blame]
<!DOCTYPE html>
<!--
| Generated by Apache Maven Doxia Site Renderer 1.8.1 from target/generated-site/markdown/udf.md at 2018-10-12
| Rendered using Apache Maven Fluido Skin 1.7
-->
<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="20181012" />
<meta http-equiv="Content-Language" content="en" />
<title>AsterixDB &#x2013; User-defined Functions</title>
<link rel="stylesheet" href="./css/apache-maven-fluido-1.7.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.7.min.js"></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-10-12</li>
<li id="projectVersion" class="pull-right">Version: 0.9.4</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="span2">
<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"><span class="none"></span>Option 1: using NCService</a></li>
<li><a href="ansible.html" title="Option 2: using Ansible"><span class="none"></span>Option 2: using Ansible</a></li>
<li><a href="aws.html" title="Option 3: using Amazon Web Services"><span class="none"></span>Option 3: using Amazon Web Services</a></li>
<li class="nav-header">AsterixDB Primer</li>
<li><a href="sqlpp/primer-sqlpp.html" title="Using SQL++"><span class="none"></span>Using SQL++</a></li>
<li class="nav-header">Data Model</li>
<li><a href="datamodel.html" title="The Asterix Data Model"><span class="none"></span>The Asterix Data Model</a></li>
<li class="nav-header">Queries</li>
<li><a href="sqlpp/manual.html" title="The SQL++ Query Language"><span class="none"></span>The SQL++ Query Language</a></li>
<li><a href="sqlpp/builtins.html" title="Builtin Functions"><span class="none"></span>Builtin Functions</a></li>
<li class="nav-header">API/SDK</li>
<li><a href="api.html" title="HTTP API"><span class="none"></span>HTTP API</a></li>
<li><a href="csv.html" title="CSV Output"><span class="none"></span>CSV Output</a></li>
<li class="nav-header">Advanced Features</li>
<li><a href="aql/externaldata.html" title="Accessing External Data"><span class="none"></span>Accessing External Data</a></li>
<li><a href="feeds.html" title="Data Ingestion with Feeds"><span class="none"></span>Data Ingestion with Feeds</a></li>
<li class="active"><a href="#"><span class="none"></span>User Defined Functions</a></li>
<li><a href="sqlpp/filters.html" title="Filter-Based LSM Index Acceleration"><span class="none"></span>Filter-Based LSM Index Acceleration</a></li>
<li><a href="sqlpp/fulltext.html" title="Support of Full-text Queries"><span class="none"></span>Support of Full-text Queries</a></li>
<li><a href="sqlpp/similarity.html" title="Support of Similarity Queries"><span class="none"></span>Support of Similarity Queries</a></li>
<li class="nav-header">Deprecated</li>
<li><a href="aql/primer.html" title="AsterixDB Primer: Using AQL"><span class="none"></span>AsterixDB Primer: Using AQL</a></li>
<li><a href="aql/manual.html" title="Queries: The Asterix Query Language (AQL)"><span class="none"></span>Queries: The Asterix Query Language (AQL)</a></li>
<li><a href="aql/builtins.html" title="Queries: Builtin Functions (AQL)"><span class="none"></span>Queries: Builtin Functions (AQL)</a></li>
</ul>
<hr />
<div id="poweredBy">
<div class="clear"></div>
<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="span10" >
<!--
! 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>User-defined Functions</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="#introduction">Introduction</a></li>
<li><a href="#installingUDF">Installing an UDF Library</a></li>
<li><a href="#UDFOnFeeds">Attaching an UDF on Data Feeds</a></li>
<li><a href="#udfConfiguration">A quick look of the UDF configuration</a></li>
<li><a href="#uninstall">Unstalling an UDF Library</a><!--
! 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.
!--></li>
</ul></div>
<div class="section">
<h2><a name="Introduction"></a><a name="introduction">Introduction</a></h2>
<p>Apache AsterixDB supports two languages for writing user-defined functions (UDFs): SQL++ and Java. A user can encapsulate data processing logic into a UDF and invoke it later repeatedly. For SQL++ functions, a user can refer to <a href="sqlpp/manual.html#Functions">SQL++ Functions</a> for their usages. In this document, we focus on how to install/invoke/uninstall a Java function library using the Ansible script that we provide.</p></div>
<div class="section">
<h2><a name="Installing_an_UDF_Library"></a><a name="installingUDF">Installing an UDF Library</a></h2>
<p>UDFs have to be installed offline. This section describes the process assuming that you have followed the preceding <a href="ansible.html">ansible installation instructions</a> to deploy an AsterixDB instance on your local machine or cluster. Here are the instructions to install an UDF library:</p>
<ul>
<li>
<p>Step 1: Stop the AsterixDB instance if it is ACTIVE.</p>
<div>
<div>
<pre class="source">$ bin/stop.sh
</pre></div></div>
</li>
<li>
<p>Step 2: Deploy the UDF package.</p>
<div>
<div>
<pre class="source">$ bin/udf.sh -m i -d DATAVERSE_NAME -l LIBRARY_NAME -p UDF_PACKAGE_PATH
</pre></div></div>
</li>
<li>
<p>Step 3: Start AsterixDB</p>
<div>
<div>
<pre class="source">$ bin/start.sh
</pre></div></div>
</li>
</ul>
<p>After AsterixDB starts, you can use the following query to check whether your UDFs have been sucessfully registered with the system.</p>
<div>
<div>
<pre class="source"> SELECT * FROM Metadata.`Function`;
</pre></div></div>
<p>In the AsterixDB source release, we provide several sample UDFs that you can try out. You need to build the AsterixDB source to get the compiled UDF package. It can be found under the <tt>asterixdb-external</tt> sub-project. Assuming that these UDFs have been installed into the <tt>udfs</tt> dataverse and <tt>testlib</tt> library, here is an example that uses the sample UDF <tt>mysum</tt> to compute the sum of two input integers.</p>
<div>
<div>
<pre class="source"> use udfs;
testlib#mysum(3,4);
</pre></div></div>
</div>
<div class="section">
<h2><a name="Attaching_a_UDF_on_Data_Feeds"></a><a name="UDFOnFeeds" id="UDFOnFeeds">Attaching a UDF on Data Feeds</a></h2>
<p>In <a href="feeds.html">Data Ingestion using feeds</a>, we introduced an efficient way for users to get data into AsterixDB. In some use cases, users may want to pre-process the incoming data before storing it into the dataset. To meet this need, AsterixDB allows the user to attach a UDF onto the ingestion pipeline. Following the example in <a href="feeds.html">Data Ingestion</a>, here we show an example of how to attach a UDF that extracts the user names mentioned from the incoming Tweet text, storing the processed Tweets into a dataset.</p>
<p>We start by creating the datatype and dataset that will be used for the feed and UDF. One thing to keep in mind is that data flows from the feed to the UDF and then to the dataset. This means that the feed&#x2019;s datatype should be the same as the input type of the UDF, and the output datatype of the UDF should be the same as the dataset&#x2019;s datatype. Thus, users should make sure that their datatypes are consistent in the UDF configuration. Users can also take advantage of open datatypes in AsterixDB by creating a minimum description of the data for simplicity. Here we use open datatypes:</p>
<div>
<div>
<pre class="source"> use udfs;
create type TweetType if not exists as open {
id: int64
};
create dataset ProcessedTweets(TweetType) primary key id;
</pre></div></div>
<p>As the <tt>TweetType</tt> is an open datatype, processed Tweets can be stored into the dataset after they are annotated with an extra attribute. Given the datatype and dataset above, we can create a Twitter Feed with the same datatype. Please refer to section <a href="feeds.html">Data Ingestion</a> if you have any trouble in creating feeds.</p>
<div>
<div>
<pre class="source"> use udfs;
create feed TwitterFeed with {
&quot;adapter-name&quot;: &quot;push_twitter&quot;,
&quot;type-name&quot;: &quot;TweetType&quot;,
&quot;format&quot;: &quot;twitter-status&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;
};
</pre></div></div>
<p>After creating the feed, we attach the UDF onto the feed pipeline and start the feed with following statements:</p>
<div>
<div>
<pre class="source"> use udfs;
connect feed TwitterFeed to dataset ProcessedTweets apply function udfs#addMentionedUsers;
start feed TwitterFeed;
</pre></div></div>
<p>You can check the annotated Tweets by querying the <tt>ProcessedTweets</tt> dataset:</p>
<div>
<div>
<pre class="source"> SELECT * FROM ProcessedTweets LIMIT 10;
</pre></div></div>
</div>
<div class="section">
<h2><a name="A_quick_look_of_the_UDF_configuration"></a><a name="udfConfiguration">A quick look of the UDF configuration</a></h2>
<p>AsterixDB uses an XML configuration file to describe the UDFs. A user can use it to define and reuse their compiled UDFs for different purposes. Here is a snippet of the configuration used in our <a href="#UDFOnFeeds">previous example</a>:</p>
<div>
<div>
<pre class="source"> &lt;libraryFunction&gt;
&lt;name&gt;addMentionedUsers&lt;/name&gt;
&lt;function_type&gt;SCALAR&lt;/function_type&gt;
&lt;argument_type&gt;TweetType&lt;/argument_type&gt;
&lt;return_type&gt;TweetType&lt;/return_type&gt;
&lt;definition&gt;org.apache.asterix.external.library.AddMentionedUsersFactory&lt;/definition&gt;
&lt;parameters&gt;text&lt;/parameters&gt;
&lt;/libraryFunction&gt;
</pre></div></div>
<p>Here are the explanations of the fields in the configuration file:</p>
<div>
<div>
<pre class="source"> name: The proper name that is used for invoke the function.
function_type: The type of the function.
argument_type: The datatype of the arguments passed in. If there is more than one parameter, separate them with comma(s), e.g., `AINT32,AINT32`.
return_type: The datatype of the returning value.
definition: A reference to the function factory.
parameters: The parameters passed into the function.
</pre></div></div>
<p>In our feeds example, we passed in <tt>&quot;text&quot;</tt> as a parameter to the function so it knows which field to look at to get the Tweet text. If the Twitter API were to change its field names in the future, we can accommodate that change by simply modifying the configuration file instead of recompiling the whole UDF package. This feature can be further utilized in use cases where a user has a Machine Learning algorithm with different trained model files. If you are interested, You can find more examples <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 class="section">
<h2><a name="Unstalling_an_UDF_Library"></a><a name="uninstall">Unstalling an UDF Library</a></h2>
<p>If you want to uninstall the UDF library, put AsterixDB into <tt>INACTVIVE</tt> mode and run following command:</p>
<div>
<div>
<pre class="source"> $ bin/udf.sh -m u -d DATAVERSE_NAME -l LIBRARY_NAME
</pre></div></div></div>
</div>
</div>
</div>
<hr/>
<footer>
<div class="container-fluid">
<div class="row-fluid">
<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>
</div>
</footer>
</body>
</html>