File size: 988 Bytes
5da5531 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | import duckdb
from huggingface_hub import HfFileSystem
def setup_duckdb_hf_connection():
"""
Registers the Hugging Face filesystem with DuckDB to enable
'hf://' URI support for querying data.
"""
try:
hf_fs = HfFileSystem()
duckdb.register_filesystem(hf_fs)
print("Successfully registered HfFileSystem with DuckDB.")
return True
except Exception as e:
print(f"Failed to register HfFileSystem: {e}")
return False
def query_hf_data(query: str):
"""
Executes a SQL query against Hugging Face data using DuckDB.
"""
setup_duckdb_hf_connection()
try:
result = duckdb.sql(query).df()
return result
except Exception as e:
print(f"Error executing query: {e}")
return None
if __name__ == "__main__":
# Example usage:
# query = "SELECT * FROM 'hf://datasets/Brettapps/my-bucket/data.parquet' LIMIT 10"
# df = query_hf_data(query)
# print(df)
pass
|