-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Add Dynamic Schema support to StorageWriteToBigQuery #39236
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
base: master
Are you sure you want to change the base?
Changes from 8 commits
2b45865
7808954
1d2ceca
85b8546
8eb3754
d0c9aa6
9f7f24b
19c296a
9aface1
8645501
ab39151
3096ec7
d2743f8
fdb3b89
7850cbd
892f3d5
ce40e8e
b226c9a
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 |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| { | ||
| "comment": "Modify this file in a trivial way to cause this test suite to run", | ||
| "modification": 16 | ||
| "modification": 21 | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -483,6 +483,67 @@ def test_write_to_dynamic_destinations(self): | |
| use_at_least_once=False)) | ||
| hamcrest_assert(p, all_of(*bq_matchers)) | ||
|
|
||
| def test_write_to_dynamic_destinations_with_dynamic_schema(self): | ||
|
Collaborator
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. Can we add a test for the case where the destination tables are pre-created/ already exist beforehand? |
||
| base_table_spec = '{}.dynamic_dest_dyn_schema_'.format(self.dataset_id) | ||
| spec_with_project = '{}:{}'.format(self.project, base_table_spec) | ||
| table_a = base_table_spec + 'users' | ||
| table_b = base_table_spec + 'scores' | ||
|
|
||
| schema_a = "id:INTEGER,name:STRING" | ||
| schema_b = "id:INTEGER,score:INTEGER,active:BOOLEAN" | ||
|
|
||
| elements_a = [ | ||
| { | ||
| 'id': 1, 'name': 'alice' | ||
| }, | ||
| { | ||
| 'id': 2, 'name': 'bob' | ||
| }, | ||
| ] | ||
| elements_b = [ | ||
| { | ||
| 'id': 101, 'score': 95, 'active': True | ||
| }, | ||
| { | ||
| 'id': 102, 'score': 80, 'active': False | ||
| }, | ||
| ] | ||
| elements = elements_a + elements_b | ||
|
|
||
| schema_map = { | ||
| spec_with_project + 'users': schema_a, | ||
| spec_with_project + 'scores': schema_b, | ||
| } | ||
|
|
||
| bq_matchers = [ | ||
| BigqueryFullResultMatcher( | ||
| project=self.project, | ||
| query="SELECT * FROM %s" % table_a, | ||
| data=self.parse_expected_data(elements_a)), | ||
| BigqueryFullResultMatcher( | ||
| project=self.project, | ||
| query="SELECT * FROM %s" % table_b, | ||
| data=self.parse_expected_data(elements_b)), | ||
| ] | ||
|
|
||
| def get_destination(record): | ||
| if 'name' in record: | ||
| return spec_with_project + 'users' | ||
| return spec_with_project + 'scores' | ||
|
|
||
| with beam.Pipeline(argv=self.args) as p: | ||
| schema_pc = p | "CreateSchema" >> beam.Create([schema_map]) | ||
| _ = ( | ||
| p | ||
| | "CreateElements" >> beam.Create(elements) | ||
| | beam.io.WriteToBigQuery( | ||
| table=get_destination, | ||
| method=beam.io.WriteToBigQuery.Method.STORAGE_WRITE_API, | ||
| schema=lambda dest, side_map: side_map[dest], | ||
| schema_side_inputs=(beam.pvalue.AsSingleton(schema_pc), ), | ||
| use_at_least_once=False)) | ||
| hamcrest_assert(p, all_of(*bq_matchers)) | ||
|
|
||
|
Collaborator
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. Can we also validate the expected schema of the tables that were created after the pipeline ran? |
||
| def test_write_to_dynamic_destinations_with_beam_rows(self): | ||
| base_table_spec = '{}.dynamic_dest_'.format(self.dataset_id) | ||
| spec_with_project = '{}:{}'.format(self.project, base_table_spec) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can also trigger .github/trigger_files/beam_PostCommit_Python_Xlang_Gcp_Direct.json for faster validation