如何将时间戳插入Hive表?

时间:2014-10-22 15:51:29

标签: hive

我有以下内容:

hive> CREATE TABLE foo (bar timestamp) STORED AS ORC;
OK
Time taken: 0.041 seconds

hive> INSERT INTO TABLE foo VALUES ('2014-01-17 00:17:13');
NoViableAltException(26@[])
        at org.apache.hadoop.hive.ql.parse.HiveParser_SelectClauseParser.selectClause(HiveParser_SelectClauseParser.java:742)
        at org.apache.hadoop.hive.ql.parse.HiveParser.selectClause(HiveParser.java:40184)
        at org.apache.hadoop.hive.ql.parse.HiveParser.singleSelectStatement(HiveParser.java:38048)
        at org.apache.hadoop.hive.ql.parse.HiveParser.selectStatement(HiveParser.java:37754)
        at org.apache.hadoop.hive.ql.parse.HiveParser.regularBody(HiveParser.java:37654)
        at org.apache.hadoop.hive.ql.parse.HiveParser.queryStatementExpressionBody(HiveParser.java:36898)
        at org.apache.hadoop.hive.ql.parse.HiveParser.queryStatementExpression(HiveParser.java:36774)
        at org.apache.hadoop.hive.ql.parse.HiveParser.execStatement(HiveParser.java:1338)
        at org.apache.hadoop.hive.ql.parse.HiveParser.statement(HiveParser.java:1036)
        at org.apache.hadoop.hive.ql.parse.ParseDriver.parse(ParseDriver.java:199)
        at org.apache.hadoop.hive.ql.parse.ParseDriver.parse(ParseDriver.java:166)
        at org.apache.hadoop.hive.ql.Driver.compile(Driver.java:408)
        at org.apache.hadoop.hive.ql.Driver.compile(Driver.java:322)
        at org.apache.hadoop.hive.ql.Driver.compileInternal(Driver.java:976)
        at org.apache.hadoop.hive.ql.Driver.runInternal(Driver.java:1041)
        at org.apache.hadoop.hive.ql.Driver.run(Driver.java:912)
        at org.apache.hadoop.hive.ql.Driver.run(Driver.java:902)
        at org.apache.hadoop.hive.cli.CliDriver.processLocalCmd(CliDriver.java:268)
        at org.apache.hadoop.hive.cli.CliDriver.processCmd(CliDriver.java:220)
        at org.apache.hadoop.hive.cli.CliDriver.processLine(CliDriver.java:423)
        at org.apache.hadoop.hive.cli.CliDriver.executeDriver(CliDriver.java:793)
        at org.apache.hadoop.hive.cli.CliDriver.run(CliDriver.java:686)
        at org.apache.hadoop.hive.cli.CliDriver.main(CliDriver.java:625)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.apache.hadoop.util.RunJar.main(RunJar.java:212)
FAILED: ParseException line 1:22 cannot recognize input near 'values' '(' ''2014-01-17 00:17:13'' in select clause

将时间戳插入Hive表的正确方法是什么?

Hive版本是:hive-0.13.0

2 个答案:

答案 0 :(得分:2)

根据Hive Language Manual" INSERT ... VALUES可从Hive 0.14"开始。

所以,你只剩下两个选择:

  1. 从现有表中插入时间戳值或调用函数来获取它(例如from_unixtime(unix_timestamp()),例如,请参阅this answer
  2. 从文件加载数据。

答案 1 :(得分:0)

This kind of writing is correct: insert into table foo; select '2014-01-17 00:17:13' as bar from foo