简体   繁体   English

如何通过 Java 互操作从 Clojure 中的 Google BigQuery 获取 select 数据?

[英]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.

相关问题 如何从 Java 中的 BigQuery 获取分页数据? - How to get paginated data from BigQuery in Java? 如何将数据从 Google Analytic 流式传输到 Bigquery - How can streaming data from Google Analytic to Bigquery 如何通过 API 或 CLI 在 Google BigQuery 中取消授权查看 - How can I unathorized view in Google BigQuery via API or CLI 如何从 Java 中的 bigquery 读取字节类型? - How to read bytes type from bigquery in Java? Bigquery:如何声明数组变量并使用 select 语句设置数据? - Bigquery : how to declare an array variable and set data with a select statement? 当我尝试将数据从 Google Search Console 提取到 BigQuery 时,如何阻止我的脚本超时? - How do I stop my script from timing out when I try to extract data from Google Search Console to BigQuery? 如何配置从 Google BigQuery 到 Google AlloyDB 的联合查询? - How to configure federated query from Google BigQuery to Google AlloyDB? 将数据从 node.js 添加到 Google BigQuery 表 - Add Data from node.js to Google BigQuery Table 数据工作室如何从 Bigquery 制作普通表 - Data studio how to make a normal table from Bigquery Google Dataflow - 将数据保存到多个 BigQuery 表中 - Google Dataflow - save the data into multiple BigQuery tables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM