天天看点

大数据开发-第4课hive基本操作

作者:anjunact
### 1.介绍
|              | Hive                | RDBMS                  |
| ------------ | ------------------- | ---------------------- |
| 查询语言     | HQL                 | SQL                    |
| 数据存储     | HDFS                | raw device or local FS |
| 执行         | mapReduce,tez,spark | excutor                |
| 延时         | 高                  | 低                     |
| 处理数据规模 | 大                  | 小                     |
| 索引         | 0.8版后加入位图索引 | 有复杂索引             |
####  1.1 hive DDL:数据定义言

hive语句注意事项

* Sql语言大小写不敏感
* sql可以写一行或多行
* 关键字不能缩写,不能分行
* 各子句一般要分行
* -- 为注释

#####  1.1.1  创建删除数据库

* 创建数据库

```
create database myhive;
```

* 查看数据库

  ```
  show databases ;
  ```

* 删除数据库

  ```
  drop database if exists myhive;
  drop database myhive cascade;
  ```

* 进入数据

  ```
  use myhive;
  ```

##### 1.1.2创建表--类型https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Types

* 数字类型
    * tinyint
    * smarllint
    * int
    * bigint
    * float
    * double
    * decimal  长度不定
* 时间时间
    * TIMESTAMP
    * DATE
    * INTERVAL
* 字符串
    * string
    * varchar 长度不定
    * char
* misc
    * boolean
    * binary
* 复合类型
    * array
    * map
    * struct
    * uniontype

### 1.2  hive建表https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL#LanguageManualDDL-CreateTable
* like 充许复制现有表结构,不复制数据

|          | 内部表                   | 外部表                                |
| -------- | ------------------------ |------------------------------------|
| 数据管理 | hive管理                 | HDFS管理                             |
| 存储位置 | /usr/hive/warehouse      | LOCATION指定,默认位置/usr/hive/warehouse |
| 删除表   | 直接删除元数据及存储数据 | 只删除元数据                             |
| 修改表   | 修改直接同步给元数据     | 需要修复(msck repair table table_name) |

```
create database myhive;
create table test_create_table(id int,name string);
desc test_create_table;
show create table test_create_table;
-- open http://192.168.2.10:50070/
insert into test_create_table values (1,'test');
select * from test_create_table;
create external table test_extern(id int ,name string);
create external table test_extern_1(id int ,name string) location '/datas/test_extern_1';
insert into test_extern_1 values (1,'test');
select * from test_extern_1;
-- like 建表
create table test_like like test_create_table;
desc test_like;
select * from test_like;
-- comment
create table test_com(id int comment '学生ID',name string comment '学生姓名') comment '学生表';
show create table test_com;
-- as 查询建表
create table bySelect as select * from test_create_table;
desc bySelect;
select * from bySelect;

```

big data tool 连接hdfs问题
解决办法
 https://cwiki.apache.org/confluence/display/HADOOP2/WindowsProblems

github仓库
 https://github.com/cdarlint/winutils/tree/master/