How to read parquet files from Azure Blobs into Pandas DataFrame with columns projection on server-side?

huangapple go评论62阅读模式
英文:

How to read parquet files from Azure Blobs into Pandas DataFrame with columns projection on server-side?

问题

Is it possible to perform a column projection on the parquet file at server level before downloading it to be more efficient? I.e. I would like to filter only desidered columns before downloading the file.

At the moment I am connecting to Azure services only by a connection string if that helps and using the Python Client library.

英文:

Following this question: https://stackoverflow.com/questions/63351478/how-to-read-parquet-files-from-azure-blobs-into-pandas-dataframe

Is it possible to perform a column projection on the parquet file at server level before downloading it to be more efficient? I.e. I would like to filter only desidered columns before downloading the file.

At the moment I am connecting to Azure services only by a connection string if that helps and using the Python Client library.

答案1

得分: 1

在下载 Azure Blob 存储中的 Parquet 文件之前,是否可以在服务器级别执行列投影以提高效率?即,我想在下载文件之前仅筛选所需的列。

要从 Azure Blob 存储中的 Parquet 文件下载所需的列,您可以使用以下 Python 代码:

代码:

import pyarrow.parquet as pq
from azure.storage.blob import BlobServiceClient
import pandas as pd 

# 设置 BlobServiceClient
blob_service_client = BlobServiceClient.from_connection_string('your connection string')

# 获取对 Azure Blob 存储中 Parquet 文件的引用
blob_container_client = blob_service_client.get_container_client('test1')
blob_client = blob_container_client.get_blob_client('samplepar.parquet')

# 定义要从 Parquet 文件中读取的列列表
columns = ['title', 'salary', 'birthdate', 'id1', 'id2']
columns_query = ", ".join([f"[{column}]" for column in columns])
query = f"SELECT {columns_query} FROM BlobStorage"
with open("sample.parquet1", "wb") as file:
    blob_client.download_blob().download_to_stream(file)

table = pq.read_table("sample1.parquet")
available_columns = [column for column in columns if column in table.column_names]
print(available_columns)
if available_columns:
    table = table.select(available_columns)
    df = table.to_pandas()
    print(df)
else:
    print("错误:Parquet 文件中没有指定的列。")

**输出**

['title', 'salary', 'birthdate']
title salary birthdate
0 Internal Auditor 49756.53 3/8/1971
1 Accountant IV 150280.17 1/16/1968
2 Structural Engineer 144972.51 2/1/1960
3 Senior Cost Accountant 90263.05 4/8/1997


**下载的文件:**

[![enter image description here][1]][1]

[![enter image description here][2]][2]

  [1]: https://i.stack.imgur.com/Mhny3.png
  [2]: https://i.stack.imgur.com/NsHW2.png

请注意,上述代码首先从 Azure Blob 存储中下载 Parquet 文件,然后根据您指定的列对文件进行列选择,并将结果转换为 Pandas DataFrame。如果指定的列不在 Parquet 文件中,则会显示错误消息。

英文:

> Is it possible to perform a column projection on the parquet file at server level before downloading it to be more efficient? I.e. I would like to filter only desired columns before downloading the file.

To download the desired column from the parquet file in Azure blob storage, you can use the below Python code:

Code:

import pyarrow.parquet as pq
from azure.storage.blob import BlobServiceClient
import pandas as pd 

# Set up the BlobServiceClient
blob_service_client = BlobServiceClient.from_connection_string('your connection string')

# Get a reference to the Parquet file in Azure Blob Storage
blob_container_client = blob_service_client.get_container_client('test1')
blob_client = blob_container_client.get_blob_client('samplepar.parquet')

# Define the list of columns to read from the Parquet file
columns = ['title', 'salary', 'birthdate', 'id1', 'id2']
columns_query = ", ".join([f"[{column}]" for column in columns])
query = f"SELECT {columns_query} FROM BlobStorage"
with open("sample.parquet1", "wb") as file:
    blob_client.download_blob().download_to_stream(file)

table = pq.read_table("sample1.parquet")
available_columns = [column for column in columns if column in table.column_names]
print(available_columns)
if available_columns:
    table = table.select(available_columns)
    df = table.to_pandas()
    print(df)
else:
    print("Error: None of the specified columns are present in the Parquet file.")

Output:

['title', 'salary', 'birthdate']
                      title     salary  birthdate
0          Internal Auditor   49756.53   3/8/1971
1             Accountant IV  150280.17  1/16/1968
2       Structural Engineer  144972.51   2/1/1960
3    Senior Cost Accountant   90263.05   4/8/1997

How to read parquet files from Azure Blobs into Pandas DataFrame with columns projection on server-side?

Downloaded File:

How to read parquet files from Azure Blobs into Pandas DataFrame with columns projection on server-side?

huangapple
  • 本文由 发表于 2023年6月30日 00:14:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76582862.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定