Skip to content

Commit

Permalink
Add new function for xlang in snippet(apache#26693)
Browse files Browse the repository at this point in the history
  • Loading branch information
RyuSA committed Jun 21, 2023
1 parent a313f21 commit 5058da6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
40 changes: 40 additions & 0 deletions sdks/python/apache_beam/examples/snippets/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1245,6 +1245,46 @@ def table_fn(element, fictional_characters):
}})
# [END model_bigqueryio_time_partitioning]

def model_bigqueryio_xlang(
pipeline, write_project='', write_dataset='', write_table=''):
"""Examples for cross-language BigQuery sources and sinks."""

table_spec = 'clouddataflow-readonly:samples.weather_stations'
if write_project and write_dataset and write_table:
table_spec = '{}:{}.{}'.format(write_project, write_dataset, write_table)

table_schema = {
'fields': [{
'name': 'source', 'type': 'STRING', 'mode': 'NULLABLE'
}, {
'name': 'quote', 'type': 'STRING', 'mode': 'REQUIRED'
}]
}

quotes = pipeline | beam.Create([
{
'source': 'Mahatma Gandhi', 'quote': 'My life is my message.'
},
{
'source': 'Yoda', 'quote': "Do, or do not. There is no 'try'."
},
])

# [START model_bigqueryio_storage_write_api_with_frequency]
# The SDK for Python does not support `withNumStorageWriteApiStreams`
quotes | "StorageWriteAPIWithFrequency" >> beam.io.WriteToBigQuery(
table_spec,
schema=table_schema,
method=beam.io.WriteToBigQuery.Method.STORAGE_WRITE_API,
triggering_frequency=5)
# [END model_bigqueryio_storage_write_api_with_frequency]

# [START model_bigqueryio_write_with_storage_write_api]
quotes | "WriteTableWithStorageAPI" >> beam.io.WriteToBigQuery(
table_spec,
schema=table_schema,
method=beam.io.WriteToBigQuery.Method.STORAGE_WRITE_API)
# [END model_bigqueryio_write_with_storage_write_api]

def model_composite_transform_example(contents, output_path):
"""Example of a composite transform.
Expand Down
8 changes: 8 additions & 0 deletions sdks/python/apache_beam/examples/snippets/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import mock
import parameterized
import pytest

import apache_beam as beam
from apache_beam import WindowInto
Expand Down Expand Up @@ -754,6 +755,13 @@ def test_model_bigqueryio(self):
p.options.view_as(GoogleCloudOptions).temp_location = 'gs://mylocation'
snippets.model_bigqueryio(p)

@pytest.mark.uses_gcp_java_expansion_service
@unittest.skipUnless(
os.environ.get('EXPANSION_PORT'),
"EXPANSION_PORT environment var is not provided.")
def test_model_bigqueryio_xlang(self):
pass

def _run_test_pipeline_for_options(self, fn):
temp_path = self.create_temp_file('aa\nbb\ncc')
result_path = temp_path + '.result'
Expand Down

0 comments on commit 5058da6

Please sign in to comment.