You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Hive表使用Python构建Dashboard的解决方案咨询

Hey there! I’ve built similar dashboards connecting Hive with Python before, so here’s a complete, actionable solution tailored to your requirement:

1. Pull Data from Hive to Python

First, you’ll need a library to connect to Hive — pyhive is the most straightforward choice for this. Let’s set it up:

  • Install required dependencies:

    pip install pyhive pandas
    
  • Code to fetch your Hive table data into a pandas DataFrame:

    from pyhive import hive
    import pandas as pd
    
    # Connect to your Hive cluster (adjust host/port/username to match your setup)
    conn = hive.Connection(host='your-hive-host', port=10000, username='your-username')
    
    # Query the target table (replace 'your_table_name' with your actual table name)
    query = """
    SELECT entity, count, date
    FROM your_table_name
    """
    df = pd.read_sql(query, conn)
    
    # Close the connection once done
    conn.close()
    
    # Verify the data
    print(df.head())
    

2. Preprocess the Data

Your date format (25-feb-2018) needs conversion to a proper datetime type for smooth visualization. We’ll also pivot the data to fit your entity-X/date-Y layout:

# Convert date column to datetime format
df['date'] = pd.to_datetime(df['date'], format='%d-%b-%Y')

# Pivot data: rows = dates, columns = entities, values = count (fill missing values with 0)
pivot_df = df.pivot(index='date', columns='entity', values='count').fillna(0)

3. Build the Interactive Dashboard with Plotly Dash

Dash is perfect for building web-based interactive dashboards with minimal code. We’ll create a heatmap (ideal for your x=entity, y=date requirement) plus a date filter for flexibility:

First install Dash:

pip install dash plotly

Full dashboard code:

import dash
from dash import dcc, html, Input, Output
import plotly.express as px

# Initialize the Dash app
app = dash.Dash(__name__)

# Define app layout
app.layout = html.Div([
    html.H1("Entity Daily Count Dashboard"),
    # Date range picker for filtering data
    dcc.DatePickerRange(
        id='date-range',
        min_date_allowed=df['date'].min(),
        max_date_allowed=df['date'].max(),
        start_date=df['date'].min(),
        end_date=df['date'].max()
    ),
    # Heatmap visualization
    dcc.Graph(id='entity-date-heatmap')
])

# Callback to update heatmap based on selected date range
@app.callback(
    Output('entity-date-heatmap', 'figure'),
    Input('date-range', 'start_date'),
    Input('date-range', 'end_date')
)
def update_heatmap(start_date, end_date):
    filtered_df = df[(df['date'] >= start_date) & (df['date'] <= end_date)]
    pivot_filtered = filtered_df.pivot(index='date', columns='entity', values='count').fillna(0)
    
    # Create heatmap with entity as X-axis, date as Y-axis
    fig = px.imshow(
        pivot_filtered,
        x=pivot_filtered.columns,
        y=pivot_filtered.index.strftime('%d-%b-%Y'),
        labels=dict(x="Entity", y="Date", color="Count"),
        title="Daily Count per Entity",
        color_continuous_scale='Blues'
    )
    fig.update_layout(xaxis_title="Entity", yaxis_title="Date")
    return fig

# Run the app
if __name__ == '__main__':
    app.run_server(debug=True)

4. Run and Access the Dashboard

Execute the script, then open http://localhost:8050 in your browser. You’ll see:

  • An interactive heatmap where X-axis = entities, Y-axis = dates, and color intensity represents the count value
  • A date range picker to filter specific time periods

Alternative: Lightweight Setup with Streamlit

If you prefer a simpler, callback-free option, Streamlit works great too. Here’s a quick snippet:

pip install streamlit
import streamlit as st
import plotly.express as px
import pandas as pd

st.title("Entity Daily Count Dashboard")

# Date filter widgets
start_date = st.date_input("Start Date", df['date'].min())
end_date = st.date_input("End Date", df['date'].max())

# Filter data based on selection
filtered_df = df[(df['date'] >= pd.to_datetime(start_date)) & (df['date'] <= pd.to_datetime(end_date))]
pivot_filtered = filtered_df.pivot(index='date', columns='entity', values='count').fillna(0)

# Render heatmap
fig = px.imshow(
    pivot_filtered,
    x=pivot_filtered.columns,
    y=pivot_filtered.index.strftime('%d-%b-%Y'),
    labels=dict(x="Entity", y="Date", color="Count"),
    title="Daily Count per Entity",
    color_continuous_scale='Blues'
)
st.plotly_chart(fig)

Run it with streamlit run your_script.py and access via the local URL provided in the terminal.


内容的提问来源于stack exchange,提问作者sandeep g v

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:06:31