如何在Python中使用exasol数据库中的大表进行统计分析?

问题描述 投票:0回答:1

我有一个包含 3600 万行的表,我需要运行不同的统计分析(例如假设检验、分布分析等)。 由于当我使用 export_to_pandas 方法时出现内存错误,我需要以块方式读取数据,或者使用 dask 数据帧来读取数据,这是我的首选选项。 然而,经过几次尝试,我没有设法将表从 exasol 数据库导入到 dask 数据框中。

代码是什么样的?

即使是分块代码也不起作用:

import pyexasol

# Connect to Exasol
connection = pyexasol.connect(dsn="localhost:8563", user="your_user", password="your_password", schema="your_schema")

# Define your SQL query
sql_query = "SELECT * FROM your_table"

# Set the chunk size for fetching data
chunk_size = 1000

# Execute the query and fetch data in chunks
with connection.cursor() as cursor:
    cursor.execute(sql_query)
    
    while True:
        # Fetch data in chunks
        data_chunk = cursor.fetchall(chunk_size=chunk_size)
        
        # Break the loop if no more data is available
        if not data_chunk:
            break
        
        # Process the data (replace this with your actual data processing logic)
        for row in data_chunk:
            print(row)

# Close the connection
connection.close()
database statistics dask chunks exasol
1个回答
0
投票

在 Exasol 中处理大型数据集时,您可以使用 Dask 的

read_sql_table
函数来分段收集数据并将其加载到 Dask DataFrame 中以进行分布式和分块处理。首先,请确保您已安装 Dask 和 Exasol Python 库。如果您之前没有安装过,您可以通过 pip 安装它们:

pip install dask[complete]
pip install pyexasol

要将数据从 Exasol 读取到 Dask DataFrame 中,请使用以下代码:

import dask.dataframe as dd
import pyexasol

# Connect to Exasol
connection = pyexasol.connect(dsn="localhost:8563", user="your_user", password="your_password", schema="your_schema")

# Define your SQL query
sql_query = "SELECT * FROM your_table"

# Set the chunk size for fetching data
chunk_size = 1000

# Create a Dask DataFrame from Exasol
df = dd.read_sql_table(sql_query, connection, index_col="your_primary_key_column", divisions=chunk_size)

# You can perform various Dask operations on 'df' here

# If you want to compute and collect the results as a pandas DataFrame, you can do:
pandas_df = df.compute()

# Close the Exasol connection
connection.close()

pyexasol
用于连接 Exasol。定义 SQL 查询以从 Exasol 表中检索数据。设置变量
chunk_size
来控制一次检索多少行。为了防止内存困难,您可以将数据分段处理。要从 Exasol 数据构建 Dask DataFrame,请使用 Dask 中的
read_sql_table
函数。要对数据进行分区,请提供 Exasol 连接、SQL 查询和索引列(通常是主键)。
divisions
选项决定片段的分离。可以在
df
Dask DataFrame 上执行过滤、聚合和其他 Dask 操作。

© www.soinside.com 2019 - 2024. All rights reserved.