[英]How to select data from Google BigQuery in Clojure via Java interop?
I couldn't find any examples online.我在网上找不到任何例子。 Can anyone point me to an example of how to select data from Google BigQuery in Clojure via Java interop?谁能告诉我如何通过 Java 互操作从 Clojure 中的 Google BigQuery 获取 select 数据的示例?
[com.google.cloud/google-cloud-bigquery "2.16.0"]
Here's theJava example Google provides:这是 Google 提供的Java 示例:
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryException;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.QueryJobConfiguration;
import com.google.cloud.bigquery.TableResult;
// Sample to query in a table
public class Query {
public static void main(String[] args) {
// TODO(developer): Replace these variables before running the sample.
String projectId = "MY_PROJECT_ID";
String datasetName = "MY_DATASET_NAME";
String tableName = "MY_TABLE_NAME";
String query =
"SELECT name, SUM(number) as total_people\n"
+ " FROM `"
+ projectId
+ "."
+ datasetName
+ "."
+ tableName
+ "`"
+ " WHERE state = 'TX'"
+ " GROUP BY name, state"
+ " ORDER BY total_people DESC"
+ " LIMIT 20";
query(query);
}
public static void query(String query) {
try {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests.
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
QueryJobConfiguration queryConfig = QueryJobConfiguration.newBuilder(query).build();
TableResult results = bigquery.query(queryConfig);
results
.iterateAll()
.forEach(row -> row.forEach(val -> System.out.printf("%s,", val.toString())));
System.out.println("Query performed successfully.");
} catch (BigQueryException | InterruptedException e) {
System.out.println("Query not performed \n" + e.toString());
}
}
}
I wasn't able to test this code, so you will probably have to do some adjustments, but at least for the general idea:我无法测试此代码,因此您可能需要进行一些调整,但至少对于总体思路:
Dependency: [com.google.cloud/google-cloud-bigquery "2.16.0"]
依赖: [com.google.cloud/google-cloud-bigquery "2.16.0"]
Import in ns
: (:import (com.google.cloud.bigquery BigQueryOptions QueryJobConfiguration BigQuery BigQueryException BigQuery$JobOption))
在ns
中导入 : (:import (com.google.cloud.bigquery BigQueryOptions QueryJobConfiguration BigQuery BigQueryException BigQuery$JobOption))
(defn use-query [query]
(try (let [^BigQuery big-query (.getService (BigQueryOptions/getDefaultInstance))
^QueryJobConfiguration query-config (.build (QueryJobConfiguration/newBuilder query))
results (.query big-query
query-config
(into-array BigQuery$JobOption []))]
(doseq [row (.iterateAll results)
val row]
(printf "%s" val))
(println "Query performed successfully."))
(catch BigQueryException e (printf "Query not performed \n %s" e))
(catch InterruptedException e (printf "Query not performed \n %s" e))))
(let [project-id "MY_PROJECT_ID"
dataset-name "MY_DATASET_NAME"
table-name "MY_TABLE_NAME"
query (str "SELECT name, SUM(number) as total_people\n"
" FROM `"
project-id
"."
dataset-name
"."
table-name
"`"
" WHERE state = 'TX'"
" GROUP BY name, state"
" ORDER BY total_people DESC"
" LIMIT 20")]
(use-query query))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.