|
| 1 | +# Copyright 2021 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# [START bigquery_update_with_dml] |
| 16 | +importpathlib |
| 17 | + |
| 18 | +fromgoogle.cloudimportbigquery |
| 19 | +fromgoogle.cloud.bigqueryimportenums |
| 20 | + |
| 21 | + |
| 22 | +defload_from_newline_delimited_json( |
| 23 | +client:bigquery.Client, |
| 24 | +filepath:pathlib.Path, |
| 25 | +project_id:str, |
| 26 | +dataset_id:str, |
| 27 | +table_id:str, |
| 28 | +): |
| 29 | +full_table_id=f"{project_id}.{dataset_id}.{table_id}" |
| 30 | +job_config=bigquery.LoadJobConfig() |
| 31 | +job_config.source_format=enums.SourceFormat.NEWLINE_DELIMITED_JSON |
| 32 | +job_config.schema= [ |
| 33 | +bigquery.SchemaField("id",enums.SqlTypeNames.STRING), |
| 34 | +bigquery.SchemaField("user_id",enums.SqlTypeNames.INTEGER), |
| 35 | +bigquery.SchemaField("login_time",enums.SqlTypeNames.TIMESTAMP), |
| 36 | +bigquery.SchemaField("logout_time",enums.SqlTypeNames.TIMESTAMP), |
| 37 | +bigquery.SchemaField("ip_address",enums.SqlTypeNames.STRING), |
| 38 | + ] |
| 39 | + |
| 40 | +withopen(filepath,"rb")asjson_file: |
| 41 | +load_job=client.load_table_from_file( |
| 42 | +json_file,full_table_id,job_config=job_config |
| 43 | + ) |
| 44 | + |
| 45 | +# Wait for load job to finish. |
| 46 | +load_job.result() |
| 47 | + |
| 48 | + |
| 49 | +defupdate_with_dml( |
| 50 | +client:bigquery.Client,project_id:str,dataset_id:str,table_id:str |
| 51 | +): |
| 52 | +query_text=f""" |
| 53 | + UPDATE `{project_id}.{dataset_id}.{table_id}` |
| 54 | + SET ip_address = REGEXP_REPLACE(ip_address, r"(\\.[0-9]+)$", ".0") |
| 55 | + WHERE TRUE |
| 56 | + """ |
| 57 | +query_job=client.query(query_text) |
| 58 | + |
| 59 | +# Wait for query job to finish. |
| 60 | +query_job.result() |
| 61 | + |
| 62 | +print(f"DML query modified{query_job.num_dml_affected_rows} rows.") |
| 63 | +returnquery_job.num_dml_affected_rows |
| 64 | + |
| 65 | + |
| 66 | +defrun_sample(override_values={}): |
| 67 | +client=bigquery.Client() |
| 68 | +filepath=pathlib.Path(__file__).parent/"user_sessions_data.json" |
| 69 | +project_id=client.project |
| 70 | +dataset_id="sample_db" |
| 71 | +table_id="UserSessions" |
| 72 | +# [END bigquery_update_with_dml] |
| 73 | +# To facilitate testing, we replace values with alternatives |
| 74 | +# provided by the testing harness. |
| 75 | +dataset_id=override_values.get("dataset_id",dataset_id) |
| 76 | +table_id=override_values.get("table_id",table_id) |
| 77 | +# [START bigquery_update_with_dml] |
| 78 | +load_from_newline_delimited_json(client,filepath,project_id,dataset_id,table_id) |
| 79 | +returnupdate_with_dml(client,project_id,dataset_id,table_id) |
| 80 | + |
| 81 | + |
| 82 | +# [END bigquery_update_with_dml] |