Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(samples): add create and delete model (#462)
* docs(samples): add create and delete model * docs(samples): fix flaky test * docs(samples): fix npe
- Loading branch information
Praful Makani
committed
Jun 18, 2020
1 parent
e5db01c
commit df6479f
Showing
4 changed files
with
305 additions
and
0 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
samples/snippets/src/main/java/com/example/bigquery/CreateModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.bigquery; | ||
|
||
// [START bigquery_create_model] | ||
import com.google.cloud.bigquery.BigQuery; | ||
import com.google.cloud.bigquery.BigQueryException; | ||
import com.google.cloud.bigquery.BigQueryOptions; | ||
import com.google.cloud.bigquery.Job; | ||
import com.google.cloud.bigquery.JobInfo; | ||
import com.google.cloud.bigquery.QueryJobConfiguration; | ||
|
||
// Sample to create a model | ||
public class CreateModel { | ||
|
||
public static void runCreateModel() { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String datasetName = "MY_DATASET_NAME"; | ||
String modelName = "MY_MODEL_NAME"; | ||
String sql = | ||
"CREATE MODEL `" | ||
+ datasetName | ||
+ "." | ||
+ modelName | ||
+ "`" | ||
+ "OPTIONS ( " | ||
+ "model_type='linear_reg', " | ||
+ "max_iteration=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); | ||
} | ||
|
||
public static void createModel(String sql) { | ||
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. | ||
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); | ||
|
||
QueryJobConfiguration config = QueryJobConfiguration.newBuilder(sql).build(); | ||
|
||
// create a model using query and it will wait to complete job. | ||
Job job = 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 | InterruptedException e) { | ||
System.out.println("Model was not created. \n" + e.toString()); | ||
} | ||
} | ||
} | ||
// [END bigquery_create_model] |
51 changes: 51 additions & 0 deletions
51
samples/snippets/src/main/java/com/example/bigquery/DeleteModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.bigquery; | ||
|
||
// [START bigquery_delete_model] | ||
import com.google.cloud.bigquery.BigQuery; | ||
import com.google.cloud.bigquery.BigQueryException; | ||
import com.google.cloud.bigquery.BigQueryOptions; | ||
import com.google.cloud.bigquery.ModelId; | ||
|
||
// Sample to delete a model | ||
public class DeleteModel { | ||
|
||
public static void runDeleteModel() { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String datasetName = "MY_DATASET_NAME"; | ||
String modelName = "MY_MODEL_NAME"; | ||
deleteModel(datasetName, modelName); | ||
} | ||
|
||
public static void deleteModel(String datasetName, String modelName) { | ||
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. | ||
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); | ||
boolean success = bigquery.delete(ModelId.of(datasetName, modelName)); | ||
if (success) { | ||
System.out.println("Model deleted successfully"); | ||
} else { | ||
System.out.println("Model was not found"); | ||
} | ||
} catch (BigQueryException e) { | ||
System.out.println("Model was not deleted. \n" + e.toString()); | ||
} | ||
} | ||
} | ||
// [END bigquery_delete_model] |
87 changes: 87 additions & 0 deletions
87
samples/snippets/src/test/java/com/example/bigquery/CreateModelIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.bigquery; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import static junit.framework.TestCase.assertNotNull; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.PrintStream; | ||
import java.util.UUID; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
public class CreateModelIT { | ||
|
||
private String modelName; | ||
private ByteArrayOutputStream bout; | ||
private PrintStream out; | ||
|
||
private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME"); | ||
|
||
private static String requireEnvVar(String varName) { | ||
String value = System.getenv(varName); | ||
assertNotNull( | ||
"Environment variable " + varName + " is required to perform these tests.", | ||
System.getenv(varName)); | ||
return value; | ||
} | ||
|
||
@BeforeClass | ||
public static void checkRequirements() { | ||
requireEnvVar("BIGQUERY_DATASET_NAME"); | ||
} | ||
|
||
@Before | ||
public void setUp() { | ||
modelName = "MY_MODEL_NAME_TEST_" + UUID.randomUUID().toString().replace('-', '_'); | ||
bout = new ByteArrayOutputStream(); | ||
out = new PrintStream(bout); | ||
System.setOut(out); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
// Clean up | ||
DeleteModel.deleteModel(BIGQUERY_DATASET_NAME, modelName); | ||
System.setOut(null); | ||
} | ||
|
||
@Test | ||
public void testCreateModel() { | ||
String sql = | ||
"CREATE MODEL `" | ||
+ BIGQUERY_DATASET_NAME | ||
+ "." | ||
+ modelName | ||
+ "`" | ||
+ "OPTIONS ( " | ||
+ "model_type='linear_reg', " | ||
+ "max_iteration=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.createModel(sql); | ||
assertThat(bout.toString()).contains("Model created successfully"); | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
samples/snippets/src/test/java/com/example/bigquery/DeleteModelIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.bigquery; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import static junit.framework.TestCase.assertNotNull; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.PrintStream; | ||
import java.util.UUID; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
public class DeleteModelIT { | ||
|
||
private String modelName; | ||
private ByteArrayOutputStream bout; | ||
private PrintStream out; | ||
|
||
private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME"); | ||
|
||
private static String requireEnvVar(String varName) { | ||
String value = System.getenv(varName); | ||
assertNotNull( | ||
"Environment variable " + varName + " is required to perform these tests.", | ||
System.getenv(varName)); | ||
return value; | ||
} | ||
|
||
@BeforeClass | ||
public static void checkRequirements() { | ||
requireEnvVar("BIGQUERY_DATASET_NAME"); | ||
} | ||
|
||
@Before | ||
public void setUp() { | ||
bout = new ByteArrayOutputStream(); | ||
out = new PrintStream(bout); | ||
System.setOut(out); | ||
|
||
// Create a new model to be deleted | ||
modelName = "MY_MODEL_NAME_TEST_" + UUID.randomUUID().toString().substring(0, 8); | ||
String sql = | ||
"CREATE MODEL `" | ||
+ BIGQUERY_DATASET_NAME | ||
+ "." | ||
+ modelName | ||
+ "`" | ||
+ "OPTIONS ( " | ||
+ "model_type='linear_reg', " | ||
+ "max_iteration=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.createModel(sql); | ||
|
||
bout = new ByteArrayOutputStream(); | ||
out = new PrintStream(bout); | ||
System.setOut(out); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
System.setOut(null); | ||
} | ||
|
||
@Test | ||
public void testDeleteModel() { | ||
// Delete the model that was just created | ||
DeleteModel.deleteModel(BIGQUERY_DATASET_NAME, modelName); | ||
assertThat(bout.toString()).contains("Model deleted successfully"); | ||
} | ||
} |