我正在使用标准的 go sql 包与 AWS Athena 进行交互。
我的查询为每条记录返回一个 uuid(字符串)和一组电子邮件。
这是代码:
package main
import (
"fmt"
"database/sql"
_ "github.com/segmentio/go-athena"
_"encoding/json"
)
type Contact struct {
userid string
emails []string
}
func main() {
fmt.Println("hello")
db, err := sql.Open("athena", "db=example")
if err != nil {
panic(err)
}
rows, err := db.Query("SELECT userid, transform(value.emails, x -> x.value) from database LIMIT 10")
// Returns
// Row 1: "abc-123", ["email1@gmail.com", "email2@gmail.com"]
// Row 2: "def-456", ["email3@gmail.com"]
if err != nil {
panic(err)
}
for rows.Next() {
var contact Contact
rows.Scan(&contact.userid, &contact.emails)
fmt.Println(contact)
}
}
但是,我在
for
循环中收到此错误:panic: unknown type `array` with value [email1@gmail.com]
我对提到的
array
类型感到困惑,我无法理解错误。如何将返回的电子邮件列表映射到 Contact 结构中的一段字符串?
最佳答案
Athena 支持结构化数据类型。例如结构数据类型数组:
Structural types
ARRAY < data_type >
从您收到的消息中,我假设列 email 的类型为
ARRAY<VARCHAR>
.另外segmentio/go-athena
在不受支持的操作上出现 panic ,例如 Begin
用于事务(Athena 不支持)。要读取 Go 数组中的数据,您必须输入一些逻辑。见 read "SELECT *" columns into []string in go或 Read a Postgresql array directly into a Golang Slice作为首发。正如您在 pq 驱动程序中看到的那样,读取数组的实现方式可能与仅扫描一行不同
关于go - sql.Rows 到字符串 slice ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59876684/