-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUsing BigQuery Cloud Logging to Analyze BigQuery Usage
96 lines (64 loc) · 3.81 KB
/
Using BigQuery Cloud Logging to Analyze BigQuery Usage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
##----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Paste in Terminal
export PROJECT_ID=$(gcloud config get-value project)
bq mk bq_logs
bq query --location=us --use_legacy_sql=false --use_cache=false \
'SELECT current_date'
bq query --location=us --use_legacy_sql=false --use_cache=false \
'SELECT fullName, AVG(CL.numberOfYears) avgyears
FROM `qwiklabs-resources.qlbqsamples.persons_living`, UNNEST(citiesLived) as CL
GROUP BY fullname'
bq query --location=us --use_legacy_sql=false --use_cache=false \
'select month, avg(mean_temp) as avgtemp from `qwiklabs-resources.qlweather_geo.gsod`
where station_number = 947680
and year = 2010
group by month
order by month'
bq query --location=us --use_legacy_sql=false --use_cache=false \
'select CONCAT(departure_airport, "-", arrival_airport) as route, count(*) as numberflights
from `bigquery-samples.airline_ontime_data.airline_id_codes` ac,
`qwiklabs-resources.qlairline_ontime_data.flights` fl
where ac.code = fl.airline_code
and regexp_contains(ac.airline , r"Alaska")
group by 1
order by 2 desc
LIMIT 10'
##----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Console steps
Go to Logging >> Log router
Click Create sink
Name JobComplete
click next
Select sink service: BigQuery dataset
Select Bigquery dataset (Destination): bq_logs
click next
Paste the Query Choose logs to include in sink
Build inclusion filter
resource.type="bigquery_resource"
protoPayload.methodName="jobservice.jobcompleted"
Click create
##----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Bigquery
Go to Bigquery
Paste the query Change <YOUR-PROJECT-ID> to your Project ID In the below Query
CREATE OR REPLACE VIEW
bq_logs.v_querylogs AS
SELECT
resource.labels.project_id,
protopayload_auditlog.authenticationInfo.principalEmail,
protopayload_auditlog.servicedata_v1_bigquery.jobCompletedEvent.job.jobConfiguration.query.query,
protopayload_auditlog.servicedata_v1_bigquery.jobCompletedEvent.job.jobConfiguration.query.statementType,
protopayload_auditlog.servicedata_v1_bigquery.jobCompletedEvent.job.jobStatus.error.message,
protopayload_auditlog.servicedata_v1_bigquery.jobCompletedEvent.job.jobStatistics.startTime,
protopayload_auditlog.servicedata_v1_bigquery.jobCompletedEvent.job.jobStatistics.endTime,
TIMESTAMP_DIFF(protopayload_auditlog.servicedata_v1_bigquery.jobCompletedEvent.job.jobStatistics.endTime, protopayload_auditlog.servicedata_v1_bigquery.jobCompletedEvent.job.jobStatistics.startTime, MILLISECOND)/1000 AS run_seconds,
protopayload_auditlog.servicedata_v1_bigquery.jobCompletedEvent.job.jobStatistics.totalProcessedBytes,
protopayload_auditlog.servicedata_v1_bigquery.jobCompletedEvent.job.jobStatistics.totalSlotMs,
ARRAY(SELECT as STRUCT datasetid, tableId FROM UNNEST(protopayload_auditlog.servicedata_v1_bigquery.jobCompletedEvent.job.jobStatistics.referencedTables)) as tables_ref,
protopayload_auditlog.servicedata_v1_bigquery.jobCompletedEvent.job.jobStatistics.totalTablesProcessed,
protopayload_auditlog.servicedata_v1_bigquery.jobCompletedEvent.job.jobStatistics.queryOutputRowCount,
severity
FROM
`<YOUR-PROJECT-ID>.bq_logs.cloudaudit_googleapis_com_data_access_*`
ORDER BY
startTime