- Apache Presto 教程
- Apache Presto - 首頁
- Apache Presto - 概述
- Apache Presto - 架構
- Apache Presto - 安裝
- Apache Presto - 配置
- Apache Presto - 管理
- Apache Presto - SQL 操作
- Apache Presto - SQL 函式
- Apache Presto - MySQL 聯結器
- Apache Presto - JMX 聯結器
- Apache Presto - HIVE 聯結器
- Apache Presto - KAFKA 聯結器
- Apache Presto - JDBC 介面
- 自定函式應用程式
- Apache Presto 實用資源
- Apache Presto - 快速指南
- Apache Presto - 實用資源
- Apache Presto - 討論
Apache Presto - 自定函式應用程式
建立一個 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<SqlFunction> 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 <T> List<T> getServices(Class<T> 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 檔案。
<?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
使用 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
廣告