在使用Coral-trino翻译Hivesql2PrestoSql遇见的问题和解决方法

本文主要是介绍在使用Coral-trino翻译Hivesql2PrestoSql遇见的问题和解决方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

随着公司业务的发展,hive, kylin在用户的即时查询时,越来越难以满足用户快速的需求。另外最近Presto在大数据即时查询方面性能越来越来,很多企业都在接入和使用presto,网上相关的技术文章很多,因此,我们在接入Presto查询引擎以后,也不得不处理Sql路由的问题。现在数据分析师和数据ETL开发的同学已经习惯了Hive常用的写法,虽然prestoSql 与hiveSql在语法方法很接近,但是在部分语义和函数方面还有不相同的地方,这就给用户带来使用的困惑和不便。为了保证用户的平滑的接入Presto, 改善即时查询平台的性能和体验,我们调研了许多网页大牛的分析的技术博客和文章,其中一个大牛文章:Presto/Trino执行Hive SQL的方案探索 - 知乎,总结了hivesql翻译prestoSql的三种方案,并在文章最后提议使用Coral-trino开源的组件实现hiveSql2prestoSql的翻译功能。此外呢,B站在使用presto查询引擎时,也用到Coral-trino,来翻译hiveSql,具体文章:Presto在B站的实践 - 哔哩哔哩。下面说下Coral-trino的接入和遇见问题,以及解决方法。

一、Coral-trino的接入和使用

1.1 Coral-trino的接入

    1.1.1 Coral开源项目地址

          Coral 开源地址:https://github.com/linkedin/coral,目前该项目的ljfgem 老兄一直在孜孜不倦的迭代,为我们解决问题。虽然这项目是gradle 开发的,对于习惯用Maven的同学接入起来有点麻烦(在下不才,现在也没搞通gradle),但是并不影响我们代码阅读。

      阿里云maven中央仓库, https://developer.aliyun.com/mvn/search

  
maven地址:
<dependency><groupId>com.linkedin.coral</groupId><artifactId>coral-trino</artifactId><version>2.0.79</version>
</dependency>

     1.1.2 Coral的基础组件Apache Calcite

       Coral是由linkedin开源的,其基础是Apache Calcite,并对其做大量的优化,并将其开源出来,linkedin版的calcite 项目地址: GitHub - linkedin/linkedin-calcite: LinkedIn's version of Apache Calcite

1.2 HiveSql2PrestoSql翻译Demo

@Slf4j
public class CoralHiveTest {private static HiveConf hiveConf;private static HiveToRelConverter hiveToRelConverter;private static HiveToTrinoConverter hiveToTrinoConverter;@BeforeClasspublic static void beforeClass() throws Exception{hiveConf = new HiveConf();hiveConf.set("hive.metastore.uris", "thrift://(your hive metastore ip):9083");HiveMetaStoreClient metaStoreClient = new HiveMetaStoreClient(hiveConf);HiveMscAdapter hiveMscAdapteri = new HiveMscAdapter(metaStoreClient);List<String> dbNamses = hiveMscAdapteri.getAllDatabases();log.info("dbNamses size:{}", dbNamses.size());List<String> tablenames = hiveMscAdapteri.getAllTables("dw_dim");hiveToRelConverter = new HiveToRelConverter(hiveMscAdapteri);}@Testpublic void testLateralViewArray2() {String sql = "select collect_list(shop_name) from dw_dim.tb_dim_shop where " +"city_name in ('石家庄')";RelNode relNode = hiveToRelConverter.convertSql(sql);RelToTrinoConverter relToTrinoConverter = new RelToTrinoConverter();String expandedSql = relToTrinoConverter.convert(relNode);log.info("expandedSql:{}", expandedSql);}
}

 二、遇见的问题以及解决方法

2.1 hive版本低导致hive库表找不到异常

org.apache.calcite.runtime.CalciteContextException: At line 0, column 0: Object 't_dim_shop' not found within 'hive.dw_dim'at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)at java.lang.reflect.Constructor.newInstance(Constructor.java:423)at org.apache.calcite.runtime.Resources$ExInstWithCause.ex(Resources.java:463)at org.apache.calcite.sql.SqlUtil.newContextException(SqlUtil.java:834)at org.apache.calcite.sql.SqlUtil.newContextException(SqlUtil.java:819)at org.apache.calcite.sql.validate.SqlValidatorImpl.newValidationError(SqlValidatorImpl.java:4867)at org.apache.calcite.sql.validate.IdentifierNamespace.resolveImpl(IdentifierNamespace.java:127)at org.apache.calcite.sql.validate.IdentifierNamespace.validateImpl(IdentifierNamespace.java:177)at org.apache.calcite.sql.validate.AbstractNamespace.validate(AbstractNamespace.java:84)at org.apache.calcite.sql.validate.SqlValidatorImpl.validateNamespace(SqlValidatorImpl.java:1005)at org.apache.calcite.sql.validate.SqlValidatorImpl.validateQuery(SqlValidatorImpl.java:965)at org.apache.calcite.sql.validate.SqlValidatorImpl.validateFrom(SqlValidatorImpl.java:3125)at org.apache.calcite.sql.validate.SqlValidatorImpl.validateSelect(SqlValidatorImpl.java:3379)at org.apache.calcite.sql.validate.SelectNamespace.validateImpl(SelectNamespace.java:60)at org.apache.calcite.sql.validate.AbstractNamespace.validate(AbstractNamespace.java:84)at org.apache.calcite.sql.validate.SqlValidatorImpl.validateNamespace(SqlValidatorImpl.java:1005)at org.apache.calcite.sql.validate.SqlValidatorImpl.validateQuery(SqlValidatorImpl.java:965)at org.apache.calcite.sql.SqlSelect.validate(SqlSelect.java:216)at org.apache.calcite.sql.validate.SqlValidatorImpl.validateScopedExpression(SqlValidatorImpl.java:940)at org.apache.calcite.sql.validate.SqlValidatorImpl.validate(SqlValidatorImpl.java:647)at com.linkedin.coral.hive.hive2rel.HiveSqlToRelConverter.convertQuery(HiveSqlToRelConverter.java:59)at com.linkedin.coral.common.ToRelConverter.toRel(ToRelConverter.java:166)at com.linkedin.coral.common.ToRelConverter.convertSql(ToRelConverter.java:119)at com.luckincoffee.dataq.CoralHiveTest.testLateralViewArray2(CoralHiveTest.java:56)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:498)at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)at org.junit.runners.ParentRunner.run(ParentRunner.java:363)at org.junit.runner.JUnitCore.run(JUnitCore.java:137)at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
Caused by: org.apache.calcite.sql.validate.SqlValidatorException: Object 't_dim_shop' not found within 'hive.dw_dim'at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)at java.lang.reflect.Constructor.newInstance(Constructor.java:423)at org.apache.calcite.runtime.Resources$ExInstWithCause.ex(Resources.java:463)at org.apache.calcite.runtime.Resources$ExInst.ex(Resources.java:572)... 44 more

  根据原因是因为我的本地hive版本太低,只是1.2.1版本,我的hive-metastore版本是2.3.6,

改用1.2.1版本以后就解决了,库表找不到问题。

2.2 sql中汉字被unicode编码后报错问题

   当我执行这样的sql时:

 

 String sql = "select * from dw_dim.tb_dim_shop where dt= '2022-05-28' and city_name in ('石家庄')";
即当hivesql中包含中文时,就会抛出这样的错:

java.lang.RuntimeException: while converting `tb_dim_shop`.`dt` = '2022-05-28' AND `dim_shop_d_his`.`city_name` IN ((u&'\77f3\5bb6\5e84'))at org.apache.calcite.sql2rel.ReflectiveConvertletTable.lambda$registerNodeTypeMethod$0(ReflectiveConvertletTable.java:86)at org.apache.calcite.sql2rel.SqlNodeToRexConverterImpl.convertCall(SqlNodeToRexConverterImpl.java:63)at org.apache.calcite.sql2rel.SqlToRelConverter$Blackboard.visit(SqlToRelConverter.java:4787)at org.apache.calcite.sql2rel.SqlToRelConverter$Blackboard.visit(SqlToRelConverter.java:4092)at org.apache.calcite.sql.SqlCall.accept(SqlCall.java:139)at org.apache.calcite.sql2rel.SqlToRelConverter$Blackboard.convertExpression(SqlToRelConverter.java:4656)at org.apache.calcite.sql2rel.SqlToRelConverter.convertWhere(SqlToRelConverter.java:981)at org.apache.calcite.sql2rel.SqlToRelConverter.convertSelectImpl(SqlToRelConverter.java:649)at org.apache.calcite.sql2rel.SqlToRelConverter.convertSelect(SqlToRelConverter.java:627)at org.apache.calcite.sql2rel.SqlToRelConverter.convertQueryRecursive(SqlToRelConverter.java:3181)at com.linkedin.coral.hive.hive2rel.HiveSqlToRelConverter.convertQuery(HiveSqlToRelConverter.java:63)at com.linkedin.coral.common.ToRelConverter.toRel(ToRelConverter.java:166)at com.linkedin.coral.common.ToRelConverter.convertSql(ToRelConverter.java:119)at com.luckincoffee.dataq.CoralHiveTest.testCnWhere(CoralHiveTest.java:70)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:498)at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)at org.junit.runners.ParentRunner.run(ParentRunner.java:363)at org.junit.runner.JUnitCore.run(JUnitCore.java:137)at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
Caused by: java.lang.reflect.InvocationTargetExceptionat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:498)at org.apache.calcite.sql2rel.ReflectiveConvertletTable.lambda$registerNodeTypeMethod$0(ReflectiveConvertletTable.java:83)... 36 more
Caused by: java.lang.RuntimeException: No list startedat org.apache.calcite.sql.pretty.SqlPrettyWriter.sep(SqlPrettyWriter.java:958)at org.apache.calcite.sql.pretty.SqlPrettyWriter.sep(SqlPrettyWriter.java:953)at com.linkedin.coral.hive.hive2rel.functions.HiveInOperator.unparse(HiveInOperator.java:67)at org.apache.calcite.sql.SqlDialect.unparseCall(SqlDialect.java:437)at org.apache.calcite.sql.SqlCall.unparse(SqlCall.java:104)at org.apache.calcite.sql.SqlNode.toSqlString(SqlNode.java:153)at org.apache.calcite.sql.SqlNode.toSqlString(SqlNode.java:158)at org.apache.calcite.sql.SqlNode.toString(SqlNode.java:125)at java.lang.String.valueOf(String.java:2994)at java.lang.StringBuilder.append(StringBuilder.java:131)at org.apache.calcite.sql2rel.ReflectiveConvertletTable.lambda$registerOpTypeMethod$1(ReflectiveConvertletTable.java:127)at org.apache.calcite.sql2rel.SqlNodeToRexConverterImpl.convertCall(SqlNodeToRexConverterImpl.java:63)at org.apache.calcite.sql2rel.SqlToRelConverter$Blackboard.visit(SqlToRelConverter.java:4787)at org.apache.calcite.sql2rel.SqlToRelConverter$Blackboard.visit(SqlToRelConverter.java:4092)at org.apache.calcite.sql.SqlCall.accept(SqlCall.java:139)at org.apache.calcite.sql2rel.SqlToRelConverter$Blackboard.convertExpression(SqlToRelConverter.java:4656)at org.apache.calcite.sql2rel.StandardConvertletTable.convertExpressionList(StandardConvertletTable.java:793)at org.apache.calcite.sql2rel.StandardConvertletTable.convertCall(StandardConvertletTable.java:769)at org.apache.calcite.sql2rel.StandardConvertletTable.convertCall(StandardConvertletTable.java:756)... 41 more

 首先,我们会发现,汉字 '石家庄'  被unicode 为 'u&'\77f3\5bb6\5e84''。

 其次,我们看到报错的类都是calsite当中。

 

 然后,我通过 这篇博客,也是在calcite 处理中文出现乱码(Unicode)。Calcite中文字符串toSqlString()变为乱码(Unicode),重写SqlDialect类中的quoteStringLiteral()方法解决_黄飞666的博客-CSDN博客使用Calcite,中文字符串toSqlString()时会变成乱码(unicode),可以新建一个方言类,重写quoteStringLiteral()方法解决。没有重写quoteStringLiteral()时:重写quoteStringLiteral()后:原因在SqlDialect类中的quoteStringLiteral()方法:...https://blog.csdn.net/weixin_39133753/article/details/115470036最后在coral项目的issue中找到相同问题的反馈:https://github.com/linkedin/coral/issues/152

根据贡献人,提示:

 我们需要在项目的resource文件下中添加saffron.properties 配置文件,并增加属性配置:

calcite.default.charset = utf8

 设置calsite的默认charset = utf8, 通过这样就解决了,中文unicode编码的带来的异常。

2.3 hive的udf问题

在我们数据开发中,我们会开发一些hive的udf函数。这些udf 不能被calsite直接解析,就会抛出这样的报错:

com.linkedin.coral.common.functions.UnknownSqlFunctionException: Unknown function name: ifnull

目前针对hive udf 函数注册到calsite的hive解析器中,还在探索。最近学习了,yuqi,提供的一个开源calsite 函数注册的项目:GitHub - yuqi1129/calcite-test: Test code for apache calcite

后续文章将陆续分享验证结果。敬请期待哦。

这篇关于在使用Coral-trino翻译Hivesql2PrestoSql遇见的问题和解决方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/448807

相关文章

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ

qt5cored.dll报错怎么解决? 电脑qt5cored.dll文件丢失修复技巧

《qt5cored.dll报错怎么解决?电脑qt5cored.dll文件丢失修复技巧》在进行软件安装或运行程序时,有时会遇到由于找不到qt5core.dll,无法继续执行代码,这个问题可能是由于该文... 遇到qt5cored.dll文件错误时,可能会导致基于 Qt 开发的应用程序无法正常运行或启动。这种错

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(

Linux脚本(shell)的使用方式

《Linux脚本(shell)的使用方式》:本文主要介绍Linux脚本(shell)的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录概述语法详解数学运算表达式Shell变量变量分类环境变量Shell内部变量自定义变量:定义、赋值自定义变量:引用、修改、删

Java使用HttpClient实现图片下载与本地保存功能

《Java使用HttpClient实现图片下载与本地保存功能》在当今数字化时代,网络资源的获取与处理已成为软件开发中的常见需求,其中,图片作为网络上最常见的资源之一,其下载与保存功能在许多应用场景中都... 目录引言一、Apache HttpClient简介二、技术栈与环境准备三、实现图片下载与保存功能1.

Python中使用uv创建环境及原理举例详解

《Python中使用uv创建环境及原理举例详解》uv是Astral团队开发的高性能Python工具,整合包管理、虚拟环境、Python版本控制等功能,:本文主要介绍Python中使用uv创建环境及... 目录一、uv工具简介核心特点:二、安装uv1. 通过pip安装2. 通过脚本安装验证安装:配置镜像源(可

LiteFlow轻量级工作流引擎使用示例详解

《LiteFlow轻量级工作流引擎使用示例详解》:本文主要介绍LiteFlow是一个灵活、简洁且轻量的工作流引擎,适合用于中小型项目和微服务架构中的流程编排,本文给大家介绍LiteFlow轻量级工... 目录1. LiteFlow 主要特点2. 工作流定义方式3. LiteFlow 流程示例4. LiteF