A Python REST client for interacting with Hadoop Hue's REST API
Project description
HueClientRest
A Python REST client for interacting with Hadoop Hue's REST API. This client allows you to execute SQL queries, manage files, and download results through Hue's web interface programmatically. With support of Quadient
Features
- Authentication: JWT token-based authentication
- Query Execution: Execute SQL queries with various dialects (Hive, Spark SQL, etc.)
- Result Management: Fetch query results with pagination support
- File Operations: List, download, and manage files in remote storage
- CSV Export: Save query results directly to CSV files
- Batch Operations: Execute queries and download resulting files in one operation
Installation
pip install hueclientrest
Quick Start
from hueclientrest import HueClientREST
# Initialize the client
client = HueClientREST(
host="https://your-hue-server.com",
username="your_username",
password="your_password",
verify_ssl=True
)
# Execute a query and save results to CSV
client.run(
statement="SELECT * FROM your_table LIMIT 100",
dialect="hive",
filename="results.csv"
)
Usage Examples
Basic Query Execution
from hueclientrest import HueClientREST
client = HueClientREST(
host="https://your-hue-server.com",
username="your_username",
password="your_password"
)
# Simple query execution with CSV output
client.run(
statement="SELECT count(*) FROM sales WHERE date >= '2023-01-01'",
dialect="hive",
filename="sales_count.csv"
)
Advanced Query Execution
# Step-by-step execution for more control
client.login()
# Execute query
operation_id = client.execute(
statement="SELECT * FROM large_table WHERE condition = 'value'",
dialect="spark"
)
# Wait for completion
client.wait(operation_id, poll_interval=5, timeout=600)
# Fetch results with custom batch size
headers, rows = client.fetch_all(operation_id, batch_size=5000)
# Save to CSV
client.save_csv(headers, rows, "large_results.csv")
File Operations
# List files in a directory
files = client.list_directory("/user/data/exports")
for file_info in files:
print(f"Name: {file_info['name']}, Size: {file_info.get('size', 'N/A')}")
# Download a specific file
client.download_file(
file_path="/user/data/exports/report.csv",
local_filename="./downloads/report.csv"
)
# Download all files from a directory
downloaded_files = client.download_directory_files(
directory_path="/user/data/exports",
local_dir="./downloads",
file_pattern="part-" # Only download files containing "part-"
)
# Upload file
response = client.upload('/user/uploads', '.import.csv')
Export Query Results to Files
# Execute INSERT OVERWRITE DIRECTORY and download results
statement = """
INSERT OVERWRITE DIRECTORY '/user/exports/sales_2023'
STORED AS TEXTFILE
SELECT * FROM sales WHERE year = 2023
"""
downloaded_files = client.run_and_download(
statement=statement,
directory_path="/user/exports/sales_2023",
local_dir="./sales_data",
dialect="hive",
file_pattern="part-",
timeout=900 # 15 minutes timeout
)
print(f"Downloaded {len(downloaded_files)} files")
Working with Different SQL Dialects
# Hive query
client.run(
statement="SHOW TABLES",
dialect="hive",
filename="hive_tables.csv"
)
# Spark SQL query
client.run(
statement="SELECT spark_version()",
dialect="sparksql",
filename="spark_version.csv"
)
# Impala query
client.run(
statement="SELECT version()",
dialect="impala",
filename="impala_version.csv"
)
SSL Configuration
# Disable SSL verification (not recommended for production)
client = HueClientREST(
host="https://your-hue-server.com",
username="your_username",
password="your_password",
verify_ssl=False,
ssl_warnings=False # Suppress SSL warnings
)
# Custom SSL verification
client = HueClientREST(
host="https://your-hue-server.com",
username="your_username",
password="your_password",
verify_ssl=True # Use system CA bundle
)
Error Handling
from hueclientrest import HueClientREST
client = HueClientREST(
host="https://your-hue-server.com",
username="your_username",
password="your_password"
)
try:
client.run(
statement="SELECT * FROM non_existent_table",
dialect="hive",
filename="results.csv"
)
except RuntimeError as e:
print(f"Authentication or execution error: {e}")
except TimeoutError as e:
print(f"Query timed out: {e}")
except ValueError as e:
print(f"Invalid response format: {e}")
except Exception as e:
print(f"Unexpected error: {e}")
Batch Processing
queries = [
("SELECT count(*) FROM table1", "table1_count.csv"),
("SELECT count(*) FROM table2", "table2_count.csv"),
("SELECT count(*) FROM table3", "table3_count.csv"),
]
client = HueClientREST(
host="https://your-hue-server.com",
username="your_username",
password="your_password"
)
# Login once for all queries
client.login()
for query, filename in queries:
try:
operation_id = client.execute(query, "hive")
client.wait(operation_id)
headers, rows = client.fetch_all(operation_id)
client.save_csv(headers, rows, filename)
print(f"Completed: {filename}")
except Exception as e:
print(f"Failed {filename}: {e}")
Unit tests
python -m unittest
API Reference
HueClientREST
Main client class for interacting with Hue REST API.
Constructor Parameters
host(str): Hue server URLusername(str): Username for authenticationpassword(str): Password for authenticationverify_ssl(bool): Whether to verify SSL certificates (default: True)ssl_warnings(bool): Whether to show SSL warnings (default: False)
Methods
login(): Authenticate and obtain JWT tokenexecute(statement, dialect): Execute SQL statementwait(operation_id, poll_interval, timeout): Wait for operation completionfetch_all(operation_id, batch_size): Fetch all query resultssave_csv(headers, rows, filename): Save results to CSV filerun(statement, dialect, filename, batch_size): Execute query and save to CSVlist_directory(directory_path, pagesize): List directory contentsdownload_file(file_path, local_filename): Download single filedownload_directory_files(directory_path, local_dir, file_pattern): Download multiple filesrun_and_download(statement, directory_path, local_dir, ...): Execute and download resultscheck_directory_exists(directory_path): Check if directory existsupload_file(dest_path, file_path): Upload a file to a directory
Requirements
- Python 3.7+
- requests
- urllib3
License
This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
Support
For issues and questions, please create an issue in the GitHub repository.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file hueclientrest-0.2.1.tar.gz.
File metadata
- Download URL: hueclientrest-0.2.1.tar.gz
- Upload date:
- Size: 25.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9bd6791d3e040b590c1fdba7afb6d8d34b051967b27c700995065d7fde27c3ab
|
|
| MD5 |
14ac83dbe481dfbf8b0128433992591d
|
|
| BLAKE2b-256 |
431dedafb223ca4df0920594933ca4d5ff8a3b100a826a70337ffcad52053a24
|
File details
Details for the file hueclientrest-0.2.1-py3-none-any.whl.
File metadata
- Download URL: hueclientrest-0.2.1-py3-none-any.whl
- Upload date:
- Size: 21.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
129ddc8388636028f2a034b0e11a9ace41ef0bce73561ffc9231c5b3d8f4ff4b
|
|
| MD5 |
b6f0605dc3f7898fee5907b3e414764c
|
|
| BLAKE2b-256 |
bd24c0fe210039427a7ba3ae9f1d875b3f0c26452f71abd5673ed9014348e89c
|