Hive安装和使用( 二 )


mysql> grant all on *.* to hive@localhost identified by 'hiveMhxzKhl88!';#将所有数据库的所有表的所有权限赋给hive用户,by后面的是配置hive-site.xml中配置的连接密码mysql> flush privileges;#刷新mysql系统权限关系表初始化数据库
schematool -dbType mysql -initSchema如果没有初始化执行命令的时候会报错
FAILED: HiveException java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.ql.me关于mysql的授权命令
use mysql;#给某个用户授权格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by "密码"; grant all privileges on testDB.* to test@localhost identified by '1234';#如果想指定部分权限给一用户,可以这样来写:grant select,update on testDB.* to test@localhost identified by '1234';#删除某个授权的用户Delete FROM user Where User='test' and Host='localhost';#修改密码update mysql.user set password=password('新密码') where User="test" and Host="localhost";3.hive sql入门3.1 Hive基本数据类型Hive支持基本数据类型和复杂类型, 基本数据类型主要有数值类型(INT、FLOAT、DOUBLE ) 、布尔型和字符串, 复杂类型有三种:ARRAY、MAP 和 STRUCT 。
a.基本数据类型

  • TINYINT: 1个字节
  • SMALLINT: 2个字节
  • INT: 4个字节
  • BIGINT: 8个字节
  • BOOLEAN: TRUE/FALSE
  • FLOAT: 4个字节,单精度浮点型
  • DOUBLE: 8个字节,双精度浮点型
  • STRING 字符串
b.复杂数据类型
  • ARRAY: 有序字段
  • MAP: 无序字段
  • STRUCT: 一组命名的字段
3.2 常用的HiveQL操作命令3.2.1 库操作create database if not exists testdb;#创建数据库show databases;#查看Hive中包含数据库show databases like 'h.*';#查看Hive中以h开头数据库describe databases;#查看hive数据库位置等信息alter database testdb set dbproperties;#为hive设置键值对属性use testdb;#切换到hive数据库下drop database if exists testdb;#删除不含表的数据库drop database if exists testdb cascade;#删除数据库和它中的表3.2.2 表操作1.创建表
CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name[(col_name data_type [COMMENT col_comment], ...)][COMMENT table_comment][PARTITIONED BY (col_name data_type [COMMENT col_comment], ...)][CLUSTERED BY (col_name, col_name, ...)[SORTED BY (col_name [ASC|DESC], ...)] INTO num_buckets BUCKETS][ROW FORMAT row_format][STORED AS file_format][LOCATION hdfs_path]
  • CREATE TABLE 创建一个指定名字的表 。如果相同名字的表已经存在,则抛出异常;用户可以用 IF NOT EXIST 选项来忽略这个异常
  • EXTERNAL 关键字可以让用户创建一个外部表,在建表的同时指定一个指向实际数据的路径(LOCATION)
  • LIKE 允许用户复制现有的表结构,但是不复制数据
  • COMMENT可以为表与字段增加描述
例如,建立一个usr表
create table if not exists testdb.usr(uid string comment 'uid',cuid string comment 'cuid',type string comment 'type' );create table person(name STRING,age INT); create table if not exists testdb.usr2(id int,name string,address string); 2.表查看
show tables in hive;show tables 'u.*';#查看hive中以u开头的表describe hive.usr;#查看usr表相关信息3.表修改
#重命名表alter table usr rename to custom;#修改列信息alter table usr change column pwd password string after address;#增加列alter table usr add columns(hobby string);#删除替换列alter table usr replace columns(uname string);drop table if exists usr1;4.导入数据
建立表,设定分隔符是t
create table if not exists testdb.testhive(unique_id string,uid string,cuid string,create_time int) row format delimited fields terminated by 't';load data local inpath "/home/team/r.txt" overwrite into table testhive;Hive中追加导入数据的4种方式
从本地导入: load data local inpath '/home/st.txt' (overwrite) into table student;
从Hdfs导入: load data inpath '
/user/hive/warehouse/st.txt' (overwrite) into table student;
查询导入: create table student_a as select * from student;(也可以具体查询某项数据)
查询结果导入: insert (overwrite)into table student select * from student_a;
5.导出数据
insert overwrite local directory '/usr/local/hadoop/tmp/stu'select id,name from stu;


推荐阅读