aws glue:如何将嵌套的hive结构扩展到dict?

cbjzeqam  于 2021-06-24  发布在  Hive
关注(0)|答案(2)|浏览(435)

我正在尝试扩展由aws glue crawlerMap到python中嵌套字典的表中的字段Map。但是,我找不到任何spark/hive解析器来反序列化

var_type = 'struct<loc_lat:double,service_handler:string,ip_address:string,device:bigint,source:struct<id:string,contacts:struct<admin:struct<email:string,name:string>>,name:string>,loc_name:string>'

位于python dict的表\ schema['table']['storagedescriptor']['columns']中的字符串。
如何在glue中转储表定义:

import boto3
client = boto3.client('glue')
client.get_table(DatabaseName=selected_db, Name=selected_table)

答复:

table_schema = {'Table': {'Name': 'asdfasdf',
  'DatabaseName': 'asdfasdf',
  'Owner': 'owner',
  'CreateTime': datetime.datetime(2019, 7, 29, 13, 20, 13, tzinfo=tzlocal()),
  'UpdateTime': datetime.datetime(2019, 7, 29, 13, 20, 13, tzinfo=tzlocal()),
  'LastAccessTime': datetime.datetime(2019, 7, 29, 13, 20, 13, tzinfo=tzlocal()),
  'Retention': 0,
  'StorageDescriptor': {'Columns': [{'Name': 'version', 'Type': 'int'},
    {'Name': 'payload',
     'Type': 'struct<loc_lat:double,service_handler:string,ip_address:string,device:bigint,source:struct<id:string,contacts:struct<admin:struct<email:string,name:string>>,name:string>,loc_name:string>'},
    {'Name': 'origin', 'Type': 'string'}],
   'Location': 's3://asdfasdf/',
   'InputFormat': 'org.apache.hadoop.mapred.TextInputFormat',
   'OutputFormat': 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat',
   'Compressed': False,
   'NumberOfBuckets': -1,
   'SerdeInfo': {'SerializationLibrary': 'org.openx.data.jsonserde.JsonSerDe',
    'Parameters': {'paths': 'origin,payload,version'}},
   'BucketColumns': [],
   'SortColumns': [],
   'Parameters': {'CrawlerSchemaDeserializerVersion': '1.0',
    'CrawlerSchemaSerializerVersion': '1.0',
    'UPDATED_BY_CRAWLER': 'asdfasdf',
    'averageRecordSize': '799',
    'classification': 'json',
    'compressionType': 'none',
    'objectCount': '94',
    'recordCount': '92171',
    'sizeKey': '74221058',
    'typeOfData': 'file'},
   'StoredAsSubDirectories': False},
  'PartitionKeys': [{'Name': 'partition_0', 'Type': 'string'},
   {'Name': 'partition_1', 'Type': 'string'},
   {'Name': 'partition_2', 'Type': 'string'}],
  'TableType': 'EXTERNAL_TABLE',
  'Parameters': {'CrawlerSchemaDeserializerVersion': '1.0',
   'CrawlerSchemaSerializerVersion': '1.0',
   'UPDATED_BY_CRAWLER': 'asdfasdf',
   'averageRecordSize': '799',
   'classification': 'json',
   'compressionType': 'none',
   'objectCount': '94',
   'recordCount': '92171',
   'sizeKey': '74221058',
   'typeOfData': 'file'},
  'CreatedBy': 'arn:aws:sts::asdfasdf'},
 'ResponseMetadata': {'RequestId': 'asdfasdf',
  'HTTPStatusCode': 200,
  'HTTPHeaders': {'date': 'Thu, 01 Aug 2019 16:23:06 GMT',
   'content-type': 'application/x-amz-json-1.1',
   'content-length': '3471',
   'connection': 'keep-alive',
   'x-amzn-requestid': 'asdfasdf'},
  'RetryAttempts': 0}}

目标是一个python字典和每个字段类型的值,而不是嵌入的字符串。例如

expand_function('struct<loc_lat:double,service_handler:string,ip_address:string,device:bigint,source:struct<id:string,contacts:struct<admin:struct<email:string,name:string>>,name:string>,loc_name:string>'})

退货

{
 'loc_lat':'double', 
 'service_handler':'string',
 'ip_address':'string',
 'device':'bigint',
 'source':{'id':'string',
           'contacts': {
               'admin': {
                   'email':'string',
                   'name':'string'
               },
           'name':'string'
           },
 'loc_name':'string'
}

谢谢!

z4bn682m

z4bn682m1#

下面是一个在上面嵌入的配置单元结构字符串上运行的函数。

def _hive_struct_to_json(hive_struct):
    """
    Expands embedded Hive struct strings to Python dictionaries
    Args:
        Hive struct format as string
    Returns
        JSON object
    """
    # Convert embedded hive type definition string to JSON
    hive_struct = hive_struct.replace(':', '":"')
    hive_struct = hive_struct.replace(',', '","')
    hive_struct = hive_struct.replace('struct<', '{"')
    hive_struct = hive_struct.replace('"{"', '{"')
    hive_struct = hive_struct.replace('>', '"}')
    hive_struct = hive_struct.replace('}"', '}')

    return json.loads(hive_struct)

hive_str = 'struct<loc_lat:double,service_handler:string,ip_address:string,device:bigint,source:struct<id:string,contacts:struct<admin:struct<email:string,name:string>>,name:string>,loc_name:string>'

print(json.dumps(_hive_struct_to_json(hive_str),indent=2))

退货:

{
  "loc_lat": "double",
  "service_handler": "string",
  "ip_address": "string",
  "device": "bigint",
  "source": {
    "id": "string",
    "contacts": {
      "admin": {
        "email": "string",
        "name": "string"
      }
    },
    "name": "string"
  },
  "loc_name": "string"
}
gpnt7bae

gpnt7bae2#

接受的答案不处理数组。此解决方案可以:

import json
import re

def _hive_struct_to_json(hive_str):
    """
    Expands embedded Hive struct strings to Python dictionaries
    Args:
        Hive struct format as string
    Returns
        JSON object
    """
    r = re.compile(r'(.*?)(struct<|array<|[:,>])(.*)')
    root = dict()

    to_parse = hive_str
    parents = []
    curr_elem = root

    key = None
    while to_parse:
        left, operator, to_parse = r.match(to_parse).groups()

        if operator == 'struct<' or operator == 'array<':
            parents.append(curr_elem)
            new_elem = dict() if operator == 'struct<' else list()
            if key:
                curr_elem[key] = new_elem
                curr_elem = new_elem
            elif isinstance(curr_elem, list):
                curr_elem.append(new_elem)
                curr_elem = new_elem
            key = None
        elif operator == ':':
            key = left
        elif operator == ',' or operator == '>':
            if left:
                if isinstance(curr_elem, dict):
                    curr_elem[key] = left
                elif isinstance(curr_elem, list):
                    curr_elem.append(left)

            if operator == '>':
                curr_elem = parents.pop()

    return root

hive_str = '''
    struct<
        loc_lat:double,
        service_handler:string,
        ip_address:string,
        device:bigint,
        source:struct<
            id:string,
            contacts:struct<
                admin:struct<
                    email:string,
                    name:array<string>
                >
            >,
            name:string
        >,
        loc_name:string,
        tags:array<
            struct<
                key:string,
                value:string
            >
        >
    >
'''

hive_str = re.sub(r'[\s]+', '', hive_str).strip()

print(hive_str)
print(json.dumps(_hive_struct_to_json(hive_str), indent=2))

印刷品:

struct<loc_lat:double,service_handler:string,ip_address:string,device:bigint,source:struct<id:string,contacts:struct<admin:struct<email:string,name:array<string>>>,name:string>,loc_name:string,tags:array<struct<key:string,value:string>>>

{
  "loc_lat": "double",
  "service_handler": "string",
  "ip_address": "string",
  "device": "bigint",
  "source": {
    "id": "string",
    "contacts": {
      "admin": {
        "email": "string",
        "name": [
          "string"
        ]
      }
    },
    "name": "string"
  },
  "loc_name": "string",
  "tags": [
    {
      "key": "string",
      "value": "string"
    }
  ]
}

相关问题