我有一个 AWS Athena 服务。 查询 Athena 后生成一个 CSV 文件。 假设我想在 excel 或 google 工作表中打开该 CSV 时看到后续结果(带标题)。
7到13号线没问题。这是雅典娜的实际结果。 我想添加一个标题(如图所示)。
我如何做到这一点?
不可能,因为 Athena 不支持,也不兼容 CSV 格式。如果你想添加“标题”,你可以通过使用“union all”来使用变通方法,但它不会像你期望的那样给你结果。
关于CSV格式:
即使它很脏,我也看到它使用这些标头来避免值重复。我将在这里使用我自己的示例文件,您可以稍后修改它:
col0 | col1 | col2 | col3 | col4 |
---|---|---|---|---|
顾客 | 约翰史密斯 | -- | -- | -- |
order_id | ORD_212345 | -- | -- | -- |
约会 | 10/5/2012 | -- | -- | -- |
货币 | 欧元 | -- | -- | -- |
product_id | 产品名称 | 价格 | 单位 | 数量 |
PID_1234 | 糖 | 1.20 | 公斤 | 12.00 |
PID_2345 | 法里纳 | 2.10 | 公斤 | 20.00 |
PID_3455 | 鸡蛋 | 1.05 | 单位 | 200.00 |
PID_3555 | 亲爱的 | 3.85 | 升 | 3.00 |
前四行是脏标题,它们应该代表所有其余行重复的数据。结果会是这样的:
顾客 | order_id | 约会 | 货币 | product_id | 产品名称 | 价格 | 单位 | 数量 |
---|---|---|---|---|---|---|---|---|
约翰史密斯 | ORD_212345 | 10/5/2012 | 欧元 | PID_1234 | 糖 | 1.20 | 公斤 | 12.00 |
约翰史密斯 | ORD_212345 | 10/5/2012 | 欧元 | PID_2345 | 法里纳 | 2.10 | 公斤 | 20.00 |
约翰史密斯 | ORD_212345 | 10/5/2012 | 欧元 | PID_3455 | 鸡蛋 | 1.05 | 单位 | 200.00 |
约翰史密斯 | ORD_212345 | 10/5/2012 | 欧元 | PID_3555 | 亲爱的 | 3.85 | 升 | 3.00 |
一个可能的查询是:
WITH
RAW_DATA AS (
SELECT
col0, col1, col2, col3, col4,
, "$path" file
, row_number() over () as row_num
FROM
"database"."orders"
),
HEADER_ROWS AS (
SELECT
col0 key
, col1 value
, file
FROM
"database"."orders"
WHERE row_num between 1 and 4
)
, DATA_ROWS AS (
SELECT
col0 product_id
, col1 product_name
, col2 price
, col3 unit
, col4 amount
, file
FROM
"database"."orders"
WHERE (NOT (row_num between 1 and 5))
)
, HEADER AS (
SELECT
file
, "map_agg"(key, value) header
FROM
HEADER_ROWS
GROUP BY file
)
, ALL_DATA AS (
SELECT
, header['customer'] customer
, header['order_id'] order_id
, header['date'] date
, header['currency'] currency
, data_row.product_id
, data_row.product_name
, data_row.price
, data_row.unit
, data_row.amount
FROM
(HEADER
INNER JOIN DATA_ROWS data_row ON (HEADER.file = data_row.FILE))
)
SELECT * FROM ALL_DATA
如果你只想跳过头文件:
SELECT
*, row_number() over () as row_num
FROM
"mytable"
WHERE NOT (row_num between 1 and 7)