Skip to content

Commit

Permalink
feat: new sample - Get Model (#124)
Browse files Browse the repository at this point in the history
* feat: add new sample - Tables: Relax column query append

* nit

* update based on comments

* update exception handling

* refactor

* update base on comments

* code refactoring

* fix build errors

* add debugging statements

* remove unused exception

* feat: new sample - Models: Get a model resource

* feat: new sample - Models: Get a model resource

* fix build error
  • Loading branch information
stephaniewang526 committed Jan 27, 2020
1 parent 1a1480f commit a03670b
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
51 changes: 51 additions & 0 deletions 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]
65 changes: 65 additions & 0 deletions 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");
}
}

0 comments on commit a03670b

Please sign in to comment.