- Notifications
You must be signed in to change notification settings - Fork 128
feat: add samples #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6da1138 15f0945 7032ee8 dcfc812 166ea16 116f215 File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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. Show resolved Hide 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] | ||
| 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. Show resolved Hide 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. Show resolved Hide resolved | ||
| try { | ||
| table.extract(format, gcsFileName); | ||
| System.out.println("Table extraction job completed successfully"); | ||
stephaniewang526 marked this conversation as resolved. Show resolved Hide resolved | ||
| } catch (BigQueryException e) { | ||
| System.out.println("Table extraction job was interrupted. \n" + e.toString()); | ||
| } | ||
| } | ||
| } | ||
| // [END bigquery_extract_table] | ||
| 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"); | ||
| } | ||
| } |
| 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; | ||
| } | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.