Query the Data Delivery Network
Query the DDNThe easiest way to query any data on Splitgraph is via the "Data Delivery Network" (DDN). The DDN is a single endpoint that speaks the PostgreSQL wire protocol. Any Splitgraph user can connect to it at data.splitgraph.com:5432
and query any version of over 40,000 datasets that are hosted or proxied by Splitgraph.
For example, you can query the traffic_crashes_people
table in this repository, by referencing it like:
"cityofchicago/traffic-crashes-people-u6pd-qa9d:latest"."traffic_crashes_people"
or in a full query, like:
SELECT
":id", -- Socrata column ID
"safety_equipment", -- Safety equipment used by vehicle occupant in crash, if any
"injury_classification", -- Severity of injury person sustained in the crash
"crash_record_id", -- This number can be used to link to the same crash in the Crashes and Vehicles datasets. This number also serves as a unique ID in the Crashes dataset.
"pedpedal_visibility", -- Visibility of pedestrian of cyclist safety equipment in use at time of crash
"physical_condition", -- Driver’s apparent physical condition at time of crash, as observed by the reporting officer
"pedpedal_action", -- Action of pedestrian or cyclist at the time of crash
"bac_result", -- Status of blood alcohol concentration testing for driver or other person involved in crash
"person_type", -- Type of roadway user involved in crash
"seat_no", -- Code for seating position of motor vehicle occupant: 1= driver, 2= center front, 3 = front passenger, 4 = second row left, 5 = second row center, 6 = second row right, 7 = enclosed passengers, 8 = exposed passengers, 9= unknown position, 10 = third row left, 11 = third row center, 12 = third row right
"ejection", -- Whether vehicle occupant was ejected or extricated from the vehicle as a result of crash
"drivers_license_class", -- Class of driver's license of person involved in crash
"ems_agency", -- EMS agency who transported person injured in crash to the hospital
"bac_result_value", -- Driver’s blood alcohol concentration test result (fatal crashes may include pedestrian or cyclist results)
"drivers_license_state", -- State issuing driver's license of person involved in crash
"ems_run_no", -- EMS agency run number
"zipcode", -- ZIP Code of residence of person involved in crash
"airbag_deployed", -- Whether vehicle occupant airbag deployed as result of crash
"pedpedal_location", -- Location of pedestrian or cyclist at the time of crash
"city", -- City of residence of person involved in crash
"state", -- State of residence of person involved in crash
"driver_vision", -- What, if any, objects obscured the driver’s vision at time of crash
"person_id", -- A unique identifier for each person record. IDs starting with P indicate passengers. IDs starting with O indicate a person who was not a passenger in the vehicle (e.g., driver, pedestrian, cyclist, etc.).
"vehicle_id", -- The corresponding CRASH_UNIT_ID from the Vehicles dataset.
"sex", -- Gender of person involved in crash, as determined by reporting officer
"cell_phone_use", -- Whether person was/was not using cellphone at the time of the crash, as determined by the reporting officer
"age", -- Age of person involved in crash
"driver_action", -- Driver action that contributed to the crash, as determined by reporting officer
"crash_date", -- Date and time of crash as entered by the reporting officer
"hospital" -- Hospital to which person injured in the crash was taken
FROM
"cityofchicago/traffic-crashes-people-u6pd-qa9d:latest"."traffic_crashes_people"
LIMIT 100;
Connecting to the DDN is easy. All you need is an existing SQL client that can connect to Postgres. As long as you have a SQL client ready, you'll be able to query cityofchicago/traffic-crashes-people-u6pd-qa9d
with SQL in under 60 seconds.
This repository is an "external" repository. That means it's hosted elsewhere, in this case at data.cityofchicago.org. When you querycityofchicago/traffic-crashes-people-u6pd-qa9d:latest
on the DDN, we "mount" the repository using the socrata
mount handler. The mount handler proxies your SQL query to the upstream data source, translating it from SQL to the relevant language (in this case SoQL).
We also cache query responses on the DDN, but we run the DDN on multiple nodes so a CACHE_HIT
is only guaranteed for subsequent queries that land on the same node.
Query Your Local Engine
bash -c "$(curl -sL https://github.com/splitgraph/splitgraph/releases/latest/download/install.sh)"
Read the installation docs.
Splitgraph Cloud is built around Splitgraph Core (GitHub), which includes a local Splitgraph Engine packaged as a Docker image. Splitgraph Cloud is basically a scaled-up version of that local Engine. When you query the Data Delivery Network or the REST API, we mount the relevant datasets in an Engine on our servers and execute your query on it.
It's possible to run this engine locally. You'll need a Mac, Windows or Linux system to install sgr
, and a Docker installation to run the engine. You don't need to know how to actually use Docker; sgr
can manage the image, container and volume for you.
There are a few ways to ingest data into the local engine.
For external repositories (like this repository), the Splitgraph Engine can "mount" upstream data sources by using sgr mount
. This feature is built around Postgres Foreign Data Wrappers (FDW). You can write custom "mount handlers" for any upstream data source. For an example, we blogged about making a custom mount handler for HackerNews stories.
For hosted datasets, where the author has pushed Splitgraph Images to the repository, you can "clone" and/or "checkout" the data using sgr clone
and sgr checkout
.
Mounting Data
This repository is an external repository. It's not hosted by Splitgraph. It is hosted by data.cityofchicago.org, and Splitgraph indexes it. This means it is not an actual Splitgraph image, so you cannot use sgr clone
to get the data. Instead, you can use the socrata
adapter with the sgr mount
command. Then, if you want, you can import the data and turn it into a Splitgraph image that others can clone.
First, install Splitgraph if you haven't already.
Mount the table with sgr mount
sgr mount socrata \
"cityofchicago/traffic-crashes-people-u6pd-qa9d" \
--handler-options '{
"domain": "data.cityofchicago.org",
"tables": {
"traffic_crashes_people": "u6pd-qa9d"
}
}'
That's it! Now you can query the data in the mounted table like any other Postgres table.
Query the data with your existing tools
Once you've loaded the data into your local Splitgraph engine, you can query it with any of your existing tools. As far as they're concerned, cityofchicago/traffic-crashes-people-u6pd-qa9d
is just another Postgres schema.