我正在配置单元表,并通过编写如下所示的预备语句查询使用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并通过将date参数转换为String来设置date参数,那么它将起作用。有人可以告诉我如何进行这项工作。
谢谢。
答案 0 :(得分:0)
问题是Hive的Date类型不是Java的Date类型,实际上是String类型。如果查看Hive的DateObjectInspector,您将看不到Java的日期,而是org.apache.hadoop.hive.common.type.Date
。我建议您使用Hadoop的Date。