generated from databricks-industry-solutions/industry-solutions-blueprints
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtutorial_graph_mapping.py
234 lines (198 loc) · 8.16 KB
/
tutorial_graph_mapping.py
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# Databricks notebook source
# MAGIC %md
# MAGIC
# MAGIC ## How to map any JSON to a Graph Data Model
# MAGIC
# MAGIC Whether you are doing schema-on-read or schema-on-write, you still need to map your source data schemas to a graph data model.
# MAGIC
# MAGIC <img src="https://github.com/lipyeowlim/public/raw/main/img/context-graph/graph_data_model.png" width="800px">
# COMMAND ----------
# MAGIC %sql
# MAGIC use schema {{tgt_db_name}};
# COMMAND ----------
# MAGIC %sql
# MAGIC SELECT raw
# MAGIC FROM okta_bronze
# MAGIC WHERE raw:eventType IN ('user.authentication.auth_via_mfa')
# MAGIC LIMIT 5
# MAGIC ;
# COMMAND ----------
# DBTITLE 1,Pretty print one JSON raw string
import json
df = spark.sql(f"""
SELECT raw
FROM okta_bronze
WHERE raw:eventType IN ('user.authentication.auth_via_mfa')
LIMIT 1""")
(json_str,) = df.first()
obj = json.loads(json_str)
print(json.dumps(obj, indent=2))
# COMMAND ----------
# MAGIC %md
# MAGIC
# MAGIC ## 1-to-1: Extracting 1 source row to 1 target edge
# COMMAND ----------
# MAGIC %sql
# MAGIC SELECT event_ts,
# MAGIC rid as src_rid,
# MAGIC 'user-okta' as sub_type,
# MAGIC raw:actor.id as sub_id,
# MAGIC raw:actor.alternateId as sub_name,
# MAGIC 'ipAddress'::string as obj_type,
# MAGIC raw:client.ipAddress::string as obj_id,
# MAGIC raw:client.ipAddress::string as obj_name,
# MAGIC 'uses'::string as pred,
# MAGIC null::string as pred_status
# MAGIC FROM okta_bronze
# MAGIC WHERE raw:eventType IN ('user.authentication.auth_via_mfa')
# MAGIC LIMIT 5
# MAGIC ;
# COMMAND ----------
# MAGIC %md
# MAGIC
# MAGIC ## 1-to-many: Extracting 1 source row to multiple edges with the same subject
# COMMAND ----------
# DBTITLE 1,Use inline(), array(), struct()
# MAGIC %sql
# MAGIC SELECT event_ts,
# MAGIC rid as src_rid,
# MAGIC 'user-okta' as sub_type,
# MAGIC raw:actor.id as sub_id,
# MAGIC raw:actor.alternateId as sub_name,
# MAGIC inline(
# MAGIC array(
# MAGIC struct('ipAddress'::string, raw:client.ipAddress::string, raw:client.ipAddress::string, 'uses'::string, null::string)
# MAGIC )
# MAGIC ) as (obj_type, obj_id, obj_name, pred, pred_status)
# MAGIC FROM okta_bronze
# MAGIC WHERE raw:eventType IN ('user.authentication.auth_via_mfa')
# MAGIC LIMIT 5
# MAGIC ;
# COMMAND ----------
# DBTITLE 1,Let's extract the geolocation of the client as an edge as well
# MAGIC %sql
# MAGIC SELECT event_ts,
# MAGIC rid as src_rid,
# MAGIC 'user-okta' as sub_type,
# MAGIC raw:actor.id as sub_id,
# MAGIC raw:actor.alternateId as sub_name,
# MAGIC inline(
# MAGIC array(
# MAGIC struct('ipAddress'::string, raw:client.ipAddress::string, raw:client.ipAddress::string, 'uses'::string, null::string),
# MAGIC struct('city_state'::string, raw:client.geographicalContext.postalCode::string,
# MAGIC raw:client.geographicalContext.city::string || ',' || raw:client.geographicalContext.state::string || ',' || raw:client.geographicalContext.country::string,
# MAGIC 'located_at'::string, null::string)
# MAGIC )
# MAGIC ) as (obj_type, obj_id, obj_name, pred, pred_status)
# MAGIC FROM okta_bronze
# MAGIC WHERE raw:eventType IN ('user.authentication.auth_via_mfa')
# MAGIC LIMIT 5
# MAGIC ;
# COMMAND ----------
# MAGIC %md
# MAGIC
# MAGIC ## What if there is an array in the json and we want to create an edge for each array element?
# MAGIC
# MAGIC We will use some SQL high-order functions!
# MAGIC
# MAGIC <img src="https://github.com/lipyeowlim/public/raw/main/img/context-graph/graph_data_model_mapping.png" width="800px">
# COMMAND ----------
# DBTITLE 1,Extract the fields in the array of records that we want
# MAGIC %sql
# MAGIC SELECT
# MAGIC from_json(raw:target[*].id, 'array<string>') AS target_ids,
# MAGIC from_json(raw:target[*].type, 'array<string>') AS target_types,
# MAGIC from_json(raw:target[*].displayName, 'array<string>') AS target_names
# MAGIC FROM okta_bronze
# MAGIC WHERE raw:eventType IN ('user.authentication.auth_via_mfa');
# COMMAND ----------
# DBTITLE 1,Stitch the extracted fields back into an array of structs
# MAGIC %sql
# MAGIC SELECT
# MAGIC arrays_zip( from_json(raw:target[*].id, 'array<string>'), from_json(raw:target[*].type, 'array<string>'), from_json(raw:target[*].displayName, 'array<string>') ) as extracted
# MAGIC FROM okta_bronze
# MAGIC WHERE raw:eventType IN ('user.authentication.auth_via_mfa');
# COMMAND ----------
# DBTITLE 1,We want to add some extra fields to each struct
# MAGIC %sql
# MAGIC SELECT
# MAGIC transform(
# MAGIC arrays_zip( from_json(raw:target[*].id, 'array<string>'), from_json(raw:target[*].type, 'array<string>'), from_json(raw:target[*].displayName, 'array<string>') ),
# MAGIC x -> struct(
# MAGIC x['1']::string,
# MAGIC x['0']::string,
# MAGIC x['2']::string,
# MAGIC 'signin'::string,
# MAGIC case when raw:outcome.result = 'SUCCESS' then 'success'::string else 'failed'::string end
# MAGIC )
# MAGIC ) as extracted
# MAGIC FROM okta_bronze
# MAGIC WHERE raw:eventType IN ('user.authentication.auth_via_mfa');
# COMMAND ----------
# DBTITLE 1,Now we use inline to turn the array of structs into rows
# MAGIC %sql
# MAGIC SELECT
# MAGIC inline(
# MAGIC transform(
# MAGIC arrays_zip( from_json(raw:target[*].id, 'array<string>'), from_json(raw:target[*].type, 'array<string>'), from_json(raw:target[*].displayName, 'array<string>') ),
# MAGIC x -> struct(
# MAGIC x['1']::string,
# MAGIC x['0']::string,
# MAGIC x['2']::string,
# MAGIC 'signin'::string,
# MAGIC case when raw:outcome.result = 'SUCCESS' then 'success'::string else 'failed'::string end
# MAGIC )
# MAGIC )
# MAGIC ) AS (obj_type, obj_id, obj_name, pred, pred_status)
# MAGIC FROM okta_bronze
# MAGIC WHERE raw:eventType IN ('user.authentication.auth_via_mfa');
# COMMAND ----------
# DBTITLE 1,But we don't want the obj_type = User, let's filter those out
# MAGIC %sql
# MAGIC SELECT
# MAGIC inline(
# MAGIC transform(
# MAGIC filter(
# MAGIC arrays_zip( from_json(raw:target[*].id, 'array<string>'), from_json(raw:target[*].type, 'array<string>'), from_json(raw:target[*].displayName, 'array<string>') ),
# MAGIC x-> x['1']!='User' and x['1']!='AppUser'),
# MAGIC x -> struct(
# MAGIC x['1']::string,
# MAGIC x['0']::string,
# MAGIC x['2']::string,
# MAGIC 'signin'::string,
# MAGIC case when raw:outcome.result = 'SUCCESS' then 'success'::string else 'failed'::string end
# MAGIC )
# MAGIC )
# MAGIC ) AS (obj_type, obj_id, obj_name, pred, pred_status)
# MAGIC FROM okta_bronze
# MAGIC WHERE raw:eventType IN ('user.authentication.auth_via_mfa');
# COMMAND ----------
# DBTITLE 1,Putting it altogether
# MAGIC %sql
# MAGIC SELECT event_ts,
# MAGIC rid as src_rid,
# MAGIC 'user-okta' as sub_type,
# MAGIC raw:actor.id as sub_id,
# MAGIC raw:actor.alternateId as sub_name,
# MAGIC inline(
# MAGIC array_union(
# MAGIC array(
# MAGIC struct('ipAddress'::string, raw:client.ipAddress::string, raw:client.ipAddress::string, 'uses'::string, null::string),
# MAGIC struct('city_state'::string, raw:client.geographicalContext.postalCode::string,
# MAGIC raw:client.geographicalContext.city::string || ',' || raw:client.geographicalContext.state::string || ',' || raw:client.geographicalContext.country::string,
# MAGIC 'located_at'::string, null::string)
# MAGIC ),
# MAGIC transform(
# MAGIC filter( arrays_zip( from_json(raw:target[*].id, 'array<string>'), from_json(raw:target[*].type, 'array<string>'), from_json(raw:target[*].displayName, 'array<string>') ), x-> x['1']!='User' and x['1']!='AppUser' ), x -> struct(
# MAGIC x['1']::string,
# MAGIC x['0']::string,
# MAGIC x['2']::string,
# MAGIC 'signin'::string,
# MAGIC case when raw:outcome.result = 'SUCCESS' then 'success'::string else 'failed'::string end)
# MAGIC )
# MAGIC )
# MAGIC ) as (obj_type, obj_id, obj_name, pred, pred_status)
# MAGIC FROM okta_bronze
# MAGIC WHERE raw:eventType IN ('user.authentication.auth_via_mfa');
# MAGIC --, 'user.authentication.sso', 'user.authentication.auth_via_social');
# COMMAND ----------