FlinkSQL-自定义标量函数ScalarFunction

   public static class HashCode extends ScalarFunction{
        private int factor = 13;
        public HashCode(int factor) {
            this.factor = factor;
        }
        //必须为public名字必须叫eval
        public int eval(String str){
            return str.hashCode()*factor;
        }
    }

  Table sourceTable = tableEnv.fromDataStream(dataStream, "f0 as id, f1 as ts, f2 as temp,pt.proctime");
        //在环境中注册UDF
        HashCode hashCode = new HashCode(5);
        tableEnv.registerFunction("hashCode",hashCode);
        //tableAPI
        Table resultTable = sourceTable.select("id,ts,hashCode(id)");
        tableEnv.toAppendStream(resultTable, Row.class).print();
        //SQL
        tableEnv.createTemporaryView("sensor",sourceTable);
        Table resultSqlTbale = tableEnv.sqlQuery("select id,ts,hashCode(id) from sensor");
        tableEnv.toAppendStream(resultSqlTbale,Row.class).print();

猜你喜欢

转载自blog.csdn.net/zhaomengsen/article/details/121258270