<@U0A71G31CDV> hi rocky, can you show me some best...
# questions-and-troubleshooting
r
@Rocky hi rocky, can you show me some best practice code(python with jupyterlab) for run&learn starrocks
r
Hi there! I'm Rocky. It's great to help you get started with StarRocks using Python and JupyterLab. For a "Run & Learn" experience, I recommend two main approaches: 1. JupySQL: Best for interactive exploration using SQL directly in notebook cells. 2. Arrow Flight SQL: Best for high-performance data science workflows (moving data into Pandas/Polars extremely fast). 1. Setup & Installation Run this in your first Jupyter cell to install the necessary drivers:
Copy code
python
# Install JupySQL for SQL magics, PyMySQL for the connection, 
# and ADBC for high-performance Arrow Flight SQL.
%pip install --quiet jupysql pymysql sqlalchemy adbc-driver-flightsql pandas
────────── 2. Method 1: Interactive SQL (JupySQL) This is the most "Jupyter-native" way to learn. It allows you to write SQL as if you were in a console but keeps the results in your notebook.
Copy code
python
import pandas as pd
%load_ext sql

# Replace with your StarRocks FE host and port (default query port is 9030)
# Format: mysql+pymysql://<user>:<password>@<host>:<port>/<database>
%sql <mysql+pymysql://root:@127.0.0.1:9030>

# Create a demo database and table
%%sql
CREATE DATABASE IF NOT EXISTS rocky_db;
USE rocky_db;

CREATE TABLE IF NOT EXISTS sample_data (
    user_id INT,
    event_time DATETIME,
    click_count INT
) 
DUPLICATE KEY(user_id)
DISTRIBUTED BY HASH(user_id) BUCKETS 4;

INSERT INTO sample_data VALUES 
(1, '2023-10-01 10:00:00', 5),
(2, '2023-10-01 10:05:00', 12),
(3, '2023-10-01 10:10:00', 8);

# Query and visualize immediately
result = %sql SELECT * FROM sample_data
df = result.DataFrame()
df.plot(kind='bar', x='user_id', y='click_count', title='User Activity')
────────── 3. Method 2: High-Performance Loading (Arrow Flight SQL) When you're ready to move large datasets (millions of rows) into Python for machine learning or analysis, use Arrow Flight SQL. It is significantly faster than the standard MySQL protocol.
Copy code
python
import adbc_driver_flightsql.dbapi as flight_sql

# StarRocks Flight SQL port is usually 9408
uri = "<grpc://127.0.0.1:9408>"
conn_info = {
    "username": "root",
    "password": "",
}

with flight_sql.connect(uri=uri, db_kwargs=conn_info) as conn:
    with conn.cursor() as cur:
        # Fetching 1 million rows is incredibly fast with this method
        cur.execute("SELECT * FROM rocky_db.sample_data")
        
        # Directly to a Pandas DataFrame
        df = cur.fetch_df()
        
print(df.head())
────────── Rocky's Best Practices for Learning: 1. Model Matters: When creating tables, experiment with different table types: * `DUPLICATE KEY`: Best for raw logs. * `PRIMARY KEY`: Best for real-time updates/upserts. * `AGGREGATE KEY`: Best for pre-aggregating data. 2. Distribution: Always define a
DISTRIBUTED BY HASH(col)
key. Choosing a high-cardinality column (like
user_id
or
order_id
) ensures your data is spread evenly across the cluster. 3. Explain Plan: If you want to see how StarRocks optimizes your query, run
EXPLAIN SELECT ...
in a
%sql
cell. It’s a great way to learn about the cost-based optimizer (CBO). Enjoy your StarRocks journey! If you need more specific examples for Stream Load or Materialized Views, just ask! Referencesintegrations/IDE_integrations/jupysql.mdunloading/arrow_flight.mdunloading/Spark_connector.md