0

I can create views using the Java API, but the query needs to be legacy 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();
}

Is there a way to create a BQ view with standard sql using the API?

1
  • I'm not seeing a related option in the API docs. What if you use #standardSQL at the start of the query itself? Commented Jun 9, 2017 at 20:32

2 Answers 2

2

You need to set setUseLegacySQL(false) on the ViewDefinition object.

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

See the API docs here.

Sign up to request clarification or add additional context in comments.

5 Comments

you are right. I was using an older version of the api
here's a link to the "new" version (ignore the big red "this is the old version" warning): developers.google.com/api-client-library/java/apis/bigquery/v2 /boggle
@AndreyFedorov - not following you. That's the older API. The new (more idiomatic) one is here: github.com/GoogleCloudPlatform/google-cloud-java/tree/master/…
oooh so new so idiomatic. could you point me to the setUseLegacySql method you are talking about? it seems my command-f is broken github.com/GoogleCloudPlatform/google-cloud-java/blob/master/…
indeed, the new API unfortunately no longer offers this method : stackoverflow.com/questions/46813165/…
0

With the new API, you can use the "#standardSQL" notation to avoid the default LegacySQL setting (the setUseLegacySql() method no longer exists in the new API).

Here is an example:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.