【问题标题】:How do I create a BigQuery view with standard SQL using the Java API?如何使用 Java API 使用标准 SQL 创建 BigQuery 视图?
【发布时间】:2017-06-09 20:06:58
【问题描述】:

我可以使用 Java API 创建视图,但查询需要是旧版 sql:

public void createView(String dataSet, String viewName, String query) throws Exception {

    Table content = new Table();
    TableReference tableReference = new TableReference();
    tableReference.setTableId(viewName);
    tableReference.setDatasetId(dataSet);
    tableReference.setProjectId(projectId);
    content.setTableReference(tableReference);

    ViewDefinition view = new ViewDefinition();

    view.setQuery(query);
    content.setView(view);
    LOG.debug("View to create: " + content);
    try {
        if (tableExists(dataSet, viewName)) {
            bigquery.tables().delete(projectId, dataSet, viewName).execute();
        }
    } catch (Exception e) {
        LOG.error("Could not delete table", e);
    }

    bigquery.tables().insert(projectId, dataSet, content).setProjectId(projectId).execute();
}

有没有办法使用 API 使用标准 sql 创建 BQ 视图?

【问题讨论】:

  • 我在 API 文档中没有看到相关选项。如果您在查询本身的开头使用#standardSQL 会怎样?

标签: google-bigquery


【解决方案1】:

您需要在ViewDefinition 对象上设置setUseLegacySQL(false)

[..]
ViewDefinition view = new ViewDefinition();
view.setQuery(query);
view.setUseLegacySql(false); //<-- you're missing this
content.setView(view);
[..]

请参阅 API 文档 here

【讨论】:

【解决方案2】:

使用新 API,您可以使用“#standardSQL”表示法来避免默认的 LegacySQL 设置(新 API 中不再存在 setUseLegacySql() 方法)。

这是一个例子:

ViewDefinition tableDefinition = ViewDefinition.newBuilder("#standardSQL\n  WITH A AS (select 1 as foo) SELECT * from A").build();

【讨论】:

    猜你喜欢
    • 2017-05-14
    • 2017-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-23
    • 2018-09-10
    • 1970-01-01
    相关资源
    最近更新 更多