Go 迭代器从 Bigquery 中读取 100 万行比 Java 或 Kotlin 慢 10 倍?
时间:2024-02-25
浏览:482
作者:佚名
大家好,今天本人给大家带来文章《Go 迭代器从 Bigquery 中读取 100 万行比 Java 或 Kotlin 慢 10 倍?》,文中内容主要涉及到,如果你对Golang方面的知识点感兴趣,那就请各位朋友继续看下去吧~希望能真正帮到你们,谢谢!
问题内容
我的目的是使用 go 查询 biquery 并为 elasticsearch 中的一些字段建立索引。这将是一项一次性批处理作业。由于团队拥有 java 知识,我们决定对两种语言进行基准测试。我注意到 go 使用“迭代器方式”运行得很慢。
为什么会出现这种时间差异?
我是否缺少 go 中的某些客户端或查询配置,或者这是预期的行为?
如何缩短阅读时间?
java/kotlin 和 go:
- 在完全相同的环境中运行。
- bigquery 数据集 200gb
- 相同的“sql”查询,连接两个表并仅检索 12 个左右字段。限制 100 万行。
- 使用交互式查询运行两种语言的 gcp 文档中的示例代码:https://cloud.google.com/bigquery/docs/running-queries。
(我简化了代码)
go 1.16.3
...
type test struct {
testno *big.rat `bigquery:"testno,nullable"`
testid bigquery.nullstring `bigquery:"testid"`
testtime bigquery.nulldatetime `bigquery:"testtime"`
firstname bigquery.nullstring `bigquery:"firstname"`
lastname bigquery.nullstring `bigquery:"lastname"`
items []itemtest `bigquery:"f0_"`
}
type itemtest struct {
itemtype bigquery.nullstring `bigquery:"itemtype"`
itemno bigquery.nullstring `bigquery:"itemno"`
productno *big.rat `bigquery:"productno,nullable"`
qty *big.rat `bigquery:"qty,nullable"`
name bigquery.nullstring `bigquery:"name"`
price *big.rat `bigquery:"price,nullable"`
}
ctx := context.background()
client, err := bigquery.newclient(ctx, projectid)
if err != nil {
// todo: handle error.
}
q := client.query(myquery)
it, err := q.read(ctx)
if err != nil {
// todo: handle error.
}
for {
start := time.now().utc()
var t test
err := it.next(&t)
if err == iterator.done {
break
}
if err != nil {
// todo: handle error.
}
end += time.since(start)
indextoes(t)
}
fmt.println(end) //13 minutes.
...
需要13分钟来读取并映射到 go 结构。
科特林
...
val start: BigDecimal = Instant.now().toEpochMilli().toBigDecimal().setScale(3)
val bigquery = BigQueryOptions.newBuilder()
.setCredentials(credentials)
.setProjectId(PROJECT_ID)
.build()
.service
val queryConfig = QueryJobConfiguration.newBuilder(query).build()
val tableResult = bigquery.query(queryConfig)
val test = results.iterateAll()
.map { myMapper.mapToTest(it) }
val end: BigDecimal = Instant.now().toEpochMilli().toBigDecimal().setScale(3)
logResults(start, end) // 60000ms = 1minute
fun logResults(start: BigDecimal, end: BigDecimal){
println("query: " + (pitB - pitA).setScale(0) + "ms")
}
//iterate through test and indexing at the same time
...
需要1 分钟...
正确答案
这两个片段都不完整,因此尚不清楚这是否是同类的。如果您想知道 Go 程序的时间都花在哪里,请考虑利用 pprof。
另一件事需要指出的是,如果您正在读取数百万行的查询输出,您将需要查看 BigQuery Storage API。使用它而不是您当前正在测试的迭代器可以使这两种语言的速度更快。
到这里,我们也就讲完了《Go 迭代器从 Bigquery 中读取 100 万行比 Java 或 Kotlin 慢 10 倍?》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
热门专题
