diff --git a/samples/src/main/java/com/example/bigquery/GetModel.java b/samples/src/main/java/com/example/bigquery/GetModel.java new file mode 100644 index 000000000..e51b3fd83 --- /dev/null +++ b/samples/src/main/java/com/example/bigquery/GetModel.java @@ -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; + +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.BigQueryOptions; +import com.google.cloud.bigquery.Model; +import com.google.cloud.bigquery.ModelId; + +// [START bigquery_get_model] +public class GetModel { + + public static void runGetModel() { + // TODO(developer): Replace these variables before running the sample. + String datasetName = "MY_DATASET_NAME"; + String modelName = "MY_MODEL_ID"; + getModel(datasetName, modelName); + } + + public static void getModel(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(); + + ModelId modelId = ModelId.of(datasetName, modelName); + Model model = bigquery.getModel(modelId); + System.out.println("Model: " + model.getDescription()); + + System.out.println("Successfully retrieved model"); + } catch (BigQueryException e) { + System.out.println("Cannot retrieve model \n" + e.toString()); + } + } +} +// [END bigquery_get_model] diff --git a/samples/src/test/java/com/example/bigquery/GetModelIT.java b/samples/src/test/java/com/example/bigquery/GetModelIT.java new file mode 100644 index 000000000..78d53ad7c --- /dev/null +++ b/samples/src/test/java/com/example/bigquery/GetModelIT.java @@ -0,0 +1,65 @@ +/* + * 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 org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class GetModelIT { + private ByteArrayOutputStream bout; + private PrintStream out; + + private static final String BIGQUERY_DATASET_NAME = System.getenv("BIGQUERY_DATASET_NAME"); + private static final String BIGQUERY_MODEL_NAME = System.getenv("BIGQUERY_MODEL_NAME"); + + private static void requireEnvVar(String varName) { + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("BIGQUERY_DATASET_NAME"); + requireEnvVar("BIGQUERY_MODEL_NAME"); + } + + @Before + public void setUp() throws Exception { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void getModel() { + GetModel.getModel(BIGQUERY_DATASET_NAME, BIGQUERY_MODEL_NAME); + assertThat(bout.toString()).contains("Successfully retrieved model"); + } +}