【问题标题】:how can I get a bigquery table schema in java如何在 java 中获取 bigquery 表模式
【发布时间】:2017-05-06 19:46:36
【问题描述】:

假设我在 BQ 中有这个 myTable 表:

[
  {"name": "executionId", "type": "STRING"},
  {"name":"metadata", "type":"record","fields":[
    {"name":"fileName", "type":"STRING"},
    {"name":"fileType", "type":"STRING"},
    {"name":"errors", "type":"STRING"}
  ]}
]

现在我正在尝试在我的代码中获取表架构。 这是我尝试使用example:

import com.google.cloud.examples.bigquery.snippets.*;

public class MyClass {
    public static void main(String[] args) throws Exception {
        BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
        BigQuerySnippets bigQuerySnippets = new BigQuerySnippets(bigquery);
        Table table = bigQuerySnippets.getTable("MY_DATASET", "myTable");

现在我该如何继续? table 有这个方法吗?

【问题讨论】:

    标签: java google-bigquery


    【解决方案1】:

    Table 扩展 TableInfo,因此您可以从 TableInfo 中获取 TableDefinition,从 TableDefinition 中获取 Schema

    Schema schema = table.getDefinition().getSchema();
    

    【讨论】:

    • 我的代码在第一行失败:BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); 我收到此错误:Exception in thread "main" java.lang.NoSuchMethodError: com.google.cloud.bigquery.BigQueryOptions.getHttpTransportFactory()Lcom/google/cloud/HttpServiceOptions$HttpTransportFactory;
    • 使用这个答案,我得到了 null 的架构,但首先调用 table.reload() 修复了它。
    • 感谢@MingweiSamuel 确实帮了很多忙
    猜你喜欢
    • 1970-01-01
    • 2010-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-09
    • 1970-01-01
    • 2021-01-13
    • 1970-01-01
    相关资源
    最近更新 更多