docs: add jupyter notebooks for data connectors

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/10009
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Sean Park-Ross <94021366+seanparkross@users.noreply.github.com>
GitOrigin-RevId: c69c05b4502f3bb428ab7dfaba3e3252d18855bd
This commit is contained in:
Rob Dominguez 2023-08-07 13:20:47 -04:00 committed by hasura-bot
parent a3bf146a00
commit 6d3a426250
9 changed files with 449 additions and 192 deletions

View File

@ -1,192 +0,0 @@
---
description: Information about Hasura Data Connectors
sidebar_label: Data Connectors
keywords:
- hasura
- docs
- databases
- data connectors
sidebar_position: 1.4
---
import Thumbnail from '@site/src/components/Thumbnail';
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
# Hasura Data Connectors
## Introduction
Hasura Data Connectors provide an easy way to build connectors to any data source and instantly obtain GraphQL APIs on
that data.
Currently, Hasura natively supports Postgres, SQL Server, and BigQuery databases. Data Connectors allow you to connect
Hasura to **_any_** other data source. Hasura has used Data Connectors to build connectors to MySQL, Oracle, Snowflake,
Amazon Athena, MariaDB, MongoDB (coming soon), with more sources in the pipeline, but you can also use them to connect
to your own. Think Microsoft Excel, SQLite, CSV, AirTable and more.
For more information on databases, check out the [Hasura Databases documentation](/docs/databases/overview.mdx) or to
jump right into integrating a native database, [check out the Quickstart](/docs/databases/quickstart.mdx).
This documentation will guide you through understanding Hasura Data Connectors concepts and how to use them.
:::warning Active Development
Data Connectors are currently in active development and are likely to change considerably. We are working hard to make
them as stable as possible soon, but please be aware that breaking changes may occur.
:::
## Hasura GraphQL Data Connector Agent
The Hasura GraphQL Data Connector Agent is a service that acts as an intermediary middleware abstraction between a data
source and the Hasura GraphQL Engine via a REST API. It allows users to implement Hasura's powerful GraphQL experience
on any data source they use. Further information about the design and implementation of the Agent service can be found
in the
[README.md of the Data Connector Agent repository](https://github.com/hasura/graphql-engine/blob/master/dc-agents/README.md).
<Thumbnail
src="/img/databases/data-connector/data-connectors-agent-diagram.png"
alt="Hasura GraphQL Data Connector Agent diagram"
/>
In addition, an agent can directly support new functionality without any other database upstream. The purpose of Data
Connector agents is to quickly and easily allow developers to author new agents to support a wide variety of new data
sources and use-cases.
## Adding Hasura GraphQL Data Connector Agent to Metadata
To use a custom Hasura GraphQL Data Connector Agent, follow the following steps on Console to add it to HGE metadata.
Before following the steps, make sure the custom connector is deployed and accessible.
<Tabs groupId="user-preference" className="api-tabs">
<TabItem value="console" label="Console">
<ol>
<li>
Navigate to the project Console to the Data tab.
<Thumbnail src="/img/databases/data-connector/data-tab.png" alt="Hasura GraphQL Data Connector Agent diagram" />
</li>
<li>
Click on the `Manage` button.
<Thumbnail
src="/img/databases/data-connector/manage-db.png"
alt="Hasura GraphQL Data Connector Agent diagram"
/>
</li>
<li>
Click on the `Data Connector Agents` button.
<Thumbnail
src="/img/databases/data-connector/connect-agent.png"
alt="Hasura GraphQL Data Connector Agent diagram"
/>
</li>
<li>
Click on the `Add Agent` button.
<Thumbnail
src="/img/databases/data-connector/add-agent.png"
alt="Hasura GraphQL Data Connector Agent diagram"
/>
</li>
<li>
Enter the values for name and agent endpoint. Click `Connect` and you're done!
<Thumbnail
src="/img/databases/data-connector/connect-final.png"
alt="Hasura GraphQL Data Connector Agent diagram"
/>
</li>
<li>
Once the above is done, the new driver supported by the custom connector will be available as one of the options
in the `Connect Database` page.
<Thumbnail
src="/img/databases/data-connector/updated-driver-list.png"
alt="Hasura GraphQL Data Connector Agent diagram"
/>
</li>
</ol>
</TabItem>
<TabItem value="cli" label="CLI">
You can add a DC agent by adding their config to the `/metadata/backend_configs.yaml` file:
```yaml
dataconnector:
sqlite:
uri: <dc agent url>
```
Apply the Metadata by running:
```yaml
hasura metadata apply
```
</TabItem>
<TabItem value="api" label="API">
You can add a DC agent to Hasura via the `dc_add_agent` Metadata API.
```http
POST /v1/metadata HTTP/1.1
Content-Type: application/json
X-Hasura-Role: admin
{
"type": "dc_add_agent",
"args": {
"name": "sqlite",
"url": "<URL where the DC agent is deployed>"
}
}
```
</TabItem>
</Tabs>
## Hasura GraphQL Data Connector SDK
The Data Connector SDK serves as a pack of documentation and resources for understanding, building, and testing Data
Connector agent implementations to ensure that they are complete, correct, idiomatic and can be developed rapidly and
with confidence. The workflow that the SDK supports out of the box is powered by Docker Compose in order to reduce the
number of dependencies required, but each component may be run natively if desired. The SDK is versioned with the Hasura
GraphQL Engine.
The SDK including the `docker-compose.yaml` file can be
[found here](https://github.com/hasura/graphql-engine/tree/master/dc-agents/sdk).
For more details on the SDK, check out the
[SDK repository README.md](https://github.com/hasura/graphql-engine/blob/master/dc-agents/sdk/README.md).
### Building a New Data Connector with the SDK
To create a new Data Connector, follow these steps:
1. [Start the container](https://github.com/hasura/graphql-engine/blob/master/dc-agents/sdk/docker-compose.yaml) with
`docker compose up`.
2. Verify that tests pass.
3. Review & make changes to the reference connector for your specific database and intended implementation.
4. Or rebuild as required, depending on your stack.
5. Rerun the tests with `docker compose run tests`.
6. Interact with the agent via Hasura GraphQL Engine at `http://localhost:8080` and view the OpenAPI Schema at
`http://localhost:8300`.
## Reference Connector
The reference connector is located under the `/reference` path within the SDK and serves as a working connector example.
It is written in TypeScript and has several key code snippet examples within the reference itself. For more information
on the reference connector, check out
[README in the reference directory](https://github.com/hasura/graphql-engine/tree/master/dc-agents/reference).

View File

@ -0,0 +1,5 @@
{
"label": "Data Connectors",
"position": 1.6
}

View File

@ -0,0 +1,118 @@
---
description: Information about adding Hasura Data Connectors
sidebar_label: Add Data Connectors
keywords:
- hasura
- docs
- databases
- data connectors
- add
sidebar_position: 2
---
import Thumbnail from '@site/src/components/Thumbnail';
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
# Add a Hasura GraphQL Data Connector Agent to Metadata
To use a custom Hasura GraphQL Data Connector Agent, follow the following steps in the Hasura Console to add it to the
Hasura GraphQL Engine metadata. Before following the steps, make sure the custom connector is deployed and accessible.
<Tabs groupId="user-preference" className="api-tabs">
<TabItem value="console" label="Console">
<ol>
<li>
Navigate to the Data tab in the project's Console.
<Thumbnail src="/img/databases/data-connector/data-tab.png" alt="Hasura GraphQL Data Connector Agent diagram" />
</li>
<li>
Click on the `Manage` button.
<Thumbnail
src="/img/databases/data-connector/manage-db.png"
alt="Hasura GraphQL Data Connector Agent diagram"
/>
</li>
<li>
Click on the `Data Connector Agents` button.
<Thumbnail
src="/img/databases/data-connector/connect-agent.png"
alt="Hasura GraphQL Data Connector Agent diagram"
/>
</li>
<li>
Click on the `Add Agent` button.
<Thumbnail
src="/img/databases/data-connector/add-agent.png"
alt="Hasura GraphQL Data Connector Agent diagram"
/>
</li>
<li>
Enter the values for name and Agent endpoint. Click `Connect` and you're done!
<Thumbnail
src="/img/databases/data-connector/connect-final.png"
alt="Hasura GraphQL Data Connector Agent diagram"
/>
</li>
<li>
Once the above is done, the new driver supported by the custom connector will be available as one of the options
in the `Connect Database` page.
<Thumbnail
src="/img/databases/data-connector/updated-driver-list.png"
alt="Hasura GraphQL Data Connector Agent diagram"
/>
</li>
</ol>
</TabItem>
<TabItem value="cli" label="CLI">
You can add a Data Connector Agent by adding its config to the `/metadata/backend_configs.yaml` file:
```yaml
dataconnector:
sqlite:
uri: <data-connector-agent-url>
```
Apply the Metadata by running:
```yaml
hasura metadata apply
```
</TabItem>
<TabItem value="api" label="API">
You can add a Data Connector Agent to Hasura via the `dc_add_agent` Metadata API.
```http
POST /v1/metadata HTTP/1.1
Content-Type: application/json
X-Hasura-Role: admin
{
"type": "dc_add_agent",
"args": {
"name": "sqlite",
"url": "<url-where-data-connector-agent-is-deployed>"
}
}
```
</TabItem>
</Tabs>

View File

@ -0,0 +1,45 @@
---
description: Information about using the Hasura Data Connectors SDK
sidebar_label: Data Connector SDK
keywords:
- hasura
- docs
- databases
- data connectors
- sdk
sidebar_position: 3
---
# Hasura GraphQL Data Connector SDK
The Data Connector SDK serves as a pack of documentation and resources for understanding, building, and testing Data
Connector Agent implementations to ensure that they are complete, correct, idiomatic and can be developed rapidly and
with confidence. The workflow that the SDK supports out of the box is powered by Docker Compose in order to reduce the
number of dependencies required, but each component may be run natively if desired. The SDK is versioned with the Hasura
GraphQL Engine.
The SDK including the `docker-compose.yaml` file can be
[found here](https://github.com/hasura/graphql-engine/tree/master/dc-agents/sdk).
For more details on the SDK, check out the
[SDK repository README.md](https://github.com/hasura/graphql-engine/blob/master/dc-agents/sdk/README.md).
## Building a new Data Connector with the SDK
To create a new Data Connector, follow these steps:
1. [Start the container](https://github.com/hasura/graphql-engine/blob/master/dc-agents/sdk/docker-compose.yaml) with
`docker compose up`.
2. Verify that the tests pass.
3. Review and make changes to the reference connector for your specific database and intended implementation.
4. Or rebuild as required, depending on your stack.
5. Rerun the tests with `docker compose run tests`.
6. Interact with the Agent via Hasura GraphQL Engine at `http://localhost:8080` and view the OpenAPI Schema at
`http://localhost:8300`.
## Reference Connector
The reference connector is located under the `/reference` path within the SDK and serves as a working connector example.
It is written in TypeScript and has several key code snippet examples within the reference itself. For more information
on the reference connector, check out
[README in the reference directory](https://github.com/hasura/graphql-engine/tree/master/dc-agents/reference).

View File

@ -0,0 +1,59 @@
---
description: Information about Hasura Data Connectors
sidebar_label: Data Connectors
keywords:
- hasura
- docs
- databases
- data connectors
sidebar_position: 1
---
import Thumbnail from '@site/src/components/Thumbnail';
# Hasura Data Connectors
## Introduction
Hasura Data Connectors provide an easy way to build connections to any data source and instantly obtain GraphQL APIs on
that data.
Currently, Hasura natively supports Postgres, MS SQL Server, and BigQuery databases. Data Connectors allow you to
connect Hasura to **_any_** other data source. Hasura has built Data Connectors for MySQL, Oracle, Snowflake, Amazon
Athena, MariaDB, MongoDB (coming soon), with more sources in the pipeline, but you can also use them to connect to
your data sources. Think Microsoft Excel, SQLite, CSV, AirTable and more.
For more information on databases, check out the [Hasura Databases documentation](/docs/databases/overview.mdx) or to
jump right into integrating a native database, [check out the Quickstart](/docs/databases/quickstart.mdx).
This documentation will guide you through understanding Hasura Data Connectors concepts and how to use them.
:::warning Active Development
Data Connectors are currently in active development and are likely to change considerably. We are working hard to make
them as stable as possible soon, but please be aware that breaking changes may occur.
:::
## Hasura GraphQL Data Connector Agent
The Hasura GraphQL Data Connector Agent is a service that acts as an intermediary middleware abstraction between a data
source and the Hasura GraphQL Engine via a REST API. It allows developers to implement Hasura's powerful GraphQL
experience on any data source they use. Further information about the design and implementation of the Agent service
can be found in the
[README.md of the Data Connector Agent repository](https://github.com/hasura/graphql-engine/blob/master/dc-agents/README.md).
<Thumbnail
src="/img/databases/data-connector/data-connectors-agent-diagram.png"
alt="Hasura GraphQL Data Connector Agent diagram"
/>
In addition, an Agent can directly support new functionality without any other database upstream. The purpose of Data
Connector Agents is to quickly and easily allow developers to author new Agents to support a wide variety of new data
sources and use-cases.
## More with Data Connectors
- [Add a Data Connector to Hasura](/databases/data-connectors/adding-data-connectors.mdx)
- [Build a Data Connector for Hasura](/databases/data-connectors/data-connector-sdk.mdx)
- [Deploy custom code to Hasura Cloud with Jupyter Notebooks](/databases/data-connectors/jupyter-notebooks.mdx)

View File

@ -0,0 +1,222 @@
---
description: Information about Hasura Data Connectors using Jupyter Notebooks
sidebar_label: Jupyter Notebooks
keywords:
- hasura
- docs
- databases
- data connectors
- jupyter
- python
sidebar_position: 4
---
import Thumbnail from '@site/src/components/Thumbnail';
# Jupyter Python Notebook & API Server
To enable quick prototyping and experimentation with AI apps, we've built a tool where you can write Python code and
expose it as an API, which can be used with Hasura Event Triggers and Actions. This tool includes a Jupyter Notebook
where Python code can be written and executed. It also comes with a Jupyter Kernel Gateway, which can be then used
to start an API out of the functions defined in the notebook. You can use this framework to prototype your AI
applications while simultaneously using Hasura Connectors to bring together data from any data source easily and
securely.
<Thumbnail
src="/img/databases/data-connector/jupyter-arch.png"
alt="Jupyter Notebook"
/>
## Prerequisites
- Install the Hasura CLI: instructions can be found [here](/hasura-cli/install-hasura-cli.mdx).
- Install the `cloud` and `notebook` plugins using the following command:
```bash
hasura plugins install cloud
hasura plugins install notebook
```
To proceed, you'll need to ensure you're logged into Hasura Cloud via the CLI:
```bash
hasura cloud login
```
## Getting started
### Step 1: Create a notebook
Once you login to Hasura Cloud, you can create a Jupyter notebook to use the tool. Run the following command by
supplying any random password which you will later use to access the notebook and the API:
```bash
hasura notebook create -p <password>
```
This takes a few minutes, you can check the status using the following command:
```bash
hasura notebook status
```
When the notebook is deployed, the CLI will display the access URL. Visit the URL in a browser to see the homepage.
You will be prompted to enter a username and password. The username is `hasura` and password is the one you set while
creating the notebook:
<Thumbnail
src="/img/databases/data-connector/jupyter-start.png"
alt="Jupyter Notebook homepage"
/>
### Step 2: Launch the notebook
Click the "Launch Notebook" button to launch the Jupyter Notebook. You should see a screen like this:
<Thumbnail
src="/img/databases/data-connector/jupyter-contents.png"
alt="Contents of the notebook"
/>
In the notebook, you'll find a few samples. You will find the handler code in `server.py`. This handler is responsible
for processing requests and serving responses from the API.
Once you have a block of code in your notebook, you can expose that as an API through the Jupyter Kernel Gateway by
adding a comment of the format `# METHOD /url`. For example:
```python
# GET /hello_world
import json
print(json.dumps({
'hello': 'world'
}))
```
### Step 3: Start the API
On the homepage, click the "Start API" button. This block of code will return `{"hello": "world"}` when you visit
`/invoke/hello_world`. More details on how to handle headers and response data can be found at
[Jupyter Kernel Gateway notebook-http mode documentation](https://jupyter-kernel-gateway.readthedocs.io/en/latest/http-mode.html).
:::info Restart the API after making changes
Each time changes are made to the notebook, click the "Restart API" button to deploy the latest version.
:::
## Templates
You can find templates for
[events and action handlers](https://github.com/hasura/jupyter-code-api-server/blob/basic-auth/notebook/server.ipynb) in
this notebook:
### Template for Event Trigger to Extract, Transform, Load (ETL) data
```python
# POST /handle_event
GRAPHQL_ENDPOINT = ""
ADMIN_SECRET = ""
import json
from gql import gql, Client
from gql.transport.requests import RequestsHTTPTransport
def handle_insert(row, client):
id = int(row['id'])
name = str(row['name'])
description = str(row['description'])
# In reality you would follow the URL from row['url']
content = "dummy content"
gql_query = gql("""
mutation insertItem($id: int!, $name: text!, $description: text!) {
insert_Product_one(object: { product_id: $id, name: $name, description: $description }) {
id
}
}
""")
print(client.execute(gql_query, variable_values={
'id': id, 'name': name, 'description': description}))
```
### Template to respond to user's questions by querying Weaviate + OpenAI
```python
# POST /handle_query
GRAPHQL_ENDPOINT = ""
ADMIN_SECRET = ""
OPENAI_API_KEY = ""
import json
from gql import gql, Client
from gql.transport.requests import RequestsHTTPTransport
from langchain.llms import OpenAI
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
def handle_query(request):
user_query = request['body']['input']['user_query']
gql_headers = dict()
gql_headers['x-hasura-admin-secret'] = ADMIN_SECRET
# Create a GraphQL client with the request transport
transport = RequestsHTTPTransport(
url=GRAPHQL_ENDPOINT, headers=gql_headers)
client = Client(transport=transport)
```
## Authentication
The APIs exposed by the notebook require Basic HTTP authentication. When added as an Event Trigger or Action to
Hasura, you'll need to add the Authorization header in basic auth format with user name as `hasura` and password as
the password you set for the notebook.
The header format is:
```bash
Authorization: Basic <token>
```
The token can be created by base64-encoding a string in `username:password` format in your terminal:
```bash
echo -ne "<username>:<password>" | base64
```
You can then use the encoded string returned by this command as your token in the authorization header.
## Exposed APIs
The container exposes the following APIs:
| API | Description |
| ------------------ | ------------------------------------------------------ |
| `/jupyter` | Jupyter notebook entrypoint. |
| `/invoke/<path>` | Invoke APIs exposed via the notebook's `server.ipynb`. |
| `/process/start` | Start the notebook API server. |
| `/process/stop` | Stop the notebook API server. |
| `/process/restart` | Restart the notebook API server. |
The source code for the container can be found [here](https://github.com/hasura/jupyter-code-api-server/tree/basic-auth).
:::info Production environments
**It is not recommended to use this notebook in production. It is intended to be a playground for experimenting and fast
iterations to validate your ideas.**
In production, we recommend that you migrate your code from a notebook and into a Flask application or equivalent,
which you can then deploy in a production-ready environment.
:::
## Deleting the notebook
Once you are done, you can keep the notebook around for your next project or delete it using the command:
```bash
hasura notebook delete
```

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 53 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 57 KiB