Skip to content

Commit

Permalink
feat: add samples (#44)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
stephaniewang526 committed Dec 18, 2019
1 parent 662164b commit d3e5e12
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 0 deletions.
46 changes: 46 additions & 0 deletions 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]
29 changes: 29 additions & 0 deletions 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]
48 changes: 48 additions & 0 deletions 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");
}
}
@@ -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;
}
}
}

0 comments on commit d3e5e12

Please sign in to comment.