blob: 10e080f9f47c6d718493a69805f86ba230adfc10 [file] [log] [blame]
<!DOCTYPE html>
<!--
| Generated by Apache Maven Doxia Site Renderer 1.8 from src/site/markdown/metron-stellar/stellar-common/3rdPartyStellar.md at 2019-05-14
| 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="20190514" />
<meta http-equiv="Content-Language" content="en" />
<title>Metron &#x2013; Custom Stellar 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>
<script type="text/javascript">
$( document ).ready( function() { $( '.carousel' ).carousel( { interval: 3500 } ) } );
</script>
</head>
<body class="topBarDisabled">
<div class="container-fluid">
<div id="banner">
<div class="pull-left"><a href="http://metron.apache.org/" id="bannerLeft"><img src="../../images/metron-logo.png" alt="Apache Metron" width="148px" height="48px"/></a></div>
<div class="pull-right"></div>
<div class="clear"><hr/></div>
</div>
<div id="breadcrumbs">
<ul class="breadcrumb">
<li class=""><a href="http://www.apache.org" class="externalLink" title="Apache">Apache</a><span class="divider">/</span></li>
<li class=""><a href="http://metron.apache.org/" class="externalLink" title="Metron">Metron</a><span class="divider">/</span></li>
<li class=""><a href="../../index.html" title="Documentation">Documentation</a><span class="divider">/</span></li>
<li class="active ">Custom Stellar Functions</li>
<li id="publishDate" class="pull-right"><span class="divider">|</span> Last Published: 2019-05-14</li>
<li id="projectVersion" class="pull-right">Version: 0.7.1</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">User Documentation</li>
<li><a href="../../index.html" title="Metron"><span class="icon-chevron-down"></span>Metron</a>
<ul class="nav nav-list">
<li><a href="../../CONTRIBUTING.html" title="CONTRIBUTING"><span class="none"></span>CONTRIBUTING</a></li>
<li><a href="../../Upgrading.html" title="Upgrading"><span class="none"></span>Upgrading</a></li>
<li><a href="../../metron-analytics/index.html" title="Analytics"><span class="icon-chevron-right"></span>Analytics</a></li>
<li><a href="../../metron-contrib/metron-docker/index.html" title="Docker"><span class="none"></span>Docker</a></li>
<li><a href="../../metron-contrib/metron-performance/index.html" title="Performance"><span class="none"></span>Performance</a></li>
<li><a href="../../metron-deployment/index.html" title="Deployment"><span class="icon-chevron-right"></span>Deployment</a></li>
<li><a href="../../metron-interface/index.html" title="Interface"><span class="icon-chevron-right"></span>Interface</a></li>
<li><a href="../../metron-platform/index.html" title="Platform"><span class="icon-chevron-right"></span>Platform</a></li>
<li><a href="../../metron-sensors/index.html" title="Sensors"><span class="icon-chevron-right"></span>Sensors</a></li>
<li><a href="../../metron-stellar/stellar-3rd-party-example/index.html" title="Stellar-3rd-party-example"><span class="none"></span>Stellar-3rd-party-example</a></li>
<li><a href="../../metron-stellar/stellar-common/index.html" title="Stellar-common"><span class="icon-chevron-down"></span>Stellar-common</a>
<ul class="nav nav-list">
<li class="active"><a href="#"><span class="none"></span>3rdPartyStellar</a></li>
</ul>
</li>
<li><a href="../../metron-stellar/stellar-zeppelin/index.html" title="Stellar-zeppelin"><span class="none"></span>Stellar-zeppelin</a></li>
<li><a href="../../use-cases/index.html" title="Use-cases"><span class="icon-chevron-right"></span>Use-cases</a></li>
</ul>
</li>
</ul>
<hr />
<div id="poweredBy">
<div class="clear"></div>
<div class="clear"></div>
<div class="clear"></div>
<div class="clear"></div>
<a href="http://maven.apache.org/" title="Built by Maven" class="poweredBy"><img class="builtBy" alt="Built by Maven" src="../../images/logos/maven-feather.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>Custom Stellar Functions</h1>
<p><a name="Custom_Stellar_Functions"></a></p>
<p>Metron is fundamentally a programmable, extensible system and Stellar is the extension language. We have some great Stellar functions available out of the box and we&#x2019;ll be adding more over time, but they may not quite scratch quite your particular itch.</p>
<p>Of course, we&#x2019;d love to have your contribution inside of Metron if you think it general purpose enough, but not every function is general-purpose or it may rely on libraries those licenses aren&#x2019;t acceptable for an Apache project. In that case, then you will be wondering how to add your custom function to a running instance of Metron.</p>
<div class="section">
<h2><a name="Building_Your_Own_Function"></a>Building Your Own Function</h2>
<p>Let&#x2019;s say that I need a function that returns the current time in milliseconds since the epoch. I notice that there&#x2019;s nothing like that currently in Metron, so I embark on the adventure of adding it for my cluster.</p>
<p>I will presume that you have an installed Metron into your local maven repo via <tt>mvn install</tt> . In the future, when we publish to a maven repo, you will not need this. I will depend on 0.4.2 for the purpose of this demonstration</p>
<div class="section">
<h3><a name="Hack.2C_Hack.2C_Hack"></a>Hack, Hack, Hack</h3>
<p>I like to use Maven, so we&#x2019;ll use that for this demonstration, but you can use whatever build system that you like. Here&#x2019;s my favorite way to build a project with groupId <tt>com.mycompany.stellar</tt> and artifactId of <tt>tempus</tt> <tt>mvn archetype:create -DgroupId=com.mycompany.stellar -DartifactId=tempus -DarchetypeArtifactId=maven-archetype-quickstart</tt></p>
<p>First, we should depend on <tt>metron-common</tt> and we can do that by adjusting the <tt>pom.xml</tt> just created:</p>
<div>
<div>
<pre class="source">&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;groupId&gt;com.mycompany.stellar&lt;/groupId&gt;
&lt;artifactId&gt;tempus&lt;/artifactId&gt;
&lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
&lt;packaging&gt;jar&lt;/packaging&gt;
&lt;name&gt;Stellar Time Functions&lt;/name&gt;
&lt;url&gt;http://mycompany.com&lt;/url&gt;
&lt;properties&gt;
&lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
&lt;/properties&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.metron&lt;/groupId&gt;
&lt;artifactId&gt;metron-common&lt;/artifactId&gt;
&lt;version&gt;0.4.2&lt;/version&gt;
&lt;!-- NOTE: We will want to depend on the deployed common on the classpath. --&gt;
&lt;scope&gt;provided&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;junit&lt;/groupId&gt;
&lt;artifactId&gt;junit&lt;/artifactId&gt;
&lt;version&gt;3.8.1&lt;/version&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;/project&gt;
</pre></div></div>
<p>Let&#x2019;s add our implementation in <tt>src/main/java/com/mycompany/stellar/TimeFunctions.java</tt> with the following content:</p>
<div>
<div>
<pre class="source">package com.notmetron.stellar;
import org.apache.metron.stellar.dsl.Context;
import org.apache.metron.stellar.dsl.ParseException;
import org.apache.metron.stellar.dsl.Stellar;
import org.apache.metron.stellar.dsl.StellarFunction;
import java.util.List;
public class TimeFunction {
@Stellar( name=&quot;NOW&quot;,
description = &quot;Right now!&quot;,
params = {},
returns=&quot;Timestamp&quot;
)
public static class Now implements StellarFunction {
public Object apply(List&lt;Object&gt; list, Context context) throws ParseException {
return System.currentTimeMillis();
}
public void initialize(Context context) { }
public boolean isInitialized() {
return true;
}
}
}
</pre></div></div>
<p>Now we can build the project via <tt>mvn package</tt> which will create a <tt>target/tempus-1.0-SNAPSHOT.jar</tt> file.</p></div></div>
<div class="section">
<h2><a name="Install_the_Function"></a>Install the Function</h2>
<p>Now that we have a jar with our custom function, we must make Metron aware of it.</p>
<div class="section">
<h3><a name="Deploy_the_Jar"></a>Deploy the Jar</h3>
<p>First you need to place the jar in HDFS, if we have it on an access node, one way to do that is:</p>
<ul>
<li><tt>hadoop fs -put tempus-1.0-SNAPSHOT.jar /apps/metron/stellar</tt> This presumes that:</li>
<li>you&#x2019;ve standardized on <tt>/apps/metron/stellar</tt> as the location for custom jars</li>
<li>you are running the command from an access node with the <tt>hadoop</tt> command installed</li>
<li>you are running from a user that has write access to <tt>/apps/metron/stellar</tt></li>
</ul></div>
<div class="section">
<h3><a name="Set_Global_Config"></a>Set Global Config</h3>
<p>You may not need this if your Metron administrator already has this setup.</p>
<p>With that dispensed with, we need to ensure that Metron knows to look at that location. We need to ensure that the <tt>stellar.function.paths</tt> property in the <tt>global.json</tt> is in place that makes Metron aware to look for Stellar functions in <tt>/apps/metron/stellar</tt> on HDFS.</p>
<p>This property looks like, the following for a vagrant install</p>
<div>
<div>
<pre class="source">{
&quot;es.clustername&quot;: &quot;metron&quot;,
&quot;es.ip&quot;: &quot;node1&quot;,
&quot;es.port&quot;: &quot;9300&quot;,
&quot;es.date.format&quot;: &quot;yyyy.MM.dd.HH&quot;,
&quot;stellar.function.paths&quot; : &quot;hdfs://node1:8020/apps/metron/stellar/.*.jar&quot;,
}
</pre></div></div>
<p>The <tt>stellar.function.paths</tt> property takes a comma separated list of URIs or URIs with regex expressions at the end. Also, note path is prefaced by the HDFS default name, which, if you do not know, can be found by executing, <tt>hdfs getconf -confKey fs.default.name</tt>, such as</p>
<div>
<div>
<pre class="source">[root@node1 ~]# hdfs getconf -confKey fs.default.name
hdfs://node1:8020
</pre></div></div>
</div>
<div class="section">
<h3><a name="Use_the_Function"></a>Use the Function</h3>
<p>Now that we have deployed the function, if we want to use it, any running topologies that use Stellar will need to be restarted.</p>
<p>Beyond that, let&#x2019;s take a look at it in the REPL:</p>
<div>
<div>
<pre class="source">Stellar, Go!
Please note that functions are loading lazily in the background and will be unavailable until loaded fully.
{es.clustername=metron, es.ip=node1, es.port=9300, es.date.format=yyyy.MM.dd.HH, stellar.function.paths=hdfs://node1:8020/apps/metron/stellar/.*.jar, profiler.client.period.duration=1, profiler.client.period.duration.units=MINUTES}
[Stellar]&gt;&gt;&gt; # Get the help for NOW
[Stellar]&gt;&gt;&gt; ?NOW
Functions loaded, you may refer to functions now...
NOW
Description: Right now!
Returns: Timestamp
[Stellar]&gt;&gt;&gt; # Try to run the NOW function, which we added:
[Stellar]&gt;&gt;&gt; NOW()
1488400515655
[Stellar]&gt;&gt;&gt; # Looks like I got a timestamp, success!
</pre></div></div></div></div>
</div>
</div>
</div>
<hr/>
<footer>
<div class="container-fluid">
<div class="row-fluid">
© 2015-2016 The Apache Software Foundation. Apache Metron, Metron, Apache, the Apache feather logo,
and the Apache Metron project logo are trademarks of The Apache Software Foundation.
</div>
</div>
</footer>
</body>
</html>