Python client for Rabbit BigQuery Job Optimizer API
Project description
Rabbit BigQuery Job Optimizer Python Client
This is the official Python client library for the Rabbit BigQuery Job Optimizer API.
Installation
pip install rabbit-bq-job-optimizer
Usage
from rabbit_bq_job_optimizer import RabbitBQJobOptimizer, OptimizationConfig
# Initialize the client
client = RabbitBQJobOptimizer(
api_key="your-api-key"
# base_url is optional - will use production endpoint by default
# timeout is optional - defaults to 5 seconds
)
# Optimize a BigQuery job configuration
job_config = {
"configuration": {
"query": {
"query": "SELECT * FROM my_table",
"useLegacySql": False,
"priority": "INTERACTIVE"
}
}
}
optimization_config = OptimizationConfig(
type="reservation_assignment",
config={
"defaultPricingMode": "on_demand",
"reservationIds": [
"my-project:US.my-reservation-us",
"my-project:EU.my-reservation-eu"
]
}
)
# Optimize the job
result = client.optimize_job(
configuration=job_config,
enabledOptimizations=[optimization_config]
)
# Access the optimized configuration
optimized_config = result.optimizedJob
# Access optimization results
for optimization in result.optimizationResults:
print(f"Type: {optimization.type}")
print(f"Applied: {optimization.performed}")
print(f"Estimated Savings: {optimization.estimatedSavings}")
Error Handling
The client raises exceptions for API errors:
from rabbit_bq_job_optimizer import RabbitBQJobOptimizerError
try:
result = client.optimize_job(job_config)
except RabbitBQJobOptimizerError as e:
print(f"Error: {e.message}")
print(f"Status Code: {e.status_code}")
Configuration
Authentication
The API key is required to use the client. It can be obtained from the Rabbit team by contacting success@followrabbit.ai. The base URL is optional and defaults to the production endpoint.
There are three ways to configure authentication:
- Pass API key directly to the constructor (uses default production endpoint):
from rabbit_bq_job_optimizer import RabbitBQJobOptimizer
client = RabbitBQJobOptimizer(
api_key="your-api-key"
)
- Pass both API key and custom base URL:
from rabbit_bq_job_optimizer import RabbitBQJobOptimizer
client = RabbitBQJobOptimizer(
api_key="your-api-key",
base_url="https://your-custom-endpoint.com"
)
- Set environment variables:
export RABBIT_API_KEY="your-api-key"
export RABBIT_API_BASE_URL="your-base-url" # Optional - will use default if not set
Then initialize the client without parameters:
from rabbit_bq_job_optimizer import RabbitBQJobOptimizer
client = RabbitBQJobOptimizer() # Will automatically use environment variables
The environment variables are:
RABBIT_API_KEY: Your Rabbit API key (required)RABBIT_API_BASE_URL: The base URL for the Rabbit API (optional, defaults to production endpoint)
Request Timeout
The client includes a configurable request timeout to prevent indefinite hangs. By default, requests will timeout after 5 seconds.
You can customize the timeout when initializing the client:
from rabbit_bq_job_optimizer import RabbitBQJobOptimizer
# Use default 5 second timeout
client = RabbitBQJobOptimizer(api_key="your-api-key")
# Use custom timeout (in seconds)
client = RabbitBQJobOptimizer(
api_key="your-api-key",
timeout=30 # 30 seconds
)
If the API doesn't respond within the specified timeout, a RabbitBQJobOptimizerError will be raised.
Development
To install the package in development mode:
pip install -e .
License
Apache License 2.0
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 rabbit_bq_job_optimizer-0.1.16.tar.gz.
File metadata
- Download URL: rabbit_bq_job_optimizer-0.1.16.tar.gz
- Upload date:
- Size: 8.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dd84f81b0b6f9d0e7dfb5cabc3faeda43c80b8b9e60c21f47beeff731a5471f4
|
|
| MD5 |
4c02f28437d8438236ed67e7851e2f1d
|
|
| BLAKE2b-256 |
c5ab44b7d48d4a07dc7ea4e06c98cb5908fd175b5189f9d2b133d2ecfd26a1fa
|
File details
Details for the file rabbit_bq_job_optimizer-0.1.16-py3-none-any.whl.
File metadata
- Download URL: rabbit_bq_job_optimizer-0.1.16-py3-none-any.whl
- Upload date:
- Size: 9.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
22e67f232d525b6ea2fcab49ded8b92b1af9a7cd34e027ef28ff2bb64456ab49
|
|
| MD5 |
6557a67d4fd00511c5bf9f3ae54e44b8
|
|
| BLAKE2b-256 |
698a5b089234d038054386200216dd3fadea46982b2b7f2b8148dfb2f0aa1cf4
|