ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Hive中新建表

2018-09-27 16:05:07  阅读:340  来源: 互联网

标签:


最基本的建表语句:

create table student(
id string comment '学号',
name string comment '姓名',
sex string comment '性别',
age string comment '年龄'
) comment '学生表';
show create table student;
CREATE TABLE `student`(
  `id` string COMMENT '学号', 
  `name` string COMMENT '姓名', 
  `sex` string COMMENT '性别', 
  `age` string COMMENT '年龄')
COMMENT '学生表'
ROW FORMAT SERDE 
  'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe' 
STORED AS INPUTFORMAT 
  'org.apache.hadoop.mapred.TextInputFormat' 
OUTPUTFORMAT 
  'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION
  'hdfs://mycluster/user/hive/warehouse/gld.db/student'
TBLPROPERTIES (
  'COLUMN_STATS_ACCURATE'='{\"BASIC_STATS\":\"true\"}', 
  'numFiles'='0', 
  'numRows'='0', 
  'rawDataSize'='0', 
  'totalSize'='0', 
  'transient_lastDdlTime'='1537867370')

插入样本数据:

insert into student values('1','孙悟空','男','100');
insert into student values('2','明世隐','男','101');
insert into student values('3','高渐离','男','102');
insert into student values('4','孙尚香','女','103');
insert into student values('5','安琪拉','女','104');
$ hdfs dfs -ls /user/hive/warehouse/gld.db/student
Found 5 items
-rwxrwxrwx   3 gld supergroup         20 2018-09-25 18:15 /user/hive/warehouse/gld.db/student/000000_0
-rwxrwxrwx   3 gld supergroup         20 2018-09-25 18:18 /user/hive/warehouse/gld.db/student/000000_0_copy_1
-rwxrwxrwx   3 gld supergroup         20 2018-09-25 18:18 /user/hive/warehouse/gld.db/student/000000_0_copy_2
-rwxrwxrwx   3 gld supergroup         20 2018-09-25 18:19 /user/hive/warehouse/gld.db/student/000000_0_copy_3
-rwxrwxrwx   3 gld supergroup         20 2018-09-25 18:19 /user/hive/warehouse/gld.db/student/000000_0_copy_4
$ hdfs dfs -cat /user/hive/warehouse/gld.db/student/000000_0
1孙悟空男100

$ hdfs dfs -cat /user/hive/warehouse/gld.db/student/000000_0_copy_1
2明世隐男101
create table student(
id string comment '学号',
name string comment '姓名',
sex string comment '性别',
age string comment '年龄'
) comment '学生表'
row format delimited
fields terminated by '\t';

以HBase作为存储方式建表

建立外部表:这个针对HBase中已有的表

HBase:

hbase> create 'gld:student', 'cf1'

Hive:

SET hbase.zookeeper.quorum=zkNode1,zkNode2,zkNode3;

CREATE DATABASE gld;
USE gld;
CREATE EXTERNAL TABLE student(
id STRING,
name STRING,
sex STRING,
age STRING
) 
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key, cf1:name, cf1:sex, cf1:age")
TBLPROPERTIES("hbase.table.name" = "gld:student");

hbase.zookeeper.quorum:设定HBase中ZooKeeper的地址,格式:ip:port,ip:port,ip:port,端口默认2181,可不填。
hbase.columns.mapping:映射HBase中对应的列,格式:列族名:列名,:key表示HBase中的rowkey。
hbase.table.name:映射HBase中对应的表名,格式:命名空间:表名。

这样之后,就可以在Hive中通过SQL语句操作HBase中的表了:insert,delete,update,select。

注意点:

  1. 当HBase已有这个表时,只能通过建外部表进行映射。不能建内部表,否则报以下错误:
    FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. MetaException(message:MetaException(message:Table gld:student already exists within HBase; use CREATE EXTERNAL TABLE instead to register it in Hive.)
  2. 因为在Hive中这个表是外部表,所以drop命令会删除Hive中的表,但不会删除HBase中的表。
  3. 如果建立外部表时这个表在HBase中不存在,则报以下错误:FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. MetaException(message:MetaException(message:HBase table gld:student doesn't exist while the table is declared as xternal table.)

建立内部表:这个针对HBase中并没有这个表

CREATE TABLE student(
id STRING,
name STRING,
sex STRING,
age STRING
) 
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key, cf1:name, cf1:sex, cf1:age")
TBLPROPERTIES("hbase.table.name" = "gld:student");

注意点:

  1. drop table student命令会同时删除Hive中的表和HBase中表!慎重!因此通常都是用第一种方式建立外部表。

标签:
来源: https://blog.csdn.net/guolindonggld/article/details/82842734

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有