Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a8cb797

Browse files
author
Praful Makani
authoredJul 29, 2020
docs(samples): add update model description (#617)
1 parent e28abed commit a8cb797

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigquery;
18+
19+
// [START bigquery_update_model_description]
20+
import com.google.cloud.bigquery.BigQuery;
21+
import com.google.cloud.bigquery.BigQueryException;
22+
import com.google.cloud.bigquery.BigQueryOptions;
23+
import com.google.cloud.bigquery.Model;
24+
import com.google.cloud.bigquery.ModelId;
25+
26+
// Sample to update description on a model
27+
public class UpdateModelDescription {
28+
29+
public static void runUpdateModelDescription() {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String datasetName = "MY_DATASET_NAME";
32+
String modelName = "MY_MODEL_NAME";
33+
String newDescription = "A really great model.";
34+
updateModelDescription(datasetName, modelName, newDescription);
35+
}
36+
37+
public static void updateModelDescription(
38+
String datasetName, String modelName, String newDescription) {
39+
try {
40+
// Initialize client that will be used to send requests. This client only needs to be created
41+
// once, and can be reused for multiple requests.
42+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
43+
44+
Model model = bigquery.getModel(ModelId.of(datasetName, modelName));
45+
bigquery.update(model.toBuilder().setDescription(newDescription).build());
46+
System.out.println("Model description updated successfully to " + newDescription);
47+
} catch (BigQueryException e) {
48+
System.out.println("Model description was not updated \n" + e.toString());
49+
}
50+
}
51+
}
52+
// [END bigquery_update_model_description]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigquery;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static junit.framework.TestCase.assertNotNull;
21+
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.PrintStream;
24+
import java.util.UUID;
25+
import org.junit.After;
26+
import org.junit.Before;
27+
import org.junit.BeforeClass;
28+
import org.junit.Test;
29+
30+
public class UpdateModelDescriptionIT {
31+
32+
private String modelName;
33+
private ByteArrayOutputStream bout;
34+
private PrintStream out;
35+
36+
private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME");
37+
38+
private static String requireEnvVar(String varName) {
39+
String value = System.getenv(varName);
40+
assertNotNull(
41+
"Environment variable " + varName + " is required to perform these tests.",
42+
System.getenv(varName));
43+
return value;
44+
}
45+
46+
@BeforeClass
47+
public static void checkRequirements() {
48+
requireEnvVar("BIGQUERY_DATASET_NAME");
49+
}
50+
51+
@Before
52+
public void setUp() {
53+
bout = new ByteArrayOutputStream();
54+
out = new PrintStream(bout);
55+
System.setOut(out);
56+
// create a test model
57+
modelName = "MY_MODEL_NAME_TEST_" + UUID.randomUUID().toString().substring(0, 8);
58+
String sql =
59+
"CREATE MODEL `"
60+
+ BIGQUERY_DATASET_NAME
61+
+ "."
62+
+ modelName
63+
+ "`"
64+
+ "OPTIONS ( "
65+
+ "model_type='linear_reg', "
66+
+ "max_iteration=1, "
67+
+ "learn_rate=0.4, "
68+
+ "learn_rate_strategy='constant' "
69+
+ ") AS ( "
70+
+ " SELECT 'a' AS f1, 2.0 AS label "
71+
+ "UNION ALL "
72+
+ "SELECT 'b' AS f1, 3.8 AS label "
73+
+ ")";
74+
CreateModel.createModel(sql);
75+
76+
bout = new ByteArrayOutputStream();
77+
out = new PrintStream(bout);
78+
System.setOut(out);
79+
}
80+
81+
@After
82+
public void tearDown() {
83+
// Clean up
84+
DeleteModel.deleteModel(BIGQUERY_DATASET_NAME, modelName);
85+
System.setOut(null);
86+
}
87+
88+
@Test
89+
public void testUpdateModelDescription() {
90+
String newDescription = "A really great model.";
91+
UpdateModelDescription.updateModelDescription(BIGQUERY_DATASET_NAME, modelName, newDescription);
92+
assertThat(bout.toString()).contains("Model description updated successfully");
93+
}
94+
}

0 commit comments

Comments
 (0)
Failed to load comments.