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 crime_data_from_2020_to_present
table in this repository, by referencing it like:
"lacity/crime-data-from-2020-to-present-2nrs-mtv8:latest"."crime_data_from_2020_to_present"
or in a full query, like:
SELECT
":id", -- Socrata column ID
"lon", -- Longtitude
"crm_cd_2", -- May contain a code for an additional crime, less serious than Crime Code 1.
"premis_desc", -- Defines the Premise Code provided.
"crm_cd_desc", -- Defines the Crime Code provided.
"date_rptd", -- MM/DD/YYYY
"lat", -- Latitude
"cross_street", -- Cross Street of rounded Address
"location", -- Street address of crime incident rounded to the nearest hundred block to maintain anonymity.
"weapon_used_cd", -- The type of weapon used in the crime.
"vict_age", -- Two character numeric
"rpt_dist_no", -- A four-digit code that represents a sub-area within a Geographic Area. All crime records reference the "RD" that it occurred in for statistical comparisons. Find LAPD Reporting Districts on the LA City GeoHub at http://geohub.lacity.org/datasets/c4f83909b81d4786aa8ba8a74a4b4db1_4
"area", -- The LAPD has 21 Community Police Stations referred to as Geographic Areas within the department. These Geographic Areas are sequentially numbered from 1-21.
"vict_sex", -- F - Female M - Male X - Unknown
"crm_cd_4", -- May contain a code for an additional crime, less serious than Crime Code 1.
"status", -- Status of the case. (IC is the default)
"weapon_desc", -- Defines the Weapon Used Code provided.
"premis_cd", -- The type of structure, vehicle, or location where the crime took place.
"area_name", -- The 21 Geographic Areas or Patrol Divisions are also given a name designation that references a landmark or the surrounding community that it is responsible for. For example 77th Street Division is located at the intersection of South Broadway and 77th Street, serving neighborhoods in South Los Angeles.
"mocodes", -- Modus Operandi: Activities associated with the suspect in commission of the crime.See attached PDF for list of MO Codes in numerical order. https://data.lacity.org/api/views/y8tr-7khq/files/3a967fbd-f210-4857-bc52-60230efe256c?download=true&filename=MO%20CODES%20(numerical%20order).pdf
"crm_cd", -- Indicates the crime committed. (Same as Crime Code 1)
"date_occ", -- MM/DD/YYYY
"dr_no", -- Division of Records Number: Official file number made up of a 2 digit year, area ID, and 5 digits
"time_occ", -- In 24 hour military time.
"part_1_2",
"crm_cd_3", -- May contain a code for an additional crime, less serious than Crime Code 1.
"crm_cd_1", -- Indicates the crime committed. Crime Code 1 is the primary and most serious one. Crime Code 2, 3, and 4 are respectively less serious offenses. Lower crime class numbers are more serious.
"vict_descent", -- Descent Code: A - Other Asian B - Black C - Chinese D - Cambodian F - Filipino G - Guamanian H - Hispanic/Latin/Mexican I - American Indian/Alaskan Native J - Japanese K - Korean L - Laotian O - Other P - Pacific Islander S - Samoan U - Hawaiian V - Vietnamese W - White X - Unknown Z - Asian Indian
"status_desc" -- Defines the Status Code provided.
FROM
"lacity/crime-data-from-2020-to-present-2nrs-mtv8:latest"."crime_data_from_2020_to_present"
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 lacity/crime-data-from-2020-to-present-2nrs-mtv8
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 lacity/crime-data-from-2020-to-present-2nrs-mtv8: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 lacity/crime-data-from-2020-to-present-2nrs-mtv8
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 lacity/crime-data-from-2020-to-present-2nrs-mtv8:latest
This will download all the objects for the latest
tag of lacity/crime-data-from-2020-to-present-2nrs-mtv8
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 lacity/crime-data-from-2020-to-present-2nrs-mtv8: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 lacity/crime-data-from-2020-to-present-2nrs-mtv8: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, lacity/crime-data-from-2020-to-present-2nrs-mtv8
is just another Postgres schema.