Presto学习笔记——Go客户端连接Presto

news/2024/9/26 5:15:42

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)}// 迭代结果行for rows.Next() {var col1 stringvar col2 intif err := rows.Scan(&col1, &col2); err != nil {fmt.Println("扫描行失败:", err)return}fmt.Println(col1, col2)}
}

参考:golang+presto查询在数据平台中ad hoc查询

如果想同时查询多行的话,也可以先定义一个struct

type YourTable struct {Col1 stringCol2 intCol3 float64// 其他列...
}

然后在迭代查询的时候使用这个struct

// 迭代结果行
for rows.Next() {var row YourTableif 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)}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

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/30582.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

Flink Batch Hash Aggregate

数据类型要求 BatchPhysicalHashAggRule match 条件会判断 isAggBufferFixedLength(agg) 为什么要求 aggCall 的类型是 Fixed Length 的才可以使用 HashAggregate ? 因为在 HashAggregate 中, 依赖于 BytesHashMap 数据结构来存储 keyValue 数据. 而 ByteHashMap 不支持变长的…

checkpoint防火墙测试授权申请

本文介绍如何在线申请checkpoint防火墙的测试授权 请先确保已注册官网账号并能正常登录Product Center,并安装好checkpoint并配置好管理IP (授权申请需要用到设备IP地址,不需要连网) (官网账号最好使用公司邮箱申请) Product Center 链接 正常登录后可看到如下图内容其中sel…

扩展实现Unity协程的完整栈跟踪

现如今Unity中的协程(Coroutine)方案已显得老旧,Unitask等异步方案可以直接解决如异常捕获等各类问题, 并且Unity官方也在开发一套异步方案,但现阶段还是需要在协程这个方案上继续琢磨。Unity协程中无法输出完整的栈跟踪,因为协程编译后会转换为IL编码的状态机,中间存在…

Object.values()对象遍历

Object.keys() 对象的遍历返回给定对象所有可枚举属性的数组;是属性名组成的数组let obj = { a: 1, b: 2, c: 3 };Object.keys(obj).map((key) => {console.log(key, obj[key]);}); Object.values() 对象的遍历 返回一个给定对象自身的所有属性值的数组; 是属性值组成的…

浅析OSERDESE3

在高速接口的应用场景下,我们会经常听说SerDes(Serializer-Deserializer)这个词,也就是串行器和解串器,更为通俗的讲就是进行串并转换的。在Xilinx的FPGA中提供了ISERDES(提供串行数据到并行数据的转换)和OSERDES(提供并行数据到串行数据的转换)。在7系列的FPGA里面提供…

玩转AI,笔记本电脑安装属于自己的Llama 3 8B大模型和对话客户端

2024 年 4 月 18 日,Meta**开源**了 Llama 3 大模型,把 AI 的门槛降低到了最低,这是人工智能领域的一个重要飞跃。我们个人也可以部署大模型了,这简直就是给个人开发者发了个大红包!Llama 3 模型有不同的参数版本,本文主要分享我在个人笔记本电脑是部署 8B 参数过程和编写…

ABC353 | 如同流星划过天空

AtCoder Beginner Contest 353 (7/7)ABC353 | 如同流星划过天空 A. & B. 依题意模拟即可。 C. 注意只有 \(f(x,y)\) 需要对 \(10^8\) 取模,\(f\) 的求和不需要。 关注到 \(a_i \in [1,10^8)\),故 \(a_i + a_j \in [2,2 \times 10^8)\)。 从而 \(f(x,y)=[x+y<10^8](x…

代码随想录算法训练营第四天 | 24.两辆交换链表中的节点

23.两两交换链表中的两个节点 题目链接 文章讲解 视频讲解时间复杂度 o(n) 空间复杂度 o(1)tips: 画图,并将每一步操作用伪代码写出来,然后将代码理顺可以避免修改代码的麻烦 初始化的时候就要将previous初始化为current的前一个节点,这样可以保证循环的第一步满足循环要求c…