-
Notifications
You must be signed in to change notification settings - Fork 8
/
hl7_fhir_json_manipulation_export.sql
364 lines (236 loc) · 7.64 KB
/
hl7_fhir_json_manipulation_export.sql
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
Change Log
Feb 15 2024 AWong Changed to @play_stage internal stage and added create DB and warehouse
References
https://www.hl7.org/fhir/patient-examples.html
https://www.hl7.org/fhir/patient-example-c.html
https://docs.snowflake.com/en/sql-reference/functions-semistructured.html
What we will see:
Using Snowflake for Data Engineering.
Create two Fast Healthcare Interoperability Resources (FHIR) record for deceased patient in Variant DataType
Create two structured data records from Snowflake Shared Data
Join those two records based on their IDs
Use Semi-Structured functions like object_construct and array_agg to manipulate the FHIR / JSON data
Verify records exported as JSON
Benefits
Snowflake for your Data Lake and Data Engineering workloads
High-performance, governed ETL / ELT with only SQL and no separate tools or data silos
Lower TCO with fewer tools, fewer data silos, and less operational risk
*/
--------------------------------------------------------
--setup
use role sysadmin;
--warehouse, database, schema
create warehouse if not exists compute_wh with warehouse_size = 'xsmall' auto_suspend = 60 initially_suspended = true;
create database if not exists play_db;
create schema if not exists play_db.public;
--set context
use role sysadmin; use warehouse compute_wh; use schema play_db.public;
//create FHIR / JSON patient record for Sherry Small
create or replace transient table patient
(
deceased variant
)
as
select parse_json(column1) as src
from values
($$
{
"resourceType": "Patient",
"id": "63081838",
"text": {
"status": "generated",
"div": "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n\t\t\t<p>Patient Sherry Small @ Acme Healthcare, Inc. MR = 123458, DECEASED</p>\n\t\t</div>"
},
"identifier": [
{
"use": "usual",
"type": {
"coding": [
{
"system": "http://terminology.hl7.org/CodeSystem/v2-0203",
"code": "MR"
}
]
},
"system": "urn:oid:0.1.2.3.4.5.6.7",
"value": "123458"
}
],
"active": true,
"name": [
{
"use": "official",
"family": "Small",
"given": [
"Sherry"
]
}
],
"gender": "female",
"birthDate": "1928-12-28",
"deceasedBoolean": true,
"managingOrganization": {
"reference": "Organization/1",
"display": "ACME Healthcare, Inc"
}
}
$$);
-----------------------------------------------------
--add patient record for Bobby Harmon
insert into patient
select parse_json(
$$
{
"resourceType": "Patient",
"id": "51991432",
"text": {
"status": "generated",
"div": "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n\t\t\t<p>Patient Bobby Harmon @ Acme Healthcare, Inc. MR = 123458, DECEASED</p>\n\t\t</div>"
},
"identifier": [
{
"use": "usual",
"type": {
"coding": [
{
"system": "http://terminology.hl7.org/CodeSystem/v2-0203",
"code": "MR"
}
]
},
"system": "urn:oid:0.1.2.3.4.5.6.7",
"value": "123458"
}
],
"active": true,
"name": [
{
"use": "official",
"family": "Harmon",
"given": [
"Bobby"
]
}
],
"gender": "male",
"birthDate": "1982-08-02",
"deceasedBoolean": true,
"managingOrganization": {
"reference": "Organization/1",
"display": "ACME Healthcare, Inc"
}
}
$$);
//Variant stores raw JSON / FHIR
select top 300 *
from patient;
-----------------------------------------------------
--create relational table from Snowflake Share with matching ID
create or replace temp table customer as
select
c.c_customer_sk, c_first_name, c_last_name, c_birth_country, c_email_address, ca_street_number, ca_street_name,
ca_street_type, ca_suite_number, ca_city, ca_county, ca_state, ca_zip, ca_country
from snowflake_sample_data.tpcds_sf10tcl.customer c
inner join snowflake_sample_data.tpcds_sf10tcl.customer_address ca on c.c_current_addr_sk = ca.ca_address_sk
where c_customer_sk in (63081838 --Sherry Small
,51991432); --Bobby Harmon
--FHIR / JSON joined with relational on customer key
select *
from patient p
inner join customer c on p.deceased:id::integer = c.c_customer_sk;
--combine into new FHIR / JSON as-is
--Snowflake for your Data Engineering and Data Lake
--notice all columns without nesting
select object_construct(*)
from patient p
inner join
(
select object_construct(*) customer
from customer
) c on p.deceased:id::integer = c.customer:C_CUSTOMER_SK;
--Advanced Data Engineering: let's nest the address and include fewer columns
select object_construct(
'c_address', array_agg(
object_construct
(
'ca_street_number', ca_street_number,
'ca_street_name', ca_street_name,
'ca_street_type', ca_street_type,
'ca_suite_number', ca_suite_number,
'ca_city', ca_city,
'ca_county', ca_county,
'ca_state', ca_state,
'ca_zip', ca_zip,
'ca_country', ca_country
)
),
'c_birth_country', C_BIRTH_COUNTRY,
'c_customer_sk', C_CUSTOMER_SK,
'c_email_address', C_EMAIL_ADDRESS
) customer
from customer
group by C_BIRTH_COUNTRY, C_CUSTOMER_SK, C_EMAIL_ADDRESS
;
-----------------------------------------------------
--combine it all
create or replace transient table patient_export (v variant);
insert into patient_export
select object_construct(*)
from patient p
inner join
(
select object_construct(
'c_address', array_agg(
object_construct
(
'ca_street_number', ca_street_number,
'ca_street_name', ca_street_name,
'ca_street_type', ca_street_type,
'ca_suite_number', ca_suite_number,
'ca_city', ca_city,
'ca_county', ca_county,
'ca_state', ca_state,
'ca_zip', ca_zip,
'ca_country', ca_country
)
),
'c_birth_country', C_BIRTH_COUNTRY,
'c_customer_sk', C_CUSTOMER_SK,
'c_email_address', C_EMAIL_ADDRESS
) customer
from customer
group by C_BIRTH_COUNTRY, C_CUSTOMER_SK, C_EMAIL_ADDRESS
) c
on p.deceased:id::integer = c.CUSTOMER:c_customer_sk;
select * from patient_export;
-----------------------------------------------------
--export as JSON file to data lake
create stage if not exists play_stage;
--see what is in our cloud storage
ls @play_stage;
--optional: remove file(s)
rm @play_stage;
--export to cloud storage
copy into @play_db.public.play_stage from
(select * from patient_export)
file_format=(type=json)
overwrite = true;
--verify files here or in cloud storage
ls @play_db.public.play_stage;
select $1, $2, $3, $4, $5, $6, $7, $8 from @play_db.public.play_stage;
/*
What we saw:
Using Snowflake for Data Engineering.
Create two Fast Healthcare Interoperability Resources (FHIR) record for deceased patient in Variant DataType
Create two structured data records from Snowflake Shared Data
Join those two records based on their IDs
Use Semi-Structured functions like object_construct and array_agg to manipulate the FHIR / JSON data
Verify records exported as JSON
Benefits
Snowflake for your Data Lake and Data Engineering workloads
High-performance, governed ETL / ELT with only SQL and no separate tools or data silos
Lower TCO with fewer tools, fewer data silos, and less operational risk
*/
--reset
-- rm @play_db.public.play_stage;