Modell erstellen
Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Modell in einem vorhandenen Dataset erstellen
Codebeispiel
Nächste Schritte
Wenn Sie nach Codebeispielen für andere Google Cloud -Produkte suchen und filtern möchten, können Sie den Google Cloud -Beispielbrowser verwenden.
Sofern nicht anders angegeben, sind die Inhalte dieser Seite unter der Creative Commons Attribution 4.0 License und Codebeispiele unter der Apache 2.0 License lizenziert. Weitere Informationen finden Sie in den Websiterichtlinien von Google Developers. Java ist eine eingetragene Marke von Oracle und/oder seinen Partnern.
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis document provides code samples in Java and Node.js for creating a model within a BigQuery dataset.\u003c/p\u003e\n"],["\u003cp\u003eThe Java sample code demonstrates how to define a linear regression model with specified options and data selection using SQL syntax, and then executes it using the BigQuery client.\u003c/p\u003e\n"],["\u003cp\u003eThe Node.js sample illustrates how to define a logistic regression model using SQL, including feature selection and a time-based filter, and then use the BigQuery client to execute this query to create the model.\u003c/p\u003e\n"],["\u003cp\u003eBoth code samples require setting up authentication using Application Default Credentials, as well as ensuring the BigQuery client library is properly installed.\u003c/p\u003e\n"]]],[],null,["# Create a model within an existing dataset.\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 model\n public class CreateModel {\n\n public static void main(String[] args) {\n // TODO(developer): Replace these variables before running the sample.\n String datasetName = \"MY_DATASET_NAME\";\n String modelName = \"MY_MODEL_NAME\";\n String sql =\n \"CREATE MODEL `\"\n + datasetName\n + \".\"\n + modelName\n + \"`\"\n + \"OPTIONS ( \"\n + \"model_type='linear_reg', \"\n + \"max_iterations=1, \"\n + \"learn_rate=0.4, \"\n + \"learn_rate_strategy='constant' \"\n + \") AS ( \"\n + \"SELECT 'a' AS f1, 2.0 AS label \"\n + \"UNION ALL \"\n + \"SELECT 'b' AS f1, 3.8 AS label \"\n + \")\";\n createModel(sql);\n }\n\n public static void createModel(String sql) {\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(sql).build();\n\n // create a model 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(\"Model created successfully\");\n } else {\n System.out.println(\"Model 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(\"Model 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\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 createModel() {\n // Creates a model named \"my_model\" in \"my_dataset\".\n\n /**\n * TODO(developer): Uncomment the following lines before running the sample\n */\n // const datasetId = \"my_dataset\";\n // const modelId = \"my_model\";\n\n const query = `CREATE OR REPLACE MODEL \\`${datasetId}.${modelId}\\`\n OPTIONS(model_type='logistic_reg') AS\n SELECT\n IF(totals.transactions IS NULL, 0, 1) AS label,\n IFNULL(device.operatingSystem, \"\") AS os,\n device.isMobile AS is_mobile,\n IFNULL(geoNetwork.country, \"\") AS country,\n IFNULL(totals.pageviews, 0) AS pageviews\n FROM\n \\`bigquery-public-data.google_analytics_sample.ga_sessions_*\\`\n WHERE\n _TABLE_SUFFIX BETWEEN '20160801' AND '20170631'\n LIMIT 100000;`;\n\n const queryOptions = {\n query: query,\n };\n\n // Run query to create a model\n const [job] = await bigquery.createQueryJob(queryOptions);\n\n // Wait for the query to finish\n await https://cloud.google.com/nodejs/docs/reference/bigquery/latest/bigquery/bigquery.html.https://cloud.google.com/nodejs/docs/reference/bigquery/latest/bigquery/job.html();\n\n console.log(`Model ${modelId} created.`);\n }\n createModel();\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)."]]