|
| 1 | +import sys |
| 2 | +from awsglue.transforms import * |
| 3 | +from awsglue.utils import getResolvedOptions |
| 4 | +from pyspark.context import SparkContext |
| 5 | +from awsglue.context import GlueContext |
| 6 | +from awsglue.job import Job |
| 7 | + |
| 8 | +args = getResolvedOptions(sys.argv, ['JOB_NAME']) |
| 9 | + |
| 10 | +sc = SparkContext() |
| 11 | +glueContext = GlueContext(sc) |
| 12 | +spark = glueContext.spark_session |
| 13 | +job = Job(glueContext) |
| 14 | + |
| 15 | +# fetch database and table metadata |
| 16 | + |
| 17 | +# --- using spark sql example --- |
| 18 | +# NOTE: `--enable-glue-datacatalog` must be enabled for job |
| 19 | +def spark_sql_example(): |
| 20 | + spark.sql("use default") |
| 21 | + tables = spark.sql("show tables").rdd.collect() |
| 22 | + output = '' |
| 23 | + for table in tables: |
| 24 | + output += f"-- schema for {table.tableName} ---\n" |
| 25 | + tableDescribe = spark.sql(f"describe `{table.tableName}`").rdd.collect() |
| 26 | + for column in tableDescribe: |
| 27 | + output += f"column name: {column['col_name']}, type: {column['data_type']}\n" |
| 28 | + output +="\n\n\n" |
| 29 | + |
| 30 | + print(output) |
| 31 | + |
| 32 | +# --- using boto3 example --- |
| 33 | + |
| 34 | +import boto3 |
| 35 | +import json |
| 36 | + |
| 37 | +def glue_client_example(): |
| 38 | + client = boto3.client('glue') |
| 39 | + databases_resp = client.get_databases() |
| 40 | + for database in databases_resp['DatabaseList']: |
| 41 | + database_name = database['Name'] |
| 42 | + tables_resp = client.get_tables(DatabaseName=database_name) |
| 43 | + for table in tables_resp['TableList']: |
| 44 | + table_name = table['Name'] |
| 45 | + table_resp = client.get_table(DatabaseName=database_name, Name=table_name) |
| 46 | + print(json.dumps(table_resp, indent=2, sort_keys=True, default=str)) |
| 47 | + |
| 48 | +def main(): |
| 49 | + spark_sql_example() |
| 50 | + glue_client_example() |
| 51 | + |
| 52 | +main() |
| 53 | + |
0 commit comments