Skip to main content
Version: 8.0.1

Running SQL on FHIR views

The Pathling library leverages the SQL on FHIR specification to provide a way to project FHIR data into easy-to-use tabular forms.

Once you have transformed your FHIR data into tabular views, you can choose to keep it in a Spark dataframe and continue to work with in Apache Spark, or export it to Python or R dataframes or a variety of different file formats for use in the tool of your choice.

from pathling import PathlingContext

pc = PathlingContext.create()
data = pc.read.ndjson("/some/file/location")

result = data.view(
resource="Patient",
select=[
{"column": [{"path": "getResourceKey()", "name": "patient_id"}]},
{
"forEach": "address",
"column": [
{"path": "line.join('\\n')", "name": "street"},
{"path": "use", "name": "use"},
{"path": "city", "name": "city"},
{"path": "postalCode", "name": "zip"},
],
},
],
)

display(result)

The result of this query would look something like this:

patient_idstreetusecityzip
1398 Kautzer Walk Suite 62homeBarnstable02675
1186 Nitzsche ForgeworkRevere02151
21087 Quitzon ClubhomePlymouthNULL
3442 Bruen ArcadehomeNantucketNULL
4858 Miller Junction Apt 61workBrockton02301

For a more comprehensive example demonstrating SQL on FHIR queries with multiple views, complex transformations and joins, see the SQL on FHIR example.