The document provides code examples for querying data from BigQuery using ScalikeJDBC and the BigQuery Java Client Library. It shows how to left join tables, use window functions to rank rows, bind parameters, and extract results. Issues addressed include incorrectly specifying the column for a join and not knowing how to bind a custom data type as a parameter.
8. bq {
select(p.result.*, t.result.*).from(
(Post in dataset) as p
).leftJoin(
(Tag in dataset) as t
).on(p.id, t.postId)
}
.one(Post(_))
.toMany(Tag.opt)
.map(PostWithTags)
.list
.run(executor)
10. bq {
select(p.result.*, t.result.*).from(
(Post in dataset) as p
).leftJoin(
(Tag in dataset) as t
).on(p.id, t.postId)
.where
// Don't know how to bind MyDataType as parameter
.eq(t.name, MyDataType("some tag_name"))
}
11. bq {
select(
// window function
p.result.*, rank.over(orderBy(p.postedAt.desc)).as(sqls"rnk")
).from(
(Post in dataset) as p
)
}
.map { resultSet =>
PostWithRank(Post(resultSet), resultSet.int(sqls"rnk"))
}
.list
.run(executor)
23. // JDBC
val statement = conn.prepareStatement(
"select * from user where id = ? and name = ?"
)
// bind parameters
statement.setInt(1, 42)
statement.setString(2, "John")
// BigQuery Java Client Library
val request = QueryRequest.newBuilder(
"select * from user where id = ? and name = ?")
.setPositionalParameters(
Seq(
QueryParameterValue.int64(42),
QueryParameterValue.string("John")
).asJava
)
.build()