You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by ja...@apache.org on 2017/03/02 02:54:33 UTC

flink git commit: [FLINK-5794] [doc] Update the documentation about “UDF/UDTF" support have parameters constructor.

Repository: flink
Updated Branches:
  refs/heads/master 72f18dc71 -> bf4eed144


[FLINK-5794] [doc] Update the documentation about \u201cUDF/UDTF" support have parameters constructor.

This closes #3450


Project: http://git-wip-us.apache.org/repos/asf/flink/repo
Commit: http://git-wip-us.apache.org/repos/asf/flink/commit/bf4eed14
Tree: http://git-wip-us.apache.org/repos/asf/flink/tree/bf4eed14
Diff: http://git-wip-us.apache.org/repos/asf/flink/diff/bf4eed14

Branch: refs/heads/master
Commit: bf4eed144428179a2390eb67642f8ac755896fc2
Parents: 72f18dc
Author: \u91d1\u7af9 <ji...@alibaba-inc.com>
Authored: Thu Mar 2 00:58:02 2017 +0800
Committer: Jark Wu <wu...@alibaba-inc.com>
Committed: Thu Mar 2 09:22:54 2017 +0800

----------------------------------------------------------------------
 docs/dev/table_api.md | 41 +++++++++++++++++++++++++++--------------
 1 file changed, 27 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flink/blob/bf4eed14/docs/dev/table_api.md
----------------------------------------------------------------------
diff --git a/docs/dev/table_api.md b/docs/dev/table_api.md
index 1b43b7c..ddcc9e9 100644
--- a/docs/dev/table_api.md
+++ b/docs/dev/table_api.md
@@ -4734,21 +4734,27 @@ If a required scalar function is not contained in the built-in functions, it is
 
 In order to define a scalar function one has to extend the base class `ScalarFunction` in `org.apache.flink.table.functions` and implement (one or more) evaluation methods. The behavior of a scalar function is determined by the evaluation method. An evaluation method must be declared publicly and named `eval`. The parameter types and return type of the evaluation method also determine the parameter and return types of the scalar function. Evaluation methods can also be overloaded by implementing multiple methods named `eval`.
 
-The following example snippet shows how to define your own hash code function:
+The following example shows how to define your own hash code function, register it in the TableEnvironment, and call it in a query. Note that you can configure your scalar function via a constructor before it is registered:
 
 <div class="codetabs" markdown="1">
 <div data-lang="java" markdown="1">
 {% highlight java %}
-public static class HashCode extends ScalarFunction {
+public class HashCode extends ScalarFunction {
+  private int factor = 12;
+  
+  public HashCode(int factor) {
+      this.factor = factor;
+  }
+  
   public int eval(String s) {
-    return s.hashCode() * 12;
+      return s.hashCode() * factor;
   }
 }
 
 BatchTableEnvironment tableEnv = TableEnvironment.getTableEnvironment(env);
 
 // register the function
-tableEnv.registerFunction("hashCode", new HashCode())
+tableEnv.registerFunction("hashCode", new HashCode(10))
 
 // use the function in Java Table API
 myTable.select("string, string.hashCode(), hashCode(string)");
@@ -4761,19 +4767,20 @@ tableEnv.sql("SELECT string, HASHCODE(string) FROM MyTable");
 <div data-lang="scala" markdown="1">
 {% highlight scala %}
 // must be defined in static/object context
-object hashCode extends ScalarFunction {
+class HashCode(factor: Int) extends ScalarFunction {
   def eval(s: String): Int = {
-    s.hashCode() * 12
+    s.hashCode() * factor
   }
 }
 
 val tableEnv = TableEnvironment.getTableEnvironment(env)
 
 // use the function in Scala Table API
+val hashCode = new HashCode(10)
 myTable.select('string, hashCode('string))
 
 // register and use the function in SQL
-tableEnv.registerFunction("hashCode", hashCode)
+tableEnv.registerFunction("hashCode", new HashCode(10))
 tableEnv.sql("SELECT string, HASHCODE(string) FROM MyTable");
 {% endhighlight %}
 </div>
@@ -4823,15 +4830,21 @@ In order to define a table function one has to extend the base class `TableFunct
 
 In the Table API, a table function is used with `.join(Expression)` or `.leftOuterJoin(Expression)` for Scala users and `.join(String)` or `.leftOuterJoin(String)` for Java users. The `join` operator (cross) joins each row from the outer table (table on the left of the operator) with all rows produced by the table-valued function (which is on the right side of the operator). The `leftOuterJoin` operator joins each row from the outer table (table on the left of the operator) with all rows produced by the table-valued function (which is on the right side of the operator) and preserves outer rows for which the table function returns an empty table. In SQL use `LATERAL TABLE(<TableFunction>)` with CROSS JOIN and LEFT JOIN with an ON TRUE join condition (see examples below).
 
-The following examples show how to define a table-valued function and use it:
+The following example shows how to define table-valued function, register it in the TableEnvironment, and call it in a query. Note that you can configure your table function via a constructor before it is registered: 
 
 <div class="codetabs" markdown="1">
 <div data-lang="java" markdown="1">
 {% highlight java %}
 // The generic type "Tuple2<String, Integer>" determines the schema of the returned table as (String, Integer).
 public class Split extends TableFunction<Tuple2<String, Integer>> {
+    private String separator = " ";
+    
+    public Split(String separator) {
+        this.separator = separator;
+    }
+    
     public void eval(String str) {
-        for (String s : str.split(" ")) {
+        for (String s : str.split(separator)) {
             // use collect(...) to emit a row
             collect(new Tuple2<String, Integer>(s, s.length()));
         }
@@ -4842,7 +4855,7 @@ BatchTableEnvironment tableEnv = TableEnvironment.getTableEnvironment(env);
 Table myTable = ...         // table schema: [a: String]
 
 // Register the function.
-tableEnv.registerFunction("split", new Split());
+tableEnv.registerFunction("split", new Split("#"));
 
 // Use the table function in the Java Table API. "as" specifies the field names of the table.
 myTable.join("split(a) as (word, length)").select("a, word, length");
@@ -4859,10 +4872,10 @@ tableEnv.sql("SELECT a, word, length FROM MyTable LEFT JOIN LATERAL TABLE(split(
 <div data-lang="scala" markdown="1">
 {% highlight scala %}
 // The generic type "(String, Int)" determines the schema of the returned table as (String, Integer).
-class Split extends TableFunction[(String, Int)] {
+class Split(separator: String) extends TableFunction[(String, Int)] {
   def eval(str: String): Unit = {
     // use collect(...) to emit a row.
-    str.split(" ").foreach(x -> collect((x, x.length))
+    str.split(separator).foreach(x -> collect((x, x.length))
   }
 }
 
@@ -4870,13 +4883,13 @@ val tableEnv = TableEnvironment.getTableEnvironment(env)
 val myTable = ...         // table schema: [a: String]
 
 // Use the table function in the Scala Table API (Note: No registration required in Scala Table API).
-val split = new Split()
+val split = new Split("#")
 // "as" specifies the field names of the generated table.
 myTable.join(split('a) as ('word, 'length)).select('a, 'word, 'length);
 myTable.leftOuterJoin(split('a) as ('word, 'length)).select('a, 'word, 'length);
 
 // Register the table function to use it in SQL queries.
-tableEnv.registerFunction("split", new Split())
+tableEnv.registerFunction("split", new Split("#"))
 
 // Use the table function in SQL with LATERAL and TABLE keywords.
 // CROSS JOIN a table function (equivalent to "join" in Table API)