AWS Athena export array of structs to JSON

后端 未结 2 1921
梦谈多话
梦谈多话 2021-02-12 19:24

I\'ve got an Athena table where some fields have a fairly complex nested format. The backing records in S3 are JSON. Along these lines (but we have several more levels of nest

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-12 20:23

    I have skimmed through all the documentation and unfortunately there seems to be no way to do this as of now. The only possible workaround is

    converting a struct to a json when querying athena

    SELECT
      my_field,
      my_field.a,
      my_field.b,
      my_field.c.d,
      my_field.c.e
    FROM 
      my_table
    

    Or I would convert the data to json using post processing. Below script shows how

    #!/usr/bin/env python
    import io
    import re
    
    pattern1 = re.compile(r'(?<={)([a-z]+)=', re.I)
    pattern2 = re.compile(r':([a-z][^,{}. [\]]+)', re.I)
    pattern3 = re.compile(r'\\"', re.I)
    
    with io.open("test.csv") as f:
        headers = list(map(lambda f: f.strip(), f.readline().split(",")))
        for line in f.readlines():
            orig_line = line
            data = []
            for i, l in enumerate(line.split('","')):
                data.append(headers[i] + ":" + re.sub('^"|"$', "", l))
    
            line = "{" + ','.join(data) + "}"
            line = pattern1.sub(r'"\1":', line)
            line = pattern2.sub(r':"\1"', line)
            print(line)
    

    The output on your input data is

    {"timestamp":1.520640777666096E9,"stats":[{"time":15.0, "mean":45.23, "var":0.31}, {"time":19.0, "mean":17.315, "var":2.612}],"dets":[{"coords":[2.4, 1.7, 0.3], "header":{"frame":1, "seq":1, "name":"hello"}}],"pos":{"x":5.0, "y":1.4, "theta":0.04}
    }
    

    Which is a valid JSON

提交回复
热议问题