Published
Edited
Sep 7, 2020
1 fork
1 star
Insert cell
md`# Splitgraph SQL API example

This notebook showcases querying the Splitgraph SQL API that's powered by the [Splitgraph DDN](https://www.splitgraph.com/connect).

You can also get access to an SQL editor at https://www.splitgraph.com/workspace/ddn.

First, get an anonymous access token from Splitgraph.

If you have signed up for Splitgraph already and got a pair of API keys, you can use them here instead:

\`\`\`javascript
tokenRequest = (await fetch("https://api.splitgraph.com/auth/access_token",
{method: "POST",
headers: {
"Content-Type": "application/json"},
body: JSON.stringify(
{api_key: Secret("SPLITGRAPH_API_KEY"),
api_secret: Secret("SPLITGRAPH_API_SECRET")})})).json();
\`\`\`
`
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell
import { vl } from "@vega/vega-lite-api"
Insert cell

tokenRequest = (await fetch("https://api.splitgraph.com/auth/anonymous_access_token", {"method": "POST"})).json()
Insert cell
md `Use the token to run a POST request to \`data.splitgraph.com/sql/query/ddn\`:


\`\`\`sql
SELECT EXTRACT('hour' FROM time) AS hour, COUNT(1) AS count
FROM "splitgraph/congress_tweets".tweets
GROUP BY EXTRACT('hour' FROM time)
\`\`\`
`
Insert cell
sqlRequest = (await fetch("https://data.splitgraph.com/sql/query/ddn",
{method: "POST",
headers: {
"Authorization": "Bearer " + tokenRequest.access_token,
"Content-Type": "application/json"},
body: JSON.stringify({"sql":
`SELECT EXTRACT('hour' FROM time) AS hour, COUNT(1) AS count
FROM "splitgraph/congress_tweets".tweets
GROUP BY EXTRACT('hour' FROM time)`})})).json();
Insert cell
md`What are the most popular hours for Congress members to tweet?`
Insert cell
vl.markBar()
.data(sqlRequest.rows)
.encode(
vl.x().fieldO('hour'),
vl.y().fieldQ('count')
)
.render()
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more