如何通过API启动Oozie Coordinator任务并默认设置start_time为当前日期
start_time to Current Day on Trigger & API Launch Got it, let's walk through exactly what you need—setting the start_time to the current day when triggering an Oozie Coordinator job, and configuring the API to default to the current date automatically.
1. Triggering Coordinator Job with start_time Set to Current Day (Command Line)
If you're using the Oozie command-line tool, you can dynamically generate the current date in Oozie's required ISO 8601 UTC format and pass it as a parameter.
Oozie expects start_time in formats like yyyy-MM-dd'T'HH:mm'Z' (full timestamp) or yyyy-MM-dd (date-only, which defaults to midnight UTC). For "当日日期" (current day), using midnight UTC is usually the safest choice to align with daily job schedules.
Here's the command to run, using shell's date command to generate the timestamp on the fly:
oozie job -oozie http://your-oozie-server:11000/oozie \ -config /path/to/your/job.properties \ -run \ -Dstart_time=$(date +"%Y-%m-%dT00:00Z")
- The
$(date +"%Y-%m-%dT00:00Z")snippet generates the current date at midnight UTC, formatted exactly how Oozie needs it. - If your job accepts a date-only format, you can simplify to
$(date +"%Y-%m-%d").
2. Launching Coordinator via API with Default Current Date start_time
To automate this via Oozie's REST API, you'll need to dynamically generate the current date in your code and include it in the POST request payload.
Example: Python Script to Launch Coordinator with Current start_time
Oozie's API endpoint for starting jobs is http://<oozie-server>:11000/oozie/v1/jobs, and you send a JSON payload with the job configuration. Here's a practical script that sets start_time to the current day automatically:
import requests from datetime import datetime # Configure your Oozie server details Oozie_BASE_URL = "http://your-oozie-server:11000/oozie/v1/jobs" HEADERS = {"Content-Type": "application/json"} # Generate current day's midnight UTC timestamp (matches Oozie's required format) current_start_time = datetime.utcnow().strftime("%Y-%m-%dT00:00Z") # Build the API payload job_payload = { "action": "start", "configuration": { "oozie.coord.application.path": "hdfs://your-cluster/path/to/coordinator.xml", "start_time": current_start_time, # Add other required configs (e.g., end_time, timezone, job-specific params) here "end_time": current_start_time # Adjust end_time as needed for your job } } # Send the POST request to start the job response = requests.post(Oozie_BASE_URL, json=job_payload, headers=HEADERS) # Check the response if response.status_code == 200: print(f"Coordinator job started successfully! Job ID: {response.json()['id']}") else: print(f"Job failed to start. Error: {response.json()}")
Key Notes for API Usage:
- Timezone: Always use UTC for
start_time(Oozie operates in UTC by default) to avoid timezone mismatches. - Authentication: If your cluster uses Kerberos, you'll need to add authentication handling (e.g., using
requests_kerberoslibrary) to the script. - Fallback Default in Coordinator XML: If you want a fallback default in your
coordinator.xml, you can use Oozie's EL expressions—though external parameters (from API/CLI) will override this. Example:
You could set a default like<coordinator-app name="daily-job" frequency="${coord:days(1)}" start="${start_time}" end="${end_time}" timezone="UTC" xmlns="uri:oozie:coordinator:0.4"> <!-- Job actions here --> </coordinator-app>${coord:formatTime(coord:now(), 'yyyy-MM-dd\'T\'00:00Z')}if needed, but dynamic generation via API/CLI is more reliable for "current day" logic.
内容的提问来源于stack exchange,提问作者Sagan Pariyar

