如何通过Java互操作从Clojure中的Google BigQuery中选择数据



我在网上找不到任何示例。有人能给我举一个如何通过Java互操作从Clojure中的Google BigQuery中选择数据的例子吗?

[com.google.cloud/google-cloud-bigquery "2.16.0"]

以下是谷歌提供的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_peoplen"
+ " 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());
}
}
}

我无法测试此代码,因此您可能需要进行一些调整,但至少对于总体想法:

依赖项:[com.google.cloud/google-cloud-bigquery "2.16.0"]

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_peoplen"
" FROM `"
project-id
"."
dataset-name
"."
table-name
"`"
" WHERE state = 'TX'"
" GROUP BY name, state"
" ORDER BY total_people DESC"
" LIMIT 20")]
(use-query query))

最新更新