📜  Apache Presto-定制功能应用程序

📅  最后修改于: 2020-11-28 13:05:17             🧑  作者: Mango


创建一个Maven项目以开发Presto定制函数。

SimpleFunctionsFactory.java

创建SimpleFunctionsFactory类以实现FunctionFactory接口。

package com.tutorialspoint.simple.functions;  

import com.facebook.presto.metadata.FunctionFactory; 
import com.facebook.presto.metadata.FunctionListBuilder; 
import com.facebook.presto.metadata.SqlFunction; 
import com.facebook.presto.spi.type.TypeManager;  
import java.util.List;  

public class SimpleFunctionFactory implements FunctionFactory { 
   
   private final TypeManager typeManager;  
   public SimpleFunctionFactory(TypeManager typeManager) { 
      this.typeManager = typeManager; 
   }  
    @Override 
    
   public List listFunctions() { 
      return new FunctionListBuilder(typeManager) 
      .scalar(SimpleFunctions.class) 
      .getFunctions(); 
   } 
}

SimpleFunctionsPlugin.java

创建一个SimpleFunctionsPlugin类以实现Plugin接口。

package com.tutorialspoint.simple.functions;  

import com.facebook.presto.metadata.FunctionFactory; 
import com.facebook.presto.spi.Plugin; 
import com.facebook.presto.spi.type.TypeManager; 
import com.google.common.collect.ImmutableList;  
import javax.inject.Inject; 
import java.util.List; 
import static java.util.Objects.requireNonNull;  

public class SimpleFunctionsPlugin implements Plugin {  
   private TypeManager typeManager; 
   @Inject 
   
   public void setTypeManager(TypeManager typeManager) { 
      this.typeManager = requireNonNull(typeManager, "typeManager is null”); 
      //Inject TypeManager class here 
   }  
   @Override 
   
   public  List getServices(Class type){ 
      if (type == FunctionFactory.class) { 
         return ImmutableList.of(type.cast(new SimpleFunctionFactory(typeManager))); 
      } 
      return ImmutableList.of(); 
   } 
}

添加资源文件

创建在实现包中指定的资源文件。

(com.tutorialspoint.simple.functions.SimpleFunctionsPlugin)

现在移至资源文件位置@ / path / to / resource /

然后添加更改,

com.facebook.presto.spi.Plugin 

pom.xml

将以下依赖项添加到pom.xml文件。

 
  
   
   4.0.0 
   com.tutorialspoint.simple.functions 
   presto-simple-functions  
   jar  
   1.0
   presto-simple-functions
   Simple test functions for Presto 
    
      UTF-8
     
    
       
         com.facebook.presto 
         presto-spi
         0.149 
        
       
         com.facebook.presto 
         presto-main 
         0.149 
        
       
         javax.inject 
         javax.inject 
         1 
        
       
         com.google.guava 
         guava 
         19.0 
       
     
    
      presto-simple-functions  
        
       
          
            org.apache.maven.plugins 
            maven-jar-plugin 
            2.3.2 
          
       
    

SimpleFunctions.java

使用Presto属性创建SimpleFunctions类。

package com.tutorialspoint.simple.functions;  

import com.facebook.presto.operator.Description; 
import com.facebook.presto.operator.scalar.ScalarFunction; 
import com.facebook.presto.operator.scalar.StringFunctions; 
import com.facebook.presto.spi.type.StandardTypes; 
import com.facebook.presto.type.LiteralParameters; 
import com.facebook.presto.type.SqlType;  

public final class SimpleFunctions { 
   private SimpleFunctions() { 
   }  
    
   @Description("Returns summation of two numbers") 
   @ScalarFunction(“mysum") 
   //function name 
   @SqlType(StandardTypes.BIGINT) 
    
   public static long sum(@SqlType(StandardTypes.BIGINT) long num1, 
   @SqlType(StandardTypes.BIGINT) long num2) { 
      return num1 + num2; 
   } 
}

创建应用程序后,编译并执行该应用程序。它将生成JAR文件。复制该文件,然后将JAR文件移动到目标Presto服务器插件目录中。

汇编

mvn compile

执行

mvn package

现在重新启动Presto服务器并连接Presto客户端。然后按照以下说明执行自定义函数应用程序,

$ ./presto --catalog mysql --schema default

询问

presto:default> select mysum(10,10);

结果

_col0  
------- 
  20