hive prepared语句set date函数工作不正常

xoefb8l8  于 2021-06-26  发布在  Hive
关注(0)|答案(1)|浏览(370)

我有配置单元表,并且通过编写准备好的语句查询,使用jdbc驱动程序填充数据,如下所示。

Connection con = HiveConnectionMgr.getConnection();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            java.util.Date yourDate= null;
            try {
                yourDate = sdf.parse("2018-09-02");
            } catch (ParseException e1) {
                e1.printStackTrace();
            }
            String sql ="select * from txns_fact where txndate=?";
            PreparedStatement ps = con.prepareStatement(sql);
            System.out.println(sql+new java.sql.Date(yourDate.getTime()));
            java.sql.Date d = new java.sql.Date(yourDate.getTime());
            ps.setDate(1, d);  //here
            ResultSet rs = ps.executeQuery(); //not working
            while(rs.next()) {
                System.out.println("cameinnnnnnn");
                System.out.println(rs.getString(1)+"--"+rs.getTimestamp(2)+"--"+rs.getString(3));
            }

            sql = "insert into table txnaggr_fact partition(txninterval=?) values('a','b','c',4,'Test')";

            ps = con.prepareStatement(sql);

            ps.setDate(1, d); //here
            System.out.println("sql="+sql);

            ps.executeUpdate(); //throws exception

            System.out.println("inserted");

        } catch(Exception e) {
            e.printStackTrace();
        }

在插入过程中,我得到以下异常。

sql=insert into table txnaggr_fact partition(txninterval=?) values('a','b','c',4,'Test')

org.apache.hive.service.cli.HiveSQLException: Error while compiling statement: FAILED: ParseException line 1:57 mismatched input '-' expecting ) near '2018' in insert clause
        at org.apache.hive.jdbc.Utils.verifySuccess(Utils.java:256)
        at org.apache.hive.jdbc.Utils.verifySuccessWithInfo(Utils.java:242)
        at org.apache.hive.jdbc.HiveStatement.execute(HiveStatement.java:254)
        at org.apache.hive.jdbc.HiveStatement.executeUpdate(HiveStatement.java:406)
        at org.apache.hive.jdbc.HivePreparedStatement.executeUpdate(HivePreparedStatement.java:119)
        at com.cw.na.vos.DateTest.main(DateTest.java:50)
        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.apache.spark.deploy.JavaMainApplication.start(SparkApplication.scala:52)
        at org.apache.spark.deploy.SparkSubmit$.org$apache$spark$deploy$SparkSubmit$$runMain(SparkSubmit.scala:879)
        at org.apache.spark.deploy.SparkSubmit$.doRunMain$1(SparkSubmit.scala:197)
        at org.apache.spark.deploy.SparkSubmit$.submit(SparkSubmit.scala:227)
        at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:136)
        at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala)
Caused by: org.apache.hive.service.cli.HiveSQLException: Error while compiling statement: FAILED: ParseException line 1:57 mismatched input '-' expecting ) near '2018' in insert clause
        at org.apache.hive.service.cli.operation.Operation.toSQLException(Operation.java:380)
        at org.apache.hive.service.cli.operation.SQLOperation.prepare(SQLOperation.java:206)
        at org.apache.hive.service.cli.operation.SQLOperation.runInternal(SQLOperation.java:290)
        at org.apache.hive.service.cli.operation.Operation.run(Operation.java:320)
        at org.apache.hive.service.cli.session.HiveSessionImpl.executeStatementInternal(HiveSessionImpl.java:530)
        at org.apache.hive.service.cli.session.HiveSessionImpl.executeStatementAsync(HiveSessionImpl.java:517)
        at org.apache.hive.service.cli.CLIService.executeStatementAsync(CLIService.java:310)
        at org.apache.hive.service.cli.thrift.ThriftCLIService.ExecuteStatement(ThriftCLIService.java:530)
        at org.apache.hive.service.rpc.thrift.TCLIService$Processor$ExecuteStatement.getResult(TCLIService.java:1437)
        at org.apache.hive.service.rpc.thrift.TCLIService$Processor$ExecuteStatement.getResult(TCLIService.java:1422)
        at org.apache.thrift.ProcessFunction.process(ProcessFunction.java:39)
        at org.apache.thrift.TBaseProcessor.process(TBaseProcessor.java:39)
        at org.apache.hive.service.auth.TSetIpAddressProcessor.process(TSetIpAddressProcessor.java:56)
        at org.apache.thrift.server.TThreadPoolServer$WorkerProcess.run(TThreadPoolServer.java:286)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.RuntimeException: org.apache.hadoop.hive.ql.parse.ParseException:line 1:57 mismatched input '-' expecting ) near '2018' in insert clause
        at org.apache.hadoop.hive.ql.parse.ParseDriver.parse(ParseDriver.java:211)
        at org.apache.hadoop.hive.ql.parse.ParseUtils.parse(ParseUtils.java:77)
        at org.apache.hadoop.hive.ql.parse.ParseUtils.parse(ParseUtils.java:70)
        at org.apache.hadoop.hive.ql.Driver.compile(Driver.java:468)
        at org.apache.hadoop.hive.ql.Driver.compileInternal(Driver.java:1317)
        at org.apache.hadoop.hive.ql.Driver.compileAndRespond(Driver.java:1295)
        at org.apache.hive.service.cli.operation.SQLOperation.prepare(SQLOperation.java:204)
        ... 15 more

我无法理解为什么会出现异常,但是如果我使用set string并通过将其转换为string来设置date参数,那么这就行了。有人能告诉我怎么做吗。
提前谢谢。

7cjasjjr

7cjasjjr1#

问题是hive的日期类型并不是java的日期类型,而是字符串类型。如果您查看hive的dateobjectinspector,就不会看到java的日期 org.apache.hadoop.hive.common.type.Date . 我建议你使用hadoop的date。

相关问题