Skip to content

Commit df6479f

Browse files
author
Praful Makani
authored
docs(samples): add create and delete model (#462)
* docs(samples): add create and delete model * docs(samples): fix flaky test * docs(samples): fix npe
1 parent e5db01c commit df6479f

File tree

4 files changed

+305
-0
lines changed

4 files changed

+305
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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_create_model]
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.Job;
24+
import com.google.cloud.bigquery.JobInfo;
25+
import com.google.cloud.bigquery.QueryJobConfiguration;
26+
27+
// Sample to create a model
28+
public class CreateModel {
29+
30+
public static void runCreateModel() {
31+
// TODO(developer): Replace these variables before running the sample.
32+
String datasetName = "MY_DATASET_NAME";
33+
String modelName = "MY_MODEL_NAME";
34+
String sql =
35+
"CREATE MODEL `"
36+
+ datasetName
37+
+ "."
38+
+ modelName
39+
+ "`"
40+
+ "OPTIONS ( "
41+
+ "model_type='linear_reg', "
42+
+ "max_iteration=1, "
43+
+ "learn_rate=0.4, "
44+
+ "learn_rate_strategy='constant' "
45+
+ ") AS ( "
46+
+ " SELECT 'a' AS f1, 2.0 AS label "
47+
+ "UNION ALL "
48+
+ "SELECT 'b' AS f1, 3.8 AS label "
49+
+ ")";
50+
createModel(sql);
51+
}
52+
53+
public static void createModel(String sql) {
54+
try {
55+
// Initialize client that will be used to send requests. This client only needs to be created
56+
// once, and can be reused for multiple requests.
57+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
58+
59+
QueryJobConfiguration config = QueryJobConfiguration.newBuilder(sql).build();
60+
61+
// create a model using query and it will wait to complete job.
62+
Job job = bigquery.create(JobInfo.of(config));
63+
job = job.waitFor();
64+
if (job.isDone()) {
65+
System.out.println("Model created successfully");
66+
} else {
67+
System.out.println("Model was not created");
68+
}
69+
} catch (BigQueryException | InterruptedException e) {
70+
System.out.println("Model was not created. \n" + e.toString());
71+
}
72+
}
73+
}
74+
// [END bigquery_create_model]
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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_delete_model]
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.ModelId;
24+
25+
// Sample to delete a model
26+
public class DeleteModel {
27+
28+
public static void runDeleteModel() {
29+
// TODO(developer): Replace these variables before running the sample.
30+
String datasetName = "MY_DATASET_NAME";
31+
String modelName = "MY_MODEL_NAME";
32+
deleteModel(datasetName, modelName);
33+
}
34+
35+
public static void deleteModel(String datasetName, String modelName) {
36+
try {
37+
// Initialize client that will be used to send requests. This client only needs to be created
38+
// once, and can be reused for multiple requests.
39+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
40+
boolean success = bigquery.delete(ModelId.of(datasetName, modelName));
41+
if (success) {
42+
System.out.println("Model deleted successfully");
43+
} else {
44+
System.out.println("Model was not found");
45+
}
46+
} catch (BigQueryException e) {
47+
System.out.println("Model was not deleted. \n" + e.toString());
48+
}
49+
}
50+
}
51+
// [END bigquery_delete_model]
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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 CreateModelIT {
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+
modelName = "MY_MODEL_NAME_TEST_" + UUID.randomUUID().toString().replace('-', '_');
54+
bout = new ByteArrayOutputStream();
55+
out = new PrintStream(bout);
56+
System.setOut(out);
57+
}
58+
59+
@After
60+
public void tearDown() {
61+
// Clean up
62+
DeleteModel.deleteModel(BIGQUERY_DATASET_NAME, modelName);
63+
System.setOut(null);
64+
}
65+
66+
@Test
67+
public void testCreateModel() {
68+
String sql =
69+
"CREATE MODEL `"
70+
+ BIGQUERY_DATASET_NAME
71+
+ "."
72+
+ modelName
73+
+ "`"
74+
+ "OPTIONS ( "
75+
+ "model_type='linear_reg', "
76+
+ "max_iteration=1, "
77+
+ "learn_rate=0.4, "
78+
+ "learn_rate_strategy='constant' "
79+
+ ") AS ( "
80+
+ " SELECT 'a' AS f1, 2.0 AS label "
81+
+ "UNION ALL "
82+
+ "SELECT 'b' AS f1, 3.8 AS label "
83+
+ ")";
84+
CreateModel.createModel(sql);
85+
assertThat(bout.toString()).contains("Model created successfully");
86+
}
87+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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 DeleteModelIT {
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+
57+
// Create a new model to be deleted
58+
modelName = "MY_MODEL_NAME_TEST_" + UUID.randomUUID().toString().substring(0, 8);
59+
String sql =
60+
"CREATE MODEL `"
61+
+ BIGQUERY_DATASET_NAME
62+
+ "."
63+
+ modelName
64+
+ "`"
65+
+ "OPTIONS ( "
66+
+ "model_type='linear_reg', "
67+
+ "max_iteration=1, "
68+
+ "learn_rate=0.4, "
69+
+ "learn_rate_strategy='constant' "
70+
+ ") AS ( "
71+
+ " SELECT 'a' AS f1, 2.0 AS label "
72+
+ "UNION ALL "
73+
+ "SELECT 'b' AS f1, 3.8 AS label "
74+
+ ")";
75+
CreateModel.createModel(sql);
76+
77+
bout = new ByteArrayOutputStream();
78+
out = new PrintStream(bout);
79+
System.setOut(out);
80+
}
81+
82+
@After
83+
public void tearDown() {
84+
System.setOut(null);
85+
}
86+
87+
@Test
88+
public void testDeleteModel() {
89+
// Delete the model that was just created
90+
DeleteModel.deleteModel(BIGQUERY_DATASET_NAME, modelName);
91+
assertThat(bout.toString()).contains("Model deleted successfully");
92+
}
93+
}

0 commit comments

Comments
 (0)