Apache Presto - Application de fonction personnalisée

Créez un projet Maven pour développer la fonction personnalisée Presto.

SimpleFunctionsFactory.java

Créez la classe SimpleFunctionsFactory pour implémenter l'interface 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<SqlFunction> listFunctions() { 
      return new FunctionListBuilder(typeManager) 
      .scalar(SimpleFunctions.class) 
      .getFunctions(); 
   } 
}

SimpleFunctionsPlugin.java

Créez une classe SimpleFunctionsPlugin pour implémenter l'interface 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 <T> List<T> getServices(Class<T> type){ 
      if (type == FunctionFactory.class) { 
         return ImmutableList.of(type.cast(new SimpleFunctionFactory(typeManager))); 
      } 
      return ImmutableList.of(); 
   } 
}

Ajouter un fichier de ressources

Créez un fichier de ressources spécifié dans le package d'implémentation.

(com.tutorialspoint.simple.functions.SimpleFunctionsPlugin)

Maintenant, déplacez-vous vers l'emplacement du fichier de ressources @ / chemin / vers / ressource /

Puis ajoutez les modifications,

com.facebook.presto.spi.Plugin

pom.xml

Ajoutez les dépendances suivantes au fichier pom.xml.

<?xml version = "1.0"?> 
<project xmlns = "http://maven.apache.org/POM/4.0.0"  
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0  
    http://maven.apache.org/xsd/maven-4.0.0.xsd">  
   
   <modelVersion>4.0.0</modelVersion> 
   <groupId>com.tutorialspoint.simple.functions</groupId> 
   <artifactId>presto-simple-functions</artifactId>  
   <packaging>jar</packaging>  
   <version>1.0</version>
   <name>presto-simple-functions</name>
   <description>Simple test functions for Presto</description> 
   <properties> 
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>  
   <dependencies> 
      <dependency> 
         <groupId>com.facebook.presto</groupId> 
         <artifactId>presto-spi</artifactId>
         <version>0.149</version> 
      </dependency>  
      <dependency> 
         <groupId>com.facebook.presto</groupId> 
         <artifactId>presto-main</artifactId> 
         <version>0.149</version> 
      </dependency>  
      <dependency> 
         <groupId>javax.inject</groupId> 
         <artifactId>javax.inject</artifactId> 
         <version>1</version> 
      </dependency>  
      <dependency> 
         <groupId>com.google.guava</groupId> 
         <artifactId>guava</artifactId> 
         <version>19.0</version> 
      </dependency> 
   </dependencies>  
   <build> 
      <finalName>presto-simple-functions</finalName>  
      <plugins>  
      <!-- Make this jar executable --> 
         <plugin> 
            <groupId>org.apache.maven.plugins</groupId> 
            <artifactId>maven-jar-plugin</artifactId> 
            <version>2.3.2</version> 
         </plugin> 
      </plugins> 
   </build> 
</project>

SimpleFunctions.java

Créez une classe SimpleFunctions à l'aide des attributs Presto.

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; 
   } 
}

Une fois l'application créée, compilez et exécutez l'application. Il produira le fichier JAR. Copiez le fichier et déplacez le fichier JAR dans le répertoire du plugin du serveur Presto cible.

Compilation

mvn compile

Exécution

mvn package

Redémarrez maintenant le serveur Presto et connectez le client Presto. Exécutez ensuite l'application de fonction personnalisée comme expliqué ci-dessous,

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

Requete

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

Résultat

_col0  
------- 
  20