समस्या विवरण - Python में boto3 लाइब्रेरी का उपयोग करके चेक करें कि Glue जॉब मौजूद है या नहीं। उदाहरण के लिए, चेक करें कि run_s3_file_job AWS Glue में मौजूद है या नहीं।
इस समस्या को हल करने का दृष्टिकोण/एल्गोरिदम
चरण 1 - अपवादों को संभालने के लिए boto3 और botocore एक्सेप्शन्स आयात करें।
चरण 2 - job_name फंक्शन में पैरामीटर है।
चरण 3 - boto3 लाइब्रेरी का उपयोग करके AWS सत्र बनाएं। सुनिश्चित करें कि region_name डिफ़ॉल्ट प्रोफ़ाइल में उल्लिखित है। यदि नहीं बताया गया है, तो सत्र बनाते समय region_name को स्पष्ट रूप से पास करें।
चरण 4 - Glue के लिए AWS क्लाइंट बनाएं।
चरण 5 - अब get_job फंक्शन का उपयोग करें और JobName पास करें।
चरण 6 - यदि जॉब मौजूद है, तो रिस्पॉन्स में जॉब के बारे में सभी विवरण होंगे, अन्यथा यह अपवाद फेंकता है।
चरण 7 - जॉब चेक करते समय यदि कुछ गलत हुआ तो सामान्य अपवाद को संभालें।
उदाहरण
चेक करने के लिए निम्न कोड का उपयोग करें कि Glue जॉब मौजूद है या नहीं -
import boto3 from botocore.exceptions import ClientError def check_glue_job_exists(job_name): session = boto3.session.Session() glue_client = session.client('glue') try: response = glue_client.get_job(JobName=job_name) return response except ClientError as e: raise Exception("boto3 client error in check_glue_job_exists: " + e.__str__()) except Exception as e: raise Exception("Unexpected error in check_glue_job_exists: " + e.__str__()) # मौजूदा जॉब चेक करने के लिए print(check_glue_job_exists("run_s3_file_job")) # जॉब मौजूद नहीं है print(check_glue_job_exists("run_s3_file_job_not_exist"))आउटपुट
# मौजूदा जॉब चेक करने के लिए {'Job': {'Name': 'run_s3_file_job', 'Description': 'Glue job for the test', 'Role': 'arn:aws:iam::12345:role/delegated/glue-service-role', 'CreatedOn': datetime.datetime(2021, 02, 10, 15, 7, 3, 638000, tzinfo=tzlocal()), 'LastModifiedOn': datetime.datetime(2021, 02, 10, 15, 7, 3, 638000, tzinfo=tzlocal()), 'ExecutionProperty': {'MaxConcurrentRuns': 1}, 'Command': {'Name': 'glueetl', 'ScriptLocation': 's3://test/pipeline.py', 'PythonVersion': '3'}, 'DefaultArguments': {'--job-language': 'python', 'Step': '0'}, 'MaxRetries': 0, 'AllocatedCapacity': 4, 'Timeout': 2880, 'MaxCapacity': 4.0, 'WorkerType': 'G.1X', 'NumberOfWorkers': 4, 'GlueVersion': '2.0'}, 'ResponseMetadata': {'RequestId': 'e3ec9e2c-e75d-4443-bfeafef674fff7e9', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Sat, 13 Feb 2021 13:20:27 GMT', 'content-type': 'application/x-amz-json-1.1', 'content-length': '1501', 'connection': 'keep-alive', 'x-amznrequestid': 'e3ec9e2c-e75d-4443-bfea-fef674fff7e9'}, 'RetryAttempts': 0}} # जॉब मौजूद नहीं है botocore.errorfactory.EntityNotFoundException: An error occurred (EntityNotFoundException) when calling the GetJob operation: Job with name: run_s3_file_job_not_exist not found.