Create a model

Create a model within an existing dataset.

Code sample

Java

Before trying this sample, follow theJava setup instructions in theBigQuery quickstart using client libraries. For more information, see theBigQueryJava API reference documentation.

To authenticate to BigQuery, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.

importcom.google.cloud.bigquery.BigQuery;importcom.google.cloud.bigquery.BigQueryException;importcom.google.cloud.bigquery.BigQueryOptions;importcom.google.cloud.bigquery.Job;importcom.google.cloud.bigquery.JobInfo;importcom.google.cloud.bigquery.QueryJobConfiguration;// Sample to create a modelpublicclassCreateModel{publicstaticvoidmain(String[]args){// TODO(developer): Replace these variables before running the sample.StringdatasetName="MY_DATASET_NAME";StringmodelName="MY_MODEL_NAME";Stringsql="CREATE MODEL `"+datasetName+"."+modelName+"`"+"OPTIONS ( "+"model_type='linear_reg', "+"max_iterations=1, "+"learn_rate=0.4, "+"learn_rate_strategy='constant' "+") AS ( "+"SELECT 'a' AS f1, 2.0 AS label "+"UNION ALL "+"SELECT 'b' AS f1, 3.8 AS label "+")";createModel(sql);}publicstaticvoidcreateModel(Stringsql){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.BigQuerybigquery=BigQueryOptions.getDefaultInstance().getService();QueryJobConfigurationconfig=QueryJobConfiguration.newBuilder(sql).build();// create a model using query and it will wait to complete job.Jobjob=bigquery.create(JobInfo.of(config));job=job.waitFor();if(job.isDone()){System.out.println("Model created successfully");}else{System.out.println("Model was not created");}}catch(BigQueryException|InterruptedExceptione){System.out.println("Model was not created. \n"+e.toString());}}}

Node.js

Before trying this sample, follow theNode.js setup instructions in theBigQuery quickstart using client libraries. For more information, see theBigQueryNode.js API reference documentation.

To authenticate to BigQuery, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.

// Import the Google Cloud client libraryconst{BigQuery}=require('@google-cloud/bigquery');constbigquery=newBigQuery();asyncfunctioncreateModel(){// Creates a model named "my_model" in "my_dataset"./**   * TODO(developer): Uncomment the following lines before running the sample   */// const datasetId = "my_dataset";// const modelId = "my_model";constquery=`CREATE OR REPLACE MODEL \`${datasetId}.${modelId}\`       OPTIONS(model_type='logistic_reg') AS       SELECT         IF(totals.transactions IS NULL, 0, 1) AS label,         IFNULL(device.operatingSystem, "") AS os,         device.isMobile AS is_mobile,         IFNULL(geoNetwork.country, "") AS country,         IFNULL(totals.pageviews, 0) AS pageviews       FROM         \`bigquery-public-data.google_analytics_sample.ga_sessions_*\`       WHERE         _TABLE_SUFFIX BETWEEN '20160801' AND '20170631'       LIMIT  100000;`;constqueryOptions={query:query,};// Run query to create a modelconst[job]=awaitbigquery.createQueryJob(queryOptions);// Wait for the query to finishawaitjob.getQueryResults();console.log(`Model${modelId} created.`);}createModel();

What's next

To search and filter code samples for other Google Cloud products, see theGoogle Cloud sample browser.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.