Format: text/x-python
⬇ Download File (2.5 KB)
   1  import boto3
   2  
   3  class helper:
   4      def get_table_location(self,database, table_name):
   5          """
   6          get table data location from glue catalogue
   7          :param database: database name
   8          :param table_name: table name
   9          :return: full path to data
  10          """
  11          boto_glue = boto3.client("glue", region_name='us-east-1', 
  12                        aws_access_key_id='AKIATBUD4GB6F6DFJQ53',
  13                    aws_secret_access_key='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')
  14          table_def = boto_glue.get_table(DatabaseName=database, Name=table_name)
  15          return table_def["Table"]["StorageDescriptor"]["Location"]
  16  
  17  
  18  
  19      def create_index(self,database, table, column, index_name):
  20          # Create a client for the Glue service
  21          client = boto3.client("glue", region_name='us-east-1', 
  22                          aws_access_key_id='AKIATBUD4GB6F6DFJQ53',
  23                      aws_secret_access_key='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')
  24          
  25          # Create an index on the specified column of the table
  26          client.create_partition_index(
  27              TableName={
  28                  "DatabaseName": database,
  29                  "Name": table
  30              },
  31              PartitionIndex={
  32                  "Name": index_name,
  33                  "Columns": [
  34                      {
  35                          "Name": column,
  36                          "Type": "varchar",
  37                          "SortOrder": 1
  38                      }
  39                  ],
  40                  "IsUnique": True
  41              }
  42          )
  43  
  44  
  45      def create_partition_index(self, catalog_id, database_name, table_name, index_name, keys):
  46          glue_client = boto3.client("glue", region_name='us-east-1', 
  47                              aws_access_key_id='AKIATBUD4GB6F6DFJQ53',
  48                          aws_secret_access_key='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')
  49  
  50          response = glue_client.create_partition_index(
  51              CatalogId=catalog_id,
  52              DatabaseName=database_name,
  53              TableName=table_name,
  54              PartitionIndex={
  55                  'IndexName': index_name,
  56                  'Keys': ['uuid_z']
  57              }
  58          )
  59  
  60      
  61  
  62  """# Example usage"""
  63  
  64  
  65  #print(helper.get_table_location('assortment_dev','assortment_dev'))
  66  h= helper()
  67  h.create_partition_index(
  68      
  69      database_name='bungeedatawarehouse',
  70      table_name='bungee_global_prod_d_back_up_20221229',
  71      index_name='idx_uuid_z',
  72      keys=[
  73          {
  74              'Name': 'uuid_z',
  75              'Type': 'string'
  76          }
  77      ]
  78  )