Create a view with DDL
Stay organized with collections
Save and categorize content based on your preferences.
Create a view using a DDL query.
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License
, and code samples are licensed under the Apache 2.0 License
. For details, see the Google Developers Site Policies
. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis content demonstrates how to create a view in BigQuery using a Data Definition Language (DDL) query, specifically through code samples in Java, Node.js, and Python.\u003c/p\u003e\n"],["\u003cp\u003eThe code examples showcase creating a view with options for expiration, friendly name, description, and labels, as well as demonstrating how to select from an existing table.\u003c/p\u003e\n"],["\u003cp\u003eEach code sample is provided with setup instructions and links to the relevant API reference documentation and it outlines how to authenticate using Application Default Credentials.\u003c/p\u003e\n"],["\u003cp\u003eThe sample code uses a public dataset, \u003ccode\u003ebigquery-public-data.usa_names.usa_1910_current\u003c/code\u003e, to show how to create a view with filtering applied.\u003c/p\u003e\n"],["\u003cp\u003eThe created views in the samples expire after 48 hours and have org_unit development label.\u003c/p\u003e\n"]]],[],null,["# Create a view with DDL\n\nCreate a view using a DDL query.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Data definition language (DDL) statements in GoogleSQL](/bigquery/docs/reference/standard-sql/data-definition-language)\n\nCode sample\n-----------\n\n### Java\n\n\nBefore trying this sample, follow the Java setup instructions in the\n[BigQuery quickstart using\nclient libraries](/bigquery/docs/quickstarts/quickstart-client-libraries).\n\n\nFor more information, see the\n[BigQuery Java API\nreference documentation](/java/docs/reference/google-cloud-bigquery/latest/overview).\n\n\nTo authenticate to BigQuery, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/bigquery/docs/authentication#client-libs).\n\n import com.google.cloud.bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQuery.html;\n import com.google.cloud.bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQueryException.html;\n import com.google.cloud.bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQueryOptions.html;\n import com.google.cloud.bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.Job.html;\n import com.google.cloud.bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.JobInfo.html;\n import com.google.cloud.bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.QueryJobConfiguration.html;\n\n // Sample to create a view using DDL\n public class DdlCreateView {\n\n public static void main(String[] args) {\n // TODO(developer): Replace these variables before running the sample.\n String projectId = \"MY_PROJECT_ID\";\n String datasetId = \"MY_DATASET_ID\";\n String tableId = \"MY_VIEW_ID\";\n String ddl =\n \"CREATE VIEW \"\n + \"`\"\n + projectId\n + \".\"\n + datasetId\n + \".\"\n + tableId\n + \"`\"\n + \" OPTIONS(\"\n + \" expiration_timestamp=TIMESTAMP_ADD(\"\n + \" CURRENT_TIMESTAMP(), INTERVAL 48 HOUR),\"\n + \" friendly_name=\\\"new_view\\\",\"\n + \" description=\\\"a view that expires in 2 days\\\",\"\n + \" labels=[(\\\"org_unit\\\", \\\"development\\\")]\"\n + \" )\"\n + \" AS SELECT name, state, year, number\"\n + \" FROM `bigquery-public-data.usa_names.usa_1910_current`\"\n + \" WHERE state LIKE 'W%'`\";\n ddlCreateView(ddl);\n }\n\n public static void ddlCreateView(String ddl) {\n try {\n // Initialize client that will be used to send requests. This client only needs to be created\n // once, and can be reused for multiple requests.\n https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQuery.html bigquery = https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQueryOptions.html.getDefaultInstance().getService();\n\n https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.QueryJobConfiguration.html config = https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.QueryJobConfiguration.html.newBuilder(ddl).build();\n\n // create a view using query and it will wait to complete job.\n https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.Job.html job = bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQuery.html#com_google_cloud_bigquery_BigQuery_create_com_google_cloud_bigquery_DatasetInfo_com_google_cloud_bigquery_BigQuery_DatasetOption____(JobInfo.of(config));\n job = job.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.Job.html#com_google_cloud_bigquery_Job_waitFor_com_google_cloud_bigquery_BigQueryRetryConfig_com_google_cloud_RetryOption____();\n if (job.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.Job.html#com_google_cloud_bigquery_Job_isDone__()) {\n System.out.println(\"View created successfully\");\n } else {\n System.out.println(\"View was not created\");\n }\n } catch (https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQueryException.html | InterruptedException e) {\n System.out.println(\"View was not created. \\n\" + e.toString());\n }\n }\n }\n\n### Node.js\n\n\nBefore trying this sample, follow the Node.js setup instructions in the\n[BigQuery quickstart using\nclient libraries](/bigquery/docs/quickstarts/quickstart-client-libraries).\n\n\nFor more information, see the\n[BigQuery Node.js API\nreference documentation](https://googleapis.dev/nodejs/bigquery/latest/index.html).\n\n\nTo authenticate to BigQuery, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/bigquery/docs/authentication#client-libs).\n\n // Import the Google Cloud client library and create a client\n const {BigQuery} = require('https://cloud.google.com/nodejs/docs/reference/bigquery/latest/overview.html');\n const bigquery = new https://cloud.google.com/nodejs/docs/reference/bigquery/latest/bigquery/bigquery.html();\n\n async function ddlCreateView() {\n // Creates a view via a DDL query\n\n /**\n * TODO(developer): Uncomment the following lines before running the sample.\n */\n // const projectId = \"my_project\"\n // const datasetId = \"my_dataset\"\n // const tableId = \"my_new_view\"\n\n const query = `\n CREATE VIEW \\`${projectId}.${datasetId}.${tableId}\\`\n OPTIONS(\n expiration_timestamp=TIMESTAMP_ADD(\n CURRENT_TIMESTAMP(), INTERVAL 48 HOUR),\n friendly_name=\"new_view\",\n description=\"a view that expires in 2 days\",\n labels=[(\"org_unit\", \"development\")]\n )\n AS SELECT name, state, year, number\n FROM \\`bigquery-public-data.usa_names.usa_1910_current\\`\n WHERE state LIKE 'W%'`;\n\n // For all options, see https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/query\n const options = {\n query: query,\n };\n\n // Run the query as a job\n const [job] = await bigquery.createQueryJob(options);\n\n https://cloud.google.com/nodejs/docs/reference/bigquery/latest/bigquery/bigquery.html.on('complete', metadata =\u003e {\n console.log(`Created new view ${tableId} via job ${metadata.id}`);\n });\n }\n\n### Python\n\n\nBefore trying this sample, follow the Python setup instructions in the\n[BigQuery quickstart using\nclient libraries](/bigquery/docs/quickstarts/quickstart-client-libraries).\n\n\nFor more information, see the\n[BigQuery Python API\nreference documentation](/python/docs/reference/bigquery/latest).\n\n\nTo authenticate to BigQuery, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/bigquery/docs/authentication#client-libs).\n\n # from google.cloud import bigquery\n # project = 'my-project'\n # dataset_id = 'my_dataset'\n # table_id = 'new_view'\n # client = bigquery.Client(project=project)\n\n sql = \"\"\"\n CREATE VIEW `{}.{}.{}`\n OPTIONS(\n expiration_timestamp=TIMESTAMP_ADD(\n CURRENT_TIMESTAMP(), INTERVAL 48 HOUR),\n friendly_name=\"new_view\",\n description=\"a view that expires in 2 days\",\n labels=[(\"org_unit\", \"development\")]\n )\n AS SELECT name, state, year, number\n FROM `bigquery-public-data.usa_names.usa_1910_current`\n WHERE state LIKE 'W%'\n \"\"\".format(\n project, dataset_id, table_id\n )\n\n job = client.query(sql) # API request.\n job.result() # Waits for the query to finish.\n\n print(\n 'Created new view \"{}.{}.{}\".'.format(\n job.destination.project,\n job.destination.dataset_id,\n job.destination.table_id,\n )\n )\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=bigquery)."]]