无法使用主键创建配置单表

时间:2015-02-16 08:12:23

标签: hadoop hive hiveql

我无法使用主键在hive中创建外部表。以下是示例代码:

hive> create table exmp((name string),primary key(name));

这会返回以下错误消息:

  

NoViableAltException(278 @ [])at   或者是.gap。发现。   org.apache.hadoop.hive.ql.parse.HiveParser.identifier(HiveParser.java:35977)           at org.apache.hadoop.hive.ql.parse.HiveParser.columnNameType(HiveParser.java:31169)           在org.apache.hadoop.hive.ql.parse.HiveParser.columnNameTypeList(HiveParser.java:29373)           在org.apache.hadoop.hive.ql.parse.HiveParser.createTableStatement(HiveParser.java:4439)           在org.apache.hadoop.hive.ql.parse.HiveParser.ddlStatement(HiveParser.java:2084)           在org.apache.hadoop.hive.ql.parse.HiveParser.execStatement(HiveParser.java:1344)           在org.apache.hadoop.hive.ql.parse.HiveParser.statement(HiveParser.java:983)           在org.apache.hadoop.hive.ql.parse.ParseDriver.parse(ParseDriver.java:190)           在org.apache.hadoop.hive.ql.Driver.compile(Driver.java:434)           在org.apache.hadoop.hive.ql.Driver.compile(Driver.java:352)           在org.apache.hadoop.hive.ql.Driver.compileInternal(Driver.java:995)           在org.apache.hadoop.hive.ql.Driver.runInternal(Driver.java:1038)           在org.apache.hadoop.hive.ql.Driver.run(Driver.java:931)           在org.apache.hadoop.hive.ql.Driver.run(Driver.java:921)           在org.apache.hadoop.hive.cli.CliDriver.processLocalCmd(CliDriver.java:268)           在org.apache.hadoop.hive.cli.CliDriver.processCmd(CliDriver.java:220)           在org.apache.hadoop.hive.cli.CliDriver.processLine(CliDriver.java:422)           在org.apache.hadoop.hive.cli.CliDriver.executeDriver(CliDriver.java:790)           在org.apache.hadoop.hive.cli.CliDriver.run(CliDriver.java:684)           在org.apache.hadoop.hive.cli.CliDriver.main(CliDriver.java:623)           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)           at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)           在java.lang.reflect.Method.invoke(Method.java:483)           在org.apache.hadoop.util.RunJar.main(RunJar.java:212)FAILED:ParseException行1:18无法识别'附近的输入('' name'   '串'在列规范

请帮帮我。

7 个答案:

答案 0 :(得分:2)

很抱歉,如果你有任何其他与hive table相关的功能,那么hive中没有主键

CREATE DATABASE [IF NOT EXISTS] userdb;

SHOW DATABASES;

DROP DATABASE IF EXISTS userdb;

DROP DATABASE IF EXISTS userdb CASCADE;

CREATE TABLE IF NOT EXISTS employee ( eid int, name String,
 salary String, destination String)
 COMMENT "Employee details"
 ROW FORMAT DELIMITED
 FIELDS TERMINATED BY "\t"
 LINES TERMINATED BY "\n"
 STORED AS TEXTFILE;  

LOAD DATA LOCAL INPATH '/home/user/sample.txt'
    OVERWRITE INTO TABLE employee;

ALTER TABLE employee RENAME TO emp;

desc tablename;

ALTER TABLE employee CHANGE name ename String;
hive> ALTER TABLE employee CHANGE salary salary Double;

ALTER TABLE employee ADD COLUMNS ( 
    dept STRING COMMENT 'Department name');    

ALTER TABLE employee REPLACE COLUMNS ( 
    eid INT empid Int, 
    ename STRING name String);

DROP TABLE IF EXISTS employee;

SHOW TABLES;

SELECT * FROM employee WHERE Salary>40000 && Dept=TP;  

CREATE VIEW emp_30000 AS
    SELECT * FROM employee
    WHERE salary>30000;

DROP VIEW emp_30000;  

答案 1 :(得分:2)

较旧版本的配置单元不支持主键,但此支持已在2.1.0版本的配置单元中添加。所以这是示例查询

CREATE TABLE table_name ( id int, name string, dept string, primary key(id) disable novalidate );

PS:如何获取Hive版本

hive --version Hive 1.2.1000.x.x.x.xxxx-x

或进入直线并击中

beeline Beeline version 1.2.1000.x.x.x.xxxx-x by Apache Hive

答案 2 :(得分:1)

来自官方网页:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL

他们在语法中包含了创建表格:

constraint_specification:
  : [, PRIMARY KEY (col_name, ...) DISABLE NOVALIDATE ]
    [, CONSTRAINT constraint_name FOREIGN KEY (col_name, ...) REFERENCES 
table_name(col_name, ...) DISABLE NOVALIDATE .

这意味着我们可以在hive中创建带主键的表

版本信息

从Hive 2.1.0(HIVE-13290)开始。

Hive包括对未经验证的主键和外键约束的支持。 当存在约束时,某些SQL工具会生成更有效的查询。 由于未验证这些约束,因此上游系统需要确保 数据完整性在加载到Hive之前。 例如:

create table pk(id1 integer,id2 integer,

主键(id1,id2)禁用novalidate);

create table fk(id1 integer,id2 integer,

约束c1外键(id1,id2)引用pk(id2,id1)禁用 NOVALIDATE);

答案 3 :(得分:0)

我们在hive中没有任何主键和外键。删除主键并执行。

create table exmp(name string);

答案 4 :(得分:0)

首先,蜂巢中的德比没有钥匙 其次,某些密钥可能会与hive中的关键字重复显示您必须为它们添加单引号。 对于我的情况,我得到查询错误

  create table NYSE (exchange String,stock_symbol String,stock_date String,stock_price_open double, stock_price_high double, stock_price_low double, stock_price_close double, stock_volume double, stock_price_adj_close double) row format delimited fields terminated by ‘,’;

应该是

  create table NYSE (`exchange` String,stock_symbol String,stock_date String,stock_price_open double, stock_price_high double, stock_price_low double, stock_price_close double, stock_volume double, stock_price_adj_close double) row format delimited fields terminated by ',';

答案 5 :(得分:0)

Hive中没有主键概念,因为它不是数据库,并且在Hive操作中基于文件而不是基于记录。

尝试一下

create table table_name (
    row_number() over() as rowId     operation
    name string,
    age Int,
    address string) row format delimited fields terminated by ',' stored as textfile;

此处将rowId视为唯一并执行操作。

答案 6 :(得分:-4)

试试这个

create table exmp(name string,primary key(name));