1
+ /*
2
+ * Copyright 2020 Google LLC
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ package com .example .bigquery ;
18
+
19
+ // [START bigquery_update_with_dml]
20
+ import com .google .cloud .bigquery .BigQuery ;
21
+ import com .google .cloud .bigquery .BigQueryException ;
22
+ import com .google .cloud .bigquery .BigQueryOptions ;
23
+ import com .google .cloud .bigquery .FormatOptions ;
24
+ import com .google .cloud .bigquery .Job ;
25
+ import com .google .cloud .bigquery .JobId ;
26
+ import com .google .cloud .bigquery .QueryJobConfiguration ;
27
+ import com .google .cloud .bigquery .QueryParameterValue ;
28
+ import com .google .cloud .bigquery .TableDataWriteChannel ;
29
+ import com .google .cloud .bigquery .TableId ;
30
+ import com .google .cloud .bigquery .TableResult ;
31
+ import com .google .cloud .bigquery .WriteChannelConfiguration ;
32
+ import java .io .IOException ;
33
+ import java .io .OutputStream ;
34
+ import java .nio .channels .Channels ;
35
+ import java .nio .file .FileSystems ;
36
+ import java .nio .file .Files ;
37
+ import java .nio .file .Path ;
38
+ import java .util .UUID ;
39
+
40
+ // Sample to update data in BigQuery tables using DML query
41
+ public class UpdateTableDML {
42
+
43
+ public static void runUpdateTableDML () throws IOException , InterruptedException {
44
+ // TODO(developer): Replace these variables before running the sample.
45
+ String datasetName = "MY_DATASET_NAME" ;
46
+ String tableName = "MY_TABLE_NAME" ;
47
+ updateTableDML (datasetName , tableName );
48
+ }
49
+
50
+ public static void updateTableDML (String datasetName , String tableName )
51
+ throws IOException , InterruptedException {
52
+ try {
53
+ // Initialize client that will be used to send requests. This client only needs to be created
54
+ // once, and can be reused for multiple requests.
55
+ BigQuery bigquery = BigQueryOptions .getDefaultInstance ().getService ();
56
+
57
+ // Load JSON file into UserSessions table
58
+ TableId tableId = TableId .of (datasetName , tableName );
59
+
60
+ WriteChannelConfiguration writeChannelConfiguration =
61
+ WriteChannelConfiguration .newBuilder (tableId )
62
+ .setFormatOptions (FormatOptions .json ())
63
+ .build ();
64
+
65
+ // Imports a local JSON file into a table.
66
+ Path jsonPath = FileSystems .getDefault ().getPath ("src/test/resources" , "userSessionsData.json" );
67
+
68
+ // The location and JobName must be specified; other fields can be auto-detected.
69
+ String jobName = "jobId_" + UUID .randomUUID ().toString ();
70
+ JobId jobId = JobId .newBuilder ().setLocation ("us" ).setJob (jobName ).build ();
71
+
72
+ try (TableDataWriteChannel writer = bigquery .writer (jobId , writeChannelConfiguration );
73
+ OutputStream stream = Channels .newOutputStream (writer )) {
74
+ Files .copy (jsonPath , stream );
75
+ }
76
+
77
+ // Get the Job created by the TableDataWriteChannel and wait for it to complete.
78
+ Job job = bigquery .getJob (jobId );
79
+ Job completedJob = job .waitFor ();
80
+ if (completedJob == null ) {
81
+ System .out .println ("Job not executed since it no longer exists." );
82
+ return ;
83
+ } else if (completedJob .getStatus ().getError () != null ) {
84
+ System .out .println (
85
+ "BigQuery was unable to load local file to the table due to an error: \n "
86
+ + job .getStatus ().getError ());
87
+ return ;
88
+ }
89
+
90
+ System .out .println (job .getStatistics ().toString () + " userSessionsData json uploaded successfully" );
91
+
92
+ // Write a DML query to modify UserSessions table
93
+ // To create DML query job to mask the last octet in every row's ip_address column
94
+ String dmlQuery = String .format ("UPDATE `%s.%s` \n "
95
+ + "SET ip_address = REGEXP_REPLACE(ip_address, r\" (\\ .[0-9]+)$\" , \" .0\" )\n "
96
+ + "WHERE TRUE" , datasetName , tableName );
97
+
98
+ QueryJobConfiguration dmlQueryConfig =
99
+ QueryJobConfiguration .newBuilder (dmlQuery ).build ();
100
+
101
+ // Execute the query.
102
+ TableResult result = bigquery .query (dmlQueryConfig );
103
+
104
+ // Print the results.
105
+ result .iterateAll ().forEach (rows -> rows .forEach (row -> System .out .println (row .getValue ())));
106
+
107
+ System .out .println ("Table updated successfully using DML" );
108
+ } catch (BigQueryException e ) {
109
+ System .out .println ("Table update failed \n " + e .toString ());
110
+ }
111
+ }
112
+ }
113
+ // [END bigquery_update_with_dml]
0 commit comments