Presto学习笔记——Go客户端连接Presto
1.查询PrestoDB(facebook版本)
1.创建PrestoDB环境
使用docker创建presto测试环境
https://hub.docker.com/r/prestodb/presto/tags
拉取镜像
docker pull prestodb/presto:0.284
启动
docker run -p 8080:8080 -ti -v /Users/lintong/Downloads/config.properties:/opt/presto-server/etc/config.properties -v /Users/lintong/Downloads/jvm.config:/opt/presto-server/etc/jvm.config prestodb/presto:0.284
其中config.properties配置文件
coordinator=true node-scheduler.include-coordinator=true http-server.http.port=8080 query.max-memory=5GB query.max-memory-per-node=1GB discovery-server.enabled=true discovery.uri=http://localhost:8080
jvm.config
-server -Xmx4G -XX:+UseG1GC -XX:G1HeapRegionSize=32M -XX:+UseGCOverheadLimit -XX:+ExplicitGCInvokesConcurrent -XX:+HeapDumpOnOutOfMemoryError -XX:+ExitOnOutOfMemoryError -Djdk.attach.allowAttachSelf=true
配置参考:https://github.com/prestodb/presto/tree/master/docker/etc
https://prestodb.github.io/docs/current/installation/deployment.html
访问localhost:8080

2.使用client连接PrestoDB
可以使用presto-go-client,官方文档
https://github.com/prestodb/presto-go-client
引入依赖
go get github.com/prestodb/presto-go-client/presto
go连接数据库使用的是database/sql,还需要额外引入具体数据库的driver,比如
presto的github.com/prestodb/presto-go-client/presto
mysql的github.com/go-sql-driver/mysql
impala的github.com/bippio/go-impala
查询presto代码
import (
"database/sql"
"fmt"
_ "github.com/prestodb/presto-go-client/presto"
)
func main() {
dsn := "http://user@localhost:8080?catalog=default&schema=test"
db, err := sql.Open("presto", dsn)
if err != nil {
fmt.Println(err)
}
rows, err := db.Query("SELECT name, age FROM foobar WHERE id=?")
if err != nil {
fmt.Println(err)
}
defer func() {
_ = db.Close()
}()
// 迭代结果行
for rows.Next() {
var col1 string
var col2 int
if err := rows.Scan(&col1, &col2); err != nil {
fmt.Println("扫描行失败:", err)
return
}
fmt.Println(col1, col2)
}
}
参考:golang+presto查询在数据平台中ad hoc查询
如果想同时查询多行的话,也可以先定义一个struct
type YourTable struct {
Col1 string
Col2 int
Col3 float64
// 其他列...
}
然后在迭代查询的时候使用这个struct
// 迭代结果行
for rows.Next() {
var row YourTable
if err := rows.Scan(&row.Col1, &row.Col2, &row.Col3); err != nil {
fmt.Println("扫描行失败:", err)
return
}
fmt.Println(row)
}
3.使用REST API连接PrestoDB
参考官方文档
https://prestodb.io/docs/current/develop/client-protocol.html
其实presto-go-client底层也是使用presto的REST API的POST请求来提交任务,参考源码
https://github.com/prestodb/presto-go-client/tree/master/presto#L635
2.查询Trino(社区版本)
1.创建Trino环境
创建Trino环境用于测试可以使用docker
https://hub.docker.com/r/trinodb/trino/tags
拉取镜像
docker pull trinodb/trino:435
启动
docker run -p 8080:8080 -ti trinodb/trino:435
访问localhost:8080,默认密码不启用,随便输入一个用户名

界面

2.使用client连接trino
可以使用trino-go-client,官方文档
https://github.com/trinodb/trino-go-client
引入依赖
go get github.com/trinodb/trino-go-client/trino
查询trino代码
import (
"database/sql"
"fmt"
)
import _ "github.com/trinodb/trino-go-client/trino"
func main() {
dsn := "http://user@localhost:8080?catalog=default&schema=test"
db, err := sql.Open("trino", dsn)
if err != nil {
fmt.Println(err)
}
defer func() {
_ = db.Close()
}()
db.Query("SELECT * FROM foobar WHERE id=?", 1, sql.Named("X-Trino-User", string("Alice")))
}
3.使用REST API连接TrinoDB
参考官方文档
https://trino.io/docs/current/develop/client-protocol.html
其实trino-go-client底层也是使用trino的REST API的POST请求来提交任务,参考源码
https://github.com/trinodb/trino-go-client/blob/master/trino/trino.go#L820
本文只发表于博客园和tonglin0325的博客,作者:tonglin0325,转载请注明原文链接:https://www.cnblogs.com/tonglin0325/p/18187831

浙公网安备 33010602011771号