Jupyter Notebook
With the SQLAlchemy library in Python or
Tutorial-1: Integrate with Jupyter Notebook using SQLAlchemy
In this tutorial, you will first deploy a local Databend instance and Jupyter Notebook, and then run a sample notebook to connect to your local Databend through the SQLAlchemy library, as well as write and visualize data within the notebook.
Before you start, make sure you have completed the following tasks:
- You have installed on your system.Open in the new tab
- Download the sample notebook to a local folder.Open in the new tab
Step 1. Deploy Databend
- Follow the to deploy a local Databend.Open in the new tab
- Create a SQL user in Databend. You will use this account to connect to Databend in Jupyter Notebook.
CREATE USER user1 IDENTIFIED BY 'abc123';
GRANT ALL ON *.* TO user1;
Step 2. Deploy Jupyter Notebook
- Install Jupyter Notebook with pip:
pip install notebook
- Install dependencies with pip:
pip install sqlalchemy
pip install pandas
pip install pymysql
Step 3. Run Sample Notebook
- Run the command below to start Jupyter Notebook:
jupyter notebook
This will start up Jupyter and your default browser should start (or open a new tab) to the following URL: http://localhost:8888/tree
On the Files tab, navigate to the sample notebook you downloaded and open it.
In the sample notebook, run the cells sequentially. By doing so, you create a table containing 5 rows in your local Databend, and visualize the data with a bar chart.
Tutorial-2: Integrate with Jupyter Notebook using ipython-sql
In this tutorial, you will first deploy a local Databend instance and Jupyter Notebook, and then run a sample notebook to connect to your local Databend through
Before you start, ensure that you have
Step 1. Deploy Databend
- Follow the to deploy a local Databend.Open in the new tab
- Create a SQL user in Databend. You will use this account to connect to Databend in Jupyter Notebook.
CREATE USER user1 IDENTIFIED BY 'abc123';
GRANT ALL ON *.* TO user1;
Step 2. Deploy Jupyter Notebook
- Install Jupyter Notebook with pip:
pip install notebook
- Install dependencies with pip:
To proceed with this tutorial, you'll need a version of SQLAlchemy that is below 2.0. Please be aware that in SQLAlchemy 2.0 and later versions, the result.DataFrame() method has been deprecated and is no longer available. Instead, you can use the pandas library to directly create a DataFrame from query results and perform plotting.
pip install ipython-sql databend-sqlalchemy
pip install sqlalchemy
Step 3. Create and Connect a Notebook to Databend
- Run the command below to start Jupyter Notebook:
jupyter notebook
This will start up Jupyter and your default browser should start (or open a new tab) to the following URL: http://localhost:8888/tree
Select New > Python 3 to create a notebook.
Run the following code sequentially in separate cells. By doing so, you create a table containing 5 rows in your local Databend, and visualize the data with a bar chart.
%load_ext sql
%%sql databend://user1:abc123@localhost:8000/default
create table if not exists user(created_at Date, count Int32);
insert into user values('2022-04-01', 5);
insert into user values('2022-04-01', 3);
insert into user values('2022-04-03', 4);
insert into user values('2022-04-03', 1);
insert into user values('2022-04-04', 10);
result = %sql select created_at as date, count(*) as count from user group by created_at;
result
%matplotlib inline
df = result.DataFrame()
df.plot.bar(x='date', y='count')
You can now see a bar chart on the notebook:
Tutorial-3: Integrate with Jupyter Notebook with Python Binding Library
In this tutorial, you will first deploy a local Databend instance and Jupyter Notebook, and then run queries in a notebook through the
Before you start, ensure that you have
Step 1. Deploy Jupyter Notebook
- Install Jupyter Notebook with pip:
pip install notebook
- Install dependencies with pip:
pip install databend
pip install matplotlib
Step 2. Create a Notebook
- Run the command below to start Jupyter Notebook:
jupyter notebook
This will start up Jupyter and your default browser should start (or open a new tab) to the following URL: http://localhost:8888/tree
Select New > Python 3 to create a notebook.
Run the following code sequentially in separate cells:
# Import the necessary libraries
from databend import SessionContext
# Create a DataBend session
ctx = SessionContext()
# Create a table in DataBend
ctx.sql("CREATE TABLE IF NOT EXISTS user (created_at Date, count Int32)")
# Insert multiple rows of data into the table
ctx.sql("INSERT INTO user VALUES ('2022-04-01', 5), ('2022-04-01', 3), ('2022-04-03', 4), ('2022-04-03', 1), ('2022-04-04', 10)")
# Execute a query
result = ctx.sql("SELECT created_at as date, count(*) as count FROM user GROUP BY created_at")
# Display the query result
result.show()
# Import libraries for data visualization
import matplotlib.pyplot as plt
# Convert the query result to a Pandas DataFrame
df = result.to_pandas()
# Create a bar chart to visualize the data
df.plot.bar(x='date', y='count')
plt.show()
You can now see a bar chart on the notebook: