java - 如何使用 HIVE JDBC 驱动程序在列名中使用特殊字符?

标签 java jdbc hive

我有一个 java 程序,它使用 Hortonworks 的 JDBC 驱动程序连接到 VirtualBox 中的数据库。一切都工作得很好,但我在 sql 查询中遇到了冒号问题。

query = new StringBuilder("SELECT ROW_NUMBER() OVER() AS "+rowid+", * FROM "+tableName).toString();

我尝试设置的 rowid 是“:rowid:”。我在前面和后面添加了冒号,以使列名称对于其他应用程序来说是唯一的。现在冒号必须在那里,我想知道是否有办法使用官方网站上的 Hortonworks JDBC Driver for Apache Hive (v1.0.36) 来做到这一点。我已经在没有冒号的情况下对其进行了测试,这将起作用。

用冒号表示错误信息:

JDBC driver version: HiveJDBC 01.00.36.1046 java.sql.SQLException: [Simba]HiveJDBCDriver ERROR processing query/statement. Error Code: 40000, SQL state: TStatus(statusCode:ERROR_STATUS, infoMessages:[*org.apache.hive.service.cli.HiveSQLException:Error while compiling statement: FAILED: ParseException line 1:30 cannot recognize input near 'AS' ':' 'rowid' in selection target:17:16, org.apache.hive.service.cli.operation.Operation:toSQLException:Operation.java:335, org.apache.hive.service.cli.operation.SQLOperation:prepare:SQLOperation.java:148, org.apache.hive.service.cli.operation.SQLOperation:runInternal:SQLOperation.java:226, org.apache.hive.service.cli.operation.Operation:run:Operation.java:276, org.apache.hive.service.cli.session.HiveSessionImpl:executeStatementInternal:HiveSessionImpl.java:468, org.apache.hive.service.cli.session.HiveSessionImpl:executeStatementAsync:HiveSessionImpl.java:456, org.apache.hive.service.cli.CLIService:executeStatementAsync:CLIService.java:298, org.apache.hive.service.cli.thrift.ThriftCLIService:ExecuteStatement:ThriftCLIService.java:506, org.apache.hive.service.cli.thrift.TCLIService$Processor$ExecuteStatement:getResult:TCLIService.java:1317, org.apache.hive.service.cli.thrift.TCLIService$Processor$ExecuteStatement:getResult:TCLIService.java:1302, org.apache.thrift.ProcessFunction:process:ProcessFunction.java:39, org.apache.thrift.TBaseProcessor:process:TBaseProcessor.java:39, org.apache.hive.service.auth.TSetIpAddressProcessor:process:TSetIpAddressProcessor.java:56, org.apache.thrift.server.TThreadPoolServer$WorkerProcess:run:TThreadPoolServer.java:286, java.util.concurrent.ThreadPoolExecutor:runWorker:ThreadPoolExecutor.java:1145, java.util.concurrent.ThreadPoolExecutor$Worker:run:ThreadPoolExecutor.java:615, java.lang.Thread:run:Thread.java:745, *org.apache.hadoop.hive.ql.parse.ParseException:line 1:30 cannot recognize input near 'AS' ':' 'rowid' in selection target:22:6, org.apache.hadoop.hive.ql.parse.ParseDriver:parse:ParseDriver.java:205, org.apache.hadoop.hive.ql.parse.ParseDriver:parse:ParseDriver.java:166, org.apache.hadoop.hive.ql.Driver:compile:Driver.java:437, org.apache.hadoop.hive.ql.Driver:compile:Driver.java:320, org.apache.hadoop.hive.ql.Driver:compileInternal:Driver.java:1219, org.apache.hadoop.hive.ql.Driver:compileAndRespond:Driver.java:1213, org.apache.hive.service.cli.operation.SQLOperation:prepare:SQLOperation.java:146], sqlState:42000, errorCode:40000, errorMessage:Error while compiling statement: FAILED: ParseException line 1:30 cannot recognize input near 'AS' ':' 'rowid' in selection target), Query: SELECT ROW_NUMBER() OVER() AS :rowid:, * FROM input. at com.simba.hiveserver2.hivecommon.api.HS2Client.executeStatementInternal(Unknown Source) at com.simba.hiveserver2.hivecommon.api.HS2Client.executeStatement(Unknown Source) at com.simba.hiveserver2.hivecommon.dataengine.HiveJDBCNativeQueryExecutor.executeQuery(Unknown Source) at com.simba.hiveserver2.hivecommon.dataengine.HiveJDBCNativeQueryExecutor.(Unknown Source) at com.simba.hiveserver2.hivecommon.dataengine.HiveJDBCDataEngine.prepare(Unknown Source) at com.simba.hiveserver2.jdbc.common.SStatement.executeNoParams(Unknown Source) at com.simba.hiveserver2.jdbc.common.SStatement.executeQuery(Unknown Source)

有人知道吗?我尝试在字符串中添加 ' 和\",但它们都给出错误。

最佳答案

列名称中的特殊字符应使用反引号 (`) 字符括起来:

new StringBuilder("SELECT ROW_NUMBER() OVER() as `"+rowid+"`, * FROM "+tableName).toString();

阅读Supporting Quoted Identifiers in Column Names了解更多详情。

关于java - 如何使用 HIVE JDBC 驱动程序在列名中使用特殊字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40292192/

相关文章:

java - 如何从java中的url下载图像?

java - 无法从 Object 转换为 char

java - 驱动程序 :com. mysql.jdbc.Driver@2b7374f8 为 URL 返回 null

java - Statement.executeQuery() 为 SELECT 语句返回什么

java.lang.ClassNotFoundException : com. mysql.jdbc.Driver。

sql - 需要对一列进行分组添加以获取Hive中另一列的计数

java - Fedora 上的 Intellij IDEA

java - 程序会将整个 .csv 文件读取到一个 Set 中,并能够通过时间、IP 地址和互联网访问端口找到违规员工

mysql - 使用 MySQL 作为 Metastore 安装 hive-0.7.1 后无法创建表

hive - 使用 hive 连接到远程服务器