Skip to content

Commit 5907c7c

Browse files
authored
BigQuery: add avro load samples (googleapis#6832)
1 parent 24b6ec6 commit 5907c7c

1 file changed

Lines changed: 44 additions & 3 deletions

File tree

bigquery/docs/snippets.py

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,6 +1313,39 @@ def test_load_table_from_file(client, to_delete):
13131313
assert row2 in rows
13141314

13151315

1316+
def test_load_table_from_uri_avro(client, to_delete, capsys):
1317+
dataset_id = 'load_table_from_uri_avro_{}'.format(_millis())
1318+
dataset = bigquery.Dataset(client.dataset(dataset_id))
1319+
client.create_dataset(dataset)
1320+
to_delete.append(dataset)
1321+
1322+
# [START bigquery_load_table_gcs_avro]
1323+
# from google.cloud import bigquery
1324+
# client = bigquery.Client()
1325+
# dataset_id = 'my_dataset'
1326+
1327+
dataset_ref = client.dataset(dataset_id)
1328+
job_config = bigquery.LoadJobConfig()
1329+
job_config.source_format = bigquery.SourceFormat.AVRO
1330+
uri = 'gs://cloud-samples-data/bigquery/us-states/us-states.avro'
1331+
1332+
load_job = client.load_table_from_uri(
1333+
uri,
1334+
dataset_ref.table('us_states'),
1335+
job_config=job_config) # API request
1336+
print('Starting job {}'.format(load_job.job_id))
1337+
1338+
load_job.result() # Waits for table load to complete.
1339+
print('Job finished.')
1340+
1341+
destination_table = client.get_table(dataset_ref.table('us_states'))
1342+
print('Loaded {} rows.'.format(destination_table.num_rows))
1343+
# [END bigquery_load_table_gcs_avro]
1344+
1345+
out, _ = capsys.readouterr()
1346+
assert 'Loaded 50 rows.' in out
1347+
1348+
13161349
def test_load_table_from_uri_csv(client, to_delete, capsys):
13171350
dataset_id = "load_table_from_uri_csv_{}".format(_millis())
13181351
dataset = bigquery.Dataset(client.dataset(dataset_id))
@@ -1588,8 +1621,11 @@ def test_load_table_from_uri_truncate(client, to_delete, capsys):
15881621
table_ref = dataset.table("us_states")
15891622
body = six.BytesIO(b"Washington,WA")
15901623
client.load_table_from_file(body, table_ref, job_config=job_config).result()
1624+
previous_rows = client.get_table(table_ref).num_rows
1625+
assert previous_rows > 0
15911626

15921627
# Shared code
1628+
# [START bigquery_load_table_gcs_avro_truncate]
15931629
# [START bigquery_load_table_gcs_csv_truncate]
15941630
# [START bigquery_load_table_gcs_json_truncate]
15951631
# [START bigquery_load_table_gcs_parquet_truncate]
@@ -1598,17 +1634,20 @@ def test_load_table_from_uri_truncate(client, to_delete, capsys):
15981634
# client = bigquery.Client()
15991635
# table_ref = client.dataset('my_dataset').table('existing_table')
16001636

1601-
previous_rows = client.get_table(table_ref).num_rows
1602-
assert previous_rows > 0
1603-
16041637
job_config = bigquery.LoadJobConfig()
16051638
job_config.write_disposition = bigquery.WriteDisposition.WRITE_TRUNCATE
1639+
# [END bigquery_load_table_gcs_avro_truncate]
16061640
# [END bigquery_load_table_gcs_csv_truncate]
16071641
# [END bigquery_load_table_gcs_json_truncate]
16081642
# [END bigquery_load_table_gcs_parquet_truncate]
16091643
# [END bigquery_load_table_gcs_orc_truncate]
16101644

16111645
# Format-specific code
1646+
# [START bigquery_load_table_gcs_avro_truncate]
1647+
job_config.source_format = bigquery.SourceFormat.AVRO
1648+
uri = "gs://cloud-samples-data/bigquery/us-states/us-states.avro"
1649+
# [END bigquery_load_table_gcs_avro_truncate]
1650+
16121651
# [START bigquery_load_table_gcs_csv_truncate]
16131652
job_config.skip_leading_rows = 1
16141653
# The source format defaults to CSV, so the line below is optional.
@@ -1634,6 +1673,7 @@ def test_load_table_from_uri_truncate(client, to_delete, capsys):
16341673
# [END bigquery_load_table_gcs_orc_truncate]
16351674

16361675
# Shared code
1676+
# [START bigquery_load_table_gcs_avro_truncate]
16371677
# [START bigquery_load_table_gcs_csv_truncate]
16381678
# [START bigquery_load_table_gcs_json_truncate]
16391679
# [START bigquery_load_table_gcs_parquet_truncate]
@@ -1648,6 +1688,7 @@ def test_load_table_from_uri_truncate(client, to_delete, capsys):
16481688

16491689
destination_table = client.get_table(table_ref)
16501690
print("Loaded {} rows.".format(destination_table.num_rows))
1691+
# [END bigquery_load_table_gcs_avro_truncate]
16511692
# [END bigquery_load_table_gcs_csv_truncate]
16521693
# [END bigquery_load_table_gcs_json_truncate]
16531694
# [END bigquery_load_table_gcs_parquet_truncate]

0 commit comments

Comments
 (0)