Hive SQL报错:SemanticException [Error 10004]: Invalid table alias or column reference

在python脚本中执行Hive SQL

python脚本如下:

sql="""SELECT
test_url,
(case
when substring_index(test_url, '/', 1) == 'dev.test.com`:`1024/' then substr(test_url,25)
when substring_index(test_url, '/', 1) == 'localhost:80/' then substr(test_url,21)
else test_url
end) as test_url_cleaned,
page_title,
FROM test.report_table
where dt = '%s' and web_id in
(
select web_id FROM dev.dev_report
) """% (num)

执行python脚本过程中报错:

FAILED: SemanticException [Error 10004]: Line 73:47 Invalid table alias or column reference ‘dev’: (possible column names are: test_url, test_url, page_title)

排查过程:
将SQL脚本单独拿出来执行时执行成功,但是实际在执行过程中报错如上,在google搜索错误信息时一开始用FAILED: SemanticException [Error 10004]搜索并没有搜索出有用的信息,多次检查脚本、搜索错误信息无果后极度想放弃。后来详细看了一下报错信息Invalid table alias or column reference ‘dev’,无效的表别名或者列引用,因为在我的sql中dev是表名起到了混淆作用,因此一开始并没有反应过来。后来将dev去掉后,发现报错为:Invalid table alias or column reference ‘test’:,因此找到问题所在:在python脚本中执行Hive SQL,拼接SQL语句时只能用占位符的方式拼接字符串。

修改后如下:

	sql="""SELECT
			test_url,
			(case
			    when substring_index(test_url, '/', 1) == '%s' then substr(test_url,25)
			    when substring_index(test_url, '/', 1) ==  '%s' then substr(test_url,21)
			    else ct_url
			end) as test_url_cleaned,
			page_title,
		FROM
			test.report_table
		where
			dt = '%s'
			and web_id in
			(
				select web_id FROM dev.dev_report
			)
	) """% (url1,url2,num)
 
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐