ภาพรวม
สาธิตการเปลี่ยนแปลงตารางเวลา (schedule) ของ crawler ใน AWS Glue โดยใช้ update_crawler_schedule ของ boto3
รูปแบบ Cron Expression
รูปแบบ: cron(Minutes Hours Day-of-month Month Day-of-week Year)
ตัวอย่าง: cron(15 12 * * ? *) = ทุกวันเวลา 12:15 UTC
ตัวอย่างโค้ด
import boto3
from botocore.exceptions import ClientError
def update_crawler_scheduler(crawler_name, scheduler):
session = boto3.session.Session()
glue_client = session.client('glue')
try:
response = glue_client.update_crawler_schedule(
CrawlerName=crawler_name,
Schedule=scheduler
)
return response
except ClientError as e:
raise Exception(f"boto3 client error: {e}")
except Exception as e:
raise Exception(f"Unexpected error: {e}")
# ใช้งาน: ตั้งให้รันทุกวัน 12:15 UTC
print(update_crawler_scheduler("Data Dimension", "cron(15 12 * * ? *)"))ผลลัพธ์
{
'ResponseMetadata': {
'RequestId': '73e50130-...',
'HTTPStatusCode': 200,
...
}
}ข้อสังเกต
- ฟังก์ชันนี้อัปเดต schedule เท่านั้น ไม่ได้เริ่มหรือหยุด crawler
- ถ้า crawler กำลังรันอยู่ schedule ใหม่จะมีผลในรอบถัดไป
- ต้องมี permission
glue:UpdateCrawlerSchedule