This repository was archived by the owner on Mar 23, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 134
feat: add samples #44
Merged
stephaniewang526
merged 6 commits into
googleapis:master
from
stephaniewang526:feat-samples
Dec 18, 2019
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6da1138
feat: add extract table to json sample
stephaniewang526 15f0945
feat: add create table sample
stephaniewang526 7032ee8
feat: add extract table to json sample
stephaniewang526 dcfc812
chore: clean up formatting
stephaniewang526 166ea16
chore: update formatting according to comment
stephaniewang526 116f215
chore: update formatting according to comment
stephaniewang526 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
samples/src/main/java/com/example/bigquery/CreateTable.java
This file contains hidden or 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,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 | ||
|
stephaniewang526 marked this conversation as resolved.
|
||
| 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] | ||
29 changes: 29 additions & 0 deletions
29
samples/src/main/java/com/example/bigquery/ExtractTableToJSON.java
This file contains hidden or 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,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; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this null? We should be showing the user how to initialize this value.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My initial thought is user should be able to pass in a Table object of their choice to extract into GCS as CSV - but I think what you said makes sense too. We can offer an example of what this Table object could be. |
||
| String format = "CSV"; | ||
| String bucketName = "my-bucket"; | ||
| String gcsFileName = "gs://" + bucketName + "/extractTest.csv"; | ||
|
stephaniewang526 marked this conversation as resolved.
|
||
| 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) { | ||
|
|
||
|
stephaniewang526 marked this conversation as resolved.
|
||
| try { | ||
| table.extract(format, gcsFileName); | ||
| System.out.println("Table extraction job completed successfully"); | ||
|
stephaniewang526 marked this conversation as resolved.
|
||
| } catch (BigQueryException e) { | ||
| System.out.println("Table extraction job was interrupted. \n" + e.toString()); | ||
| } | ||
| } | ||
| } | ||
| // [END bigquery_extract_table] | ||
48 changes: 48 additions & 0 deletions
48
samples/src/test/java/com/example/bigquery/CreateTableIT.java
This file contains hidden or 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,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"); | ||
| } | ||
| } |
76 changes: 76 additions & 0 deletions
76
samples/src/test/java/com/example/bigquery/ExtractTableToJSONIT.java
This file contains hidden or 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,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; | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.