PyAthenaJDBC 查询字符串格式化问题
PyAthenaJDBC query string formatting issue
我正在尝试使用 PyAthenaJDBC 库制作一个 python 脚本,该脚本将使用 Athena 查询 S3 存储桶。该库非常棒,但我遇到了格式问题。
我在单独的函数中将查询构建为字符串,并将查询字符串传回 cursor.execute(query)
。
查询字符串中包含引号,如下所示:
SELECT day, elb_name
,COUNT (*) AS c
,100.0 * (
1.0 - (
SUM (
CASE
WHEN elb_response_code LIKE '5%' THEN 1
ELSE 0
END
) / cast(COUNT (*) as double)
)
) AS success_rate
,100.0 * SUM (
CASE
WHEN backend_processing_time < 0.1 THEN 1
ELSE 0
END
) / cast(COUNT (*) as double) AS t_lt_pt1
,100.0 * SUM (
CASE
WHEN backend_processing_time < 1 THEN 1
ELSE 0
END
) / cast(COUNT (*) as double) AS t_lt_1
,100.0 * SUM (
CASE
WHEN backend_processing_time < 5 THEN 1
ELSE 0
END
) / cast(COUNT (*) as double) AS t_lt_5
,100.0 * SUM (
CASE
WHEN backend_processing_time < 10 THEN 1
ELSE 0
END
) / cast(COUNT (*) as double) AS t_lt_10
FROM elb_logs_raw_native_part
WHERE year = '2017' AND
month = '03' AND
elb_name is not NULL AND
elb_name != ''
GROUP BY day, elb_name
ORDER BY c DESC
这将导致 `LIKE '5%' 语句中的第二个单引号出现错误。
ValueError: unsupported format character ''' (0x27) at index 186
我可以通过更改库中的这一行来避免错误并成功执行查询 https://github.com/laughingman7743/PyAthenaJDBC/blob/master/pyathenajdbc/formatter.py#L115
从return (operation % kwargs).strip()
到return (operation).strip()
此时在代码中,operation == query
(粘贴在上面)和kwargs == {}
我的具体问题是,我的查询结构是否不正确?或者这是我对本机字符串格式不了解的地方,尝试用空字典进行替换是个坏主意?
如果查询用作要格式化的字符串,您需要将要保留的任何 %
个字符加倍为文字 %
个字符。
而不是 WHEN elb_response_code LIKE '5%' THEN 1
,使用 WHEN elb_response_code LIKE '5%%' THEN 1
。在 formatter.py 中指定的行运行后,%%
将被转换为单个 %
.
我正在尝试使用 PyAthenaJDBC 库制作一个 python 脚本,该脚本将使用 Athena 查询 S3 存储桶。该库非常棒,但我遇到了格式问题。
我在单独的函数中将查询构建为字符串,并将查询字符串传回 cursor.execute(query)
。
查询字符串中包含引号,如下所示:
SELECT day, elb_name
,COUNT (*) AS c
,100.0 * (
1.0 - (
SUM (
CASE
WHEN elb_response_code LIKE '5%' THEN 1
ELSE 0
END
) / cast(COUNT (*) as double)
)
) AS success_rate
,100.0 * SUM (
CASE
WHEN backend_processing_time < 0.1 THEN 1
ELSE 0
END
) / cast(COUNT (*) as double) AS t_lt_pt1
,100.0 * SUM (
CASE
WHEN backend_processing_time < 1 THEN 1
ELSE 0
END
) / cast(COUNT (*) as double) AS t_lt_1
,100.0 * SUM (
CASE
WHEN backend_processing_time < 5 THEN 1
ELSE 0
END
) / cast(COUNT (*) as double) AS t_lt_5
,100.0 * SUM (
CASE
WHEN backend_processing_time < 10 THEN 1
ELSE 0
END
) / cast(COUNT (*) as double) AS t_lt_10
FROM elb_logs_raw_native_part
WHERE year = '2017' AND
month = '03' AND
elb_name is not NULL AND
elb_name != ''
GROUP BY day, elb_name
ORDER BY c DESC
这将导致 `LIKE '5%' 语句中的第二个单引号出现错误。
ValueError: unsupported format character ''' (0x27) at index 186
我可以通过更改库中的这一行来避免错误并成功执行查询 https://github.com/laughingman7743/PyAthenaJDBC/blob/master/pyathenajdbc/formatter.py#L115
从return (operation % kwargs).strip()
到return (operation).strip()
此时在代码中,operation == query
(粘贴在上面)和kwargs == {}
我的具体问题是,我的查询结构是否不正确?或者这是我对本机字符串格式不了解的地方,尝试用空字典进行替换是个坏主意?
如果查询用作要格式化的字符串,您需要将要保留的任何 %
个字符加倍为文字 %
个字符。
而不是 WHEN elb_response_code LIKE '5%' THEN 1
,使用 WHEN elb_response_code LIKE '5%%' THEN 1
。在 formatter.py 中指定的行运行后,%%
将被转换为单个 %
.