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 nesting):
我有一个Athena表,其中一些字段具有相当复杂的嵌套格式。 S3中的后备记录是JSON。沿着这些方向(但我们有几个级别的嵌套):
CREATE EXTERNAL TABLE IF NOT EXISTS test (
timestamp double,
stats array<struct<time:double, mean:double, var:double>>,
dets array<struct<coords: array<double>, header:struct<frame:int,
seq:int, name:string>>>,
pos struct<x:double, y:double, theta:double>
)
ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
WITH SERDEPROPERTIES ('ignore.malformed.json'='true')
LOCATION 's3://test-bucket/test-folder/'
Now we need to be able to query the data and import the results into Python for analysis. Because of security restrictions I can't connect directly to Athena; I need to be able to give someone the query and then they will give me the CSV results.
现在我们需要能够查询数据并将结果导入Python进行分析。由于安全限制,我无法直接连接到雅典娜;我需要能够给某人查询,然后他们会给我CSV结果。
If we just do a straight select * we get back the struct/array columns in a format that isn't quite JSON. Here's a sample input file entry:
如果我们只是直接选择*,我们以不完全JSON的格式返回struct / array列。这是一个示例输入文件条目:
{"timestamp":1520640777.666096,"stats":[{"time":15,"mean":45.23,"var":0.31},{"time":19,"mean":17.315,"var":2.612}],"dets":[{"coords":[2.4,1.7,0.3], "header":{"frame":1,"seq":1,"name":"hello"}}],"pos": {"x":5,"y":1.4,"theta":0.04}}
And example output:
示例输出:
select * from test
"timestamp","stats","dets","pos"
"1.520640777666096E9","[{time=15.0, mean=45.23, var=0.31}, {time=19.0, mean=17.315, var=2.612}]","[{coords=[2.4, 1.7, 0.3], header={frame=1, seq=1, name=hello}}]","{x=5.0, y=1.4, theta=0.04}"
I was hoping to get those nested fields exported in a more convenient format - getting them in JSON would be great.
我希望以更方便的格式导出那些嵌套字段 - 用JSON获取它们会很棒。
Unfortunately it seems that cast to JSON only works for maps, not structs, because it just flattens everything into arrays:
不幸的是,似乎转换为JSON只适用于地图而不是结构,因为它只是将所有内容展平为数组:
SELECT timestamp, cast(stats as JSON) as stats, cast(dets as JSON) as dets, cast(pos as JSON) as pos FROM "sampledb"."test"
"timestamp","stats","dets","pos"
"1.520640777666096E9","[[15.0,45.23,0.31],[19.0,17.315,2.612]]","[[[2.4,1.7,0.3],[1,1,""hello""]]]","[5.0,1.4,0.04]"
Is there a good way to convert to JSON (or another easy-to-import format) or should I just go ahead and do a custom parsing function?
是否有一种转换为JSON(或其他易于导入的格式)的好方法,还是应该继续进行自定义解析功能?
1 个解决方案
#1
3
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
在查询athena时将结构转换为json
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
或者我会使用后期处理将数据转换为json。下面的脚本显示了如何
#!/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 = 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
哪个是有效的JSON
#1
3
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
在查询athena时将结构转换为json
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
或者我会使用后期处理将数据转换为json。下面的脚本显示了如何
#!/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 = 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
哪个是有效的JSON