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 ncvs_select_household_victimization
table in this repository, by referencing it like:
"priv-data-ojp-usdoj-gov/ncvs-select-household-victimization-gkck-euys:latest"."ncvs_select_household_victimization"
or in a full query, like:
SELECT
":id", -- Socrata column ID
"region", -- Region of respondent residence. The states have been divided into four groups or census regions, starting 1995 Q1.
"popsize", -- The size range for the place in which the housing unit is located
"hhsex", -- Respondent sex
"hincome2", -- Imputed income categories, starting 2017 Q1
"newoff", -- Type of Crime
"wgtviccy", -- Annual victimization weight
"yearq", -- Year and quarter data was collected
"year", -- Year data was collected
"hhage", -- Respondent age on the last day of the month before the interview
"hhrace", -- Respondent race
"hhrace_ethnicity", -- Respondent race and Hispanic origin
"hnumber", -- The total number of people residing in the household, including those under age 12
"msa", -- Classification of respondent residence based on the Office of Management and Budget definition of metropolitan statistical areas (MSAs)
"locality", -- Location of household based on BJS geography definitions, starting 2020 Q1
"newcrime", -- Aggregate type of crime; property crime includes burglary/trespassing, motor-vehicle theft, theft.
"notify", -- Specifies whether the crime was reported to police
"locationr", -- Specifies where the victimization occurred
"idhh", -- Unique household identifier
"hhhisp", -- Respondent Hispanic origin
"hincome1", -- Total income of all members of the household for the 12 months preceding the interview. Categories available from 1993-2020 with imputed data, starting 2015 Q1.
"vicservices", -- Specifies whether victims received any help or advice from victim service agencies
"series", -- Identifies incident as a series
"newwgt" -- Series adjusted victimization weight
FROM
"priv-data-ojp-usdoj-gov/ncvs-select-household-victimization-gkck-euys:latest"."ncvs_select_household_victimization"
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 priv-data-ojp-usdoj-gov/ncvs-select-household-victimization-gkck-euys
with SQL in under 60 seconds.
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, 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 (like this repository), where the author has pushed Splitgraph Images to the repository, you can "clone" and/or "checkout" the data using sgr clone
and sgr checkout
.
Cloning Data
Because priv-data-ojp-usdoj-gov/ncvs-select-household-victimization-gkck-euys:latest
is a Splitgraph Image, you can clone the data from Spltgraph Cloud to your local engine, where you can query it like any other Postgres database, using any of your existing tools.
First, install Splitgraph if you haven't already.
Clone the metadata with sgr clone
This will be quick, and does not download the actual data.
sgr clone priv-data-ojp-usdoj-gov/ncvs-select-household-victimization-gkck-euys
Checkout the data
Once you've cloned the data, you need to "checkout" the tag that you want. For example, to checkout the latest
tag:
sgr checkout priv-data-ojp-usdoj-gov/ncvs-select-household-victimization-gkck-euys:latest
This will download all the objects for the latest
tag of priv-data-ojp-usdoj-gov/ncvs-select-household-victimization-gkck-euys
and load them into the Splitgraph Engine. Depending on your connection speed and the size of the data, you will need to wait for the checkout to complete. Once it's complete, you will be able to query the data like you would any other Postgres database.
Alternatively, use "layered checkout" to avoid downloading all the data
The data in priv-data-ojp-usdoj-gov/ncvs-select-household-victimization-gkck-euys:latest
is 0 bytes. If this is too big to download all at once, or perhaps you only need to query a subset of it, you can use a layered checkout.:
sgr checkout --layered priv-data-ojp-usdoj-gov/ncvs-select-household-victimization-gkck-euys:latest
This will not download all the data, but it will create a schema comprised of foreign tables, that you can query as you would any other data. Splitgraph will lazily download the required objects as you query the data. In some cases, this might be faster or more efficient than a regular checkout.
Read the layered querying documentation to learn about when and why you might want to use layered queries.
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, priv-data-ojp-usdoj-gov/ncvs-select-household-victimization-gkck-euys
is just another Postgres schema.