In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to use the scalar function of the Flink custom function, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Background
Flink itself provides us with a large number of built-in functions, which can already meet most of our needs, but if we do encounter some special scenarios that cannot meet our needs, we can customize the function to solve it. Today we will focus on the simplest custom function-ScalarFunction.
An example to explain the definition function
First of all, let's implement the simplest scalar function. The main function is to add and operate on two numbers. To implement the scalar function, we need to inherit the abstract class ScalarFunction, and then implement the eval method. The code is as follows:
Public static class SumFunction extends ScalarFunction {
Public Integer eval (Integer a, Integer b) {
Return a + b
}
}
Register the function through the program / / register the function through the program
SumFunction sumFunction = new SumFunction ()
TableEnv.registerFunction ("mysum", sumFunction)
Table table1 = tableEnv.sqlQuery ("select mysum (1Magne2)")
TableEnv.toAppendStream (table1, Row.class). Print ()
Register a function through sql
/ / register the function through sql
String className = SumFunction.class.getName ()
String sql = "create temporary function default_catalog.default_database.mysum1" +
"as'" + className + "'"
TableEnv.sqlUpdate (sql)
Table table2 = tableEnv.sqlQuery ("select mysum1 (3Magol 4)")
TableEnv.toAppendStream (table2, Row.class). Print ()
Then we can list all the functions through the listFunctions method of the StreamTableEnvironment class to see if our custom function is among them.
/ / list all the functions to see if they include the functions we defined.
String [] functions = tableEnv.listFunctions ()
Stream.of (functions) .filter (f-> f.startsWith ("mysum")) .forEach (System.out::println)
Finally, if the custom jar and the main program are not in a jar package, remember to add the jar containing the custom function to the classpath through the parameter-classpath (abbreviated-C) when submitting the task from the command line.
The above is all the contents of the article "how to use scalar functions of Flink custom functions". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.