Partitioning Athena Tables from Glue Cloudformation template

风格不统一 提交于 2021-02-19 06:28:26

问题


Using AWS::Glue::Table, you can set up an Athena table like here. Athena supports partitioning data based on folder structure in S3. I would like to partition my Athena table from my Glue template.

From AWS Glue Table TableInput, it appears that I can use PartitionKeys to partition my data, but when I try to use the below template, Athena fails and can't get any data.

Resources:
  ...

  MyGlueTable:
    Type: AWS::Glue::Table
    Properties:
      DatabaseName: !Ref MyGlueDatabase
      CatalogId: !Ref AWS::AccountId
      TableInput:
        Name: my-glue-table
        Parameters: { "classification" : "json" }
        PartitionKeys:
          - {Name: dt, Type: string}
        StorageDescriptor:
          Location: "s3://elasticmapreduce/samples/hive-ads/tables/impressions/"
          InputFormat: "org.apache.hadoop.mapred.TextInputFormat"
          OutputFormat: "org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat"
          SerdeInfo:
            Parameters: { "separatorChar" : "," }
            SerializationLibrary: "org.apache.hive.hcatalog.data.JsonSerDe"
          StoredAsSubDirectories: false
          Columns:
            - {Name: requestBeginTime, Type: string}
            - {Name: adId, Type: string}
            - {Name: impressionId, Type: string}
            - {Name: referrer, Type: string}
            - {Name: userAgent, Type: string}
            - {Name: userCookie, Type: string}
            - {Name: ip, Type: string}
            - {Name: number, Type: string}
            - {Name: processId, Type: string}
            - {Name: browserCookie, Type: string}
            - {Name: requestEndTime, Type: string}
            - {Name: timers, Type: "struct<modellookup:string,requesttime:string>"}
            - {Name: threadId, Type: string}
            - {Name: hostname, Type: string}
            - {Name: sessionId, Type: string}

How do I partition my data in AWS Glue?


回答1:


Got it! Pretty painful, because I had to run Glue Crawler which properly created the table with partitions, and then use the CLI to extract the correct template parameters. Here is the template,

AWSTemplateFormatVersion: 2010-09-09
Description: A partitioned Glue Table

Resources:
  MyGlueDatabase:
    Type: AWS::Glue::Database
    Properties:
      DatabaseInput:
        Name: my_glue_database
        Description: "Glue beats tape"
      CatalogId: !Ref AWS::AccountId

  MyGlueTable:
    Type: AWS::Glue::Table
    Properties:
      DatabaseName: !Ref MyGlueDatabase
      CatalogId: !Ref AWS::AccountId
      TableInput:
        Name: my_glue_table
        TableType: EXTERNAL_TABLE
        Parameters:
          CrawlerSchemaDeserializerVersion': "1.0"
          CrawlerSchemaSerializerVersion': "1.0"
          classification': json
          compressionType': none
          typeOfData': file
        PartitionKeys:
          - {Name: dt, Type: string}
        StorageDescriptor:
          BucketColumns: []
          Columns:
          - {Name: number, Type: string}
          - {Name: referrer, Type: string}
          - {Name: processid, Type: string}
          - {Name: adid, Type: string}
          - {Name: browsercookie, Type: string}
          - {Name: usercookie, Type: string}
          - {Name: requestendtime, Type: string}
          - {Name: impressionid, Type: string}
          - {Name: useragent, Type: string}
          - {Name: timers, Type: 'struct<modelLookup:string,requestTime:string>'}
          - {Name: threadid, Type: string}
          - {Name: ip, Type: string}
          - {Name: modelid, Type: string}
          - {Name: hostname, Type: string}
          - {Name: sessionid, Type: string}
          - {Name: requestbegintime, Type: string}
          Compressed: false
          InputFormat: org.apache.hadoop.mapred.TextInputFormat
          Location: s3://elasticmapreduce/samples/hive-ads/tables/impressions/
          NumberOfBuckets: -1
          OutputFormat: org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat
          Parameters: {CrawlerSchemaDeserializerVersion: '1.0', CrawlerSchemaSerializerVersion: '1.0',
            UPDATED_BY_CRAWLER: test, averageRecordSize: '644', classification: json,
            compressionType: none, objectCount: '241', recordCount: '1000109', sizeKey: '648533598',
            typeOfData: file}
          SerdeInfo:
            Parameters: {paths: 'adId,browserCookie,hostname,impressionId,ip,modelId,number,processId,referrer,requestBeginTime,requestEndTime,sessionId,threadId,timers,userAgent,userCookie'}
            SerializationLibrary: org.openx.data.jsonserde.JsonSerDe
          SortColumns: []
          StoredAsSubDirectories: false

Then Cloudformation will deploy the table, and you need to run.

MSCK REPAIR TABLE my_glue_table;

That will add all the partitions, which you will see in the output like,

Repair: Added partition to metastore my_glue_table:dt=2009-04-12-13-00
Repair: Added partition to metastore my_glue_table:dt=2009-04-12-13-05

Then you can run SQL on top of this partitioning like,

%% SELECT * FROM "my_glue_database"."my_glue_table" WHERE dt = '2009-04-14-13-00' LIMIT 10;
1   7663    cartoonnetwork.com  1178    SxRBJCmJBCLcfTS545t6qD1M8L64SC  nsdfvfvger  3VCLfFfF75BDgHgDoowHegOpkCivMJ  1239714024000   RTM6Vtrc1O3KX2FlUghUSiAQHiix8F  Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; GTB6; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0) {modellookup=0.3538, requesttime=0.7532}    15  37.215.88.35    bxxiuxduad  ec2-50-32-48-14.amazon.com  BIBIlA7dgXc2eWekUJ6hSXa7p6dQEx  1239714024000   2009-04-14-13-00
2   17646   coursera.org    1255    Fskm4W6JKX6vf7UMaW55KObTJCtm1E  xftjotkexc  jH6DRWtkeH3tVg6c4mcLW36UW3LvqX  1239714027000   uQqO1fNoeM8KdesiVg86o4iK7FkqLt  Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322)  {modellookup=0.2986, requesttime=0.9616}    21  37.218.101.204  bxxiuxduad  ec2-50-32-48-14.amazon.com  OjgTQWOqHJopoWf9LpJ4We1UE7uJao  1239714026000   2009-04-14-13-00


来源:https://stackoverflow.com/questions/50279209/partitioning-athena-tables-from-glue-cloudformation-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!