From d3e5e12f2dfa6524ed2311df1449a5ea7a1fc2e1 Mon Sep 17 00:00:00 2001 From: Stephanie Wang Date: Wed, 18 Dec 2019 11:47:14 -0500 Subject: [PATCH] feat: add samples (#44) * feat: add extract table to json sample * feat: add create table sample * feat: add extract table to json sample * chore: clean up formatting * chore: update formatting according to comment * chore: update formatting according to comment --- .../com/example/bigquery/CreateTable.java | 46 +++++++++++ .../example/bigquery/ExtractTableToJSON.java | 29 +++++++ .../com/example/bigquery/CreateTableIT.java | 48 ++++++++++++ .../bigquery/ExtractTableToJSONIT.java | 76 +++++++++++++++++++ 4 files changed, 199 insertions(+) create mode 100644 samples/src/main/java/com/example/bigquery/CreateTable.java create mode 100644 samples/src/main/java/com/example/bigquery/ExtractTableToJSON.java create mode 100644 samples/src/test/java/com/example/bigquery/CreateTableIT.java create mode 100644 samples/src/test/java/com/example/bigquery/ExtractTableToJSONIT.java diff --git a/samples/src/main/java/com/example/bigquery/CreateTable.java b/samples/src/main/java/com/example/bigquery/CreateTable.java new file mode 100644 index 000000000..0f56495b4 --- /dev/null +++ b/samples/src/main/java/com/example/bigquery/CreateTable.java @@ -0,0 +1,46 @@ +package com.example.bigquery; + +// [START bigquery_create_table] +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.BigQueryOptions; +import com.google.cloud.bigquery.Field; +import com.google.cloud.bigquery.LegacySQLTypeName; +import com.google.cloud.bigquery.Schema; +import com.google.cloud.bigquery.StandardTableDefinition; +import com.google.cloud.bigquery.TableDefinition; +import com.google.cloud.bigquery.TableId; +import com.google.cloud.bigquery.TableInfo; + +public class CreateTable { + + public static void runCreateTable() { + // TODO(developer): Replace these variables before running the sample. + String datasetName = "my-dataset-name"; + String tableName = "my_table_name"; + Schema schema = + Schema.of( + // LegacySQLTypeName will be updated to StandardSQLTypeName once release rolls out + Field.of("stringField", LegacySQLTypeName.STRING), + Field.of("booleanField", LegacySQLTypeName.BOOLEAN)); + createTable(datasetName, tableName, schema); + } + + public static void createTable(String datasetName, String tableName, Schema schema) { + // 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(); + + TableId tableId = TableId.of(datasetName, tableName); + TableDefinition tableDefinition = StandardTableDefinition.of(schema); + TableInfo tableInfo = TableInfo.newBuilder(tableId, tableDefinition).build(); + + try { + bigquery.create(tableInfo); + System.out.println("Table created successfully"); + } catch (BigQueryException e) { + System.out.println("Table was not created. \n" + e.toString()); + } + } +} +// [END bigquery_create_table] diff --git a/samples/src/main/java/com/example/bigquery/ExtractTableToJSON.java b/samples/src/main/java/com/example/bigquery/ExtractTableToJSON.java new file mode 100644 index 000000000..a9b4a9abe --- /dev/null +++ b/samples/src/main/java/com/example/bigquery/ExtractTableToJSON.java @@ -0,0 +1,29 @@ +package com.example.bigquery; + +// [START bigquery_extract_table] +import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.Table; + +public class ExtractTableToJSON { + + public static void runExtractTableToJSON() { + // TODO(developer): Replace these variables before running the sample. + Table table = null; + String format = "CSV"; + String bucketName = "my-bucket"; + String gcsFileName = "gs://" + bucketName + "/extractTest.csv"; + extractTableToJSON(table, format, gcsFileName); + } + + // Exports my-dataset-name:my_table to gcs://my-bucket/my-file as raw CSV + public static void extractTableToJSON(Table table, String format, String gcsFileName) { + + try { + table.extract(format, gcsFileName); + System.out.println("Table extraction job completed successfully"); + } catch (BigQueryException e) { + System.out.println("Table extraction job was interrupted. \n" + e.toString()); + } + } +} +// [END bigquery_extract_table] diff --git a/samples/src/test/java/com/example/bigquery/CreateTableIT.java b/samples/src/test/java/com/example/bigquery/CreateTableIT.java new file mode 100644 index 000000000..09c490e2d --- /dev/null +++ b/samples/src/test/java/com/example/bigquery/CreateTableIT.java @@ -0,0 +1,48 @@ +package com.example.bigquery; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.cloud.bigquery.Field; +import com.google.cloud.bigquery.LegacySQLTypeName; +import com.google.cloud.bigquery.Schema; +import com.google.cloud.bigquery.testing.RemoteBigQueryHelper; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class CreateTableIT { + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testCreateTable() { + String generatedDatasetName = RemoteBigQueryHelper.generateDatasetName(); + + // Create a new dataset to create a table in + CreateDataset.createDataset(generatedDatasetName); + + // Create an empty table with specific schema in the dataset just created + String tableName = "my_table_name"; + Schema schema = + Schema.of( + Field.of("stringField", LegacySQLTypeName.STRING), + Field.of("booleanField", LegacySQLTypeName.BOOLEAN)); + CreateTable.createTable(generatedDatasetName, tableName, schema); + + assertThat(bout.toString()).contains("Table created successfully"); + } +} diff --git a/samples/src/test/java/com/example/bigquery/ExtractTableToJSONIT.java b/samples/src/test/java/com/example/bigquery/ExtractTableToJSONIT.java new file mode 100644 index 000000000..d515d5098 --- /dev/null +++ b/samples/src/test/java/com/example/bigquery/ExtractTableToJSONIT.java @@ -0,0 +1,76 @@ +package com.example.bigquery; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.BigQueryOptions; +import com.google.cloud.bigquery.Field; +import com.google.cloud.bigquery.LegacySQLTypeName; +import com.google.cloud.bigquery.Schema; +import com.google.cloud.bigquery.StandardTableDefinition; +import com.google.cloud.bigquery.Table; +import com.google.cloud.bigquery.TableDefinition; +import com.google.cloud.bigquery.TableId; +import com.google.cloud.bigquery.TableInfo; +import com.google.cloud.bigquery.testing.RemoteBigQueryHelper; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class ExtractTableToJSONIT { + private ByteArrayOutputStream bout; + private PrintStream out; + + @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 testExtractTableToJSON() { + String generatedDatasetName = RemoteBigQueryHelper.generateDatasetName(); + + // Create a new dataset to create a new table in + CreateDataset.createDataset(generatedDatasetName); + + // Create a new table to extract to GCS for + String tableName = "my_table_name"; + Schema schema = + Schema.of( + Field.of("stringField", LegacySQLTypeName.STRING), + Field.of("booleanField", LegacySQLTypeName.BOOLEAN)); + Table table = createTableHelper(generatedDatasetName, tableName, schema); + + // Extract table content to GCS in CSV format + ExtractTableToJSON.extractTableToJSON(table, "CSV", "gs://my-bucket/extractTest.csv"); + assertThat(bout.toString()).contains("Table extraction job completed successfully"); + } + + private static Table createTableHelper(String datasetName, String tableName, Schema schema) { + // 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(); + + TableId tableId = TableId.of(datasetName, tableName); + TableDefinition tableDefinition = StandardTableDefinition.of(schema); + TableInfo tableInfo = TableInfo.newBuilder(tableId, tableDefinition).build(); + + try { + Table table = bigquery.create(tableInfo); + return table; + } catch (BigQueryException e) { + System.out.println("Table was not created. \n" + e.toString()); + return null; + } + } +}