目录
建表格式4(常用):create table xxxx as select_statement(SQL语句)
建表格式5:create table xxxx like table_name 只想建表,不需要加载数据
一、基本语法
1、Hive--建表
[external]代表创建外部表
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]
// 指定Hive储存格式:textFile、rcFile、SequenceFile 默认为:textFile
[STORED AS file_format]
| STORED BY 'storage.handler.class.name' [ WITH SERDEPROPERTIES (...) ] (Note: only available starting with 0.6.0)
]
// 指定储存位置
[location hdfs_path]
// 跟外部表配合使用,比如:映射HBase表,然后可以使用HQL对hbase数据进行查询,当然速度比较慢
[TBLPROPERTIES (property_name=property_value, ...)] (Note: only available starting with 0.6.0)
[AS select_statement] (Note: this feature is only available starting with 0.5.0.)
例如:
建表格式1:不指定(location)数据的储存位置
create table students
(
id bigint,
name string,
age int,
gender string,
clazz string
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','; // 必选,指定列分隔符
(row format delimited fields terminated by ',';)
建表格式2:指定location
create table students2
(
id bigint,
name string,
age int,
gender string,
clazz string
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
LOCATION '/input1'; // 指定Hive表的数据的存储位置,一般在数据已经上传到HDFS,想要直接使用,会指定Location,通常Locaion会跟外部表一起使用,内部表一般使用默认的location
建表格式3:指定储存格式.
create table students3
(
id bigint,
name string,
age int,
gender string,
clazz string
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
STORED AS rcfile; //指定储存格式为rcfile,inputFormat:RCFileInputFormat,outputFormat:RCFileOutputFormat,如果不指定,默认为textfile,注意:除textfile以外,其他的存储格式的数据都不能直接加载,需要使用从表加载的方式。
建表格式4(常用):create table xxxx as select_statement(SQL语句)
create table students4 as select * from students2;
建表格式5:create table xxxx like table_name 只想建表,不需要加载数据
create table students5 like students;
2、Hive加载数据
1、使用```hdfs dfs -put '本地数据' 'hive表对应的HDFS目录下
dfs -put /usr/local/data/test.txt /user/hive/warehouse/shujia.db/test;
2、使用 load data inpath
1)将Linux上的本地目录下的文件上传到hive表 需使用 local
load data local inpath '/usr/local/data/test.txt' into table test;
否则会报错如下
2)将HDFS上的/input1目录下面的数据 移动至 students表对应的HDFS目录下,注意是 移动、移动、移动
load data inpath '/data/text.txt' into table test;

3、清空表
truncate table test;
4、 overwrite 覆盖加载
load data local inpath '/usr/local/data/test.txt' overwrite into table test;
5、insert into table xxxx SQL语句 (没有as) 传输给别的格式的hive table到table xxxx
// 将 students表的数据插入到students2 这是复制 不是移动 students表中的表中的数据不会丢失
insert into table test01 select * from test;
// 覆盖插入 把into 换成 overwrite(将原本的数据移除到回收站,传入新的数据)
insert overwrite table test01 select * from test;
3、内部表和外部表的区别
1)、 建表:
// 内部表
create table students_internal
(
id bigint,
name string,
age int,
gender string,
clazz string
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
LOCATION '/input2';
// 外部表
create external table students_external
(
id bigint,
name string,
age int,
gender string,
clazz string
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
LOCATION '/input3';
2)、加载数据:
hive> dfs -put /usr/local/soft/data/students.txt /input2/;
hive> dfs -put /usr/local/soft/data/students.txt /input3/;
##### 删除表:
hive> drop table students_internal;
Moved: 'hdfs://master:9000/input2' to trash at: hdfs://master:9000/user/root/.Trash/Current
OK
Time taken: 0.474 seconds
hive> drop table students_external;
OK
Time taken: 0.09 seconds
hive>
> 可以看出,删除内部表的时候,表中的数据(HDFS上的文件)会被同表的元数据一起删除
> 删除外部表的时候,只会删除表的元数据,不会删除表中的数据(HDFS上的文件)
> 一般在公司中,使用外部表多一点,因为数据可以需要被多个程序使用,避免误删,通常外部表会结合location一起使用
> 外部表还可以将其他数据源中的数据 映射到 hive中,比如说:hbase,ElasticSearch......
> 设计外部表的初衷就是 让 表的元数据 与 数据 解耦
> Managed tables are Hive owned tables where the entire lifecycle of the tables’ data are managed and controlled by Hive. External tables are tables where Hive has loose coupling with the data.
> All the write operations to the Managed tables are performed using Hive SQL commands. If a Managed table or partition is dropped, the data and metadata associated with that table or partition are deleted. The transactional semantics (ACID) are also supported only on Managed tables.
4、Hive--JDBC
##### 启动hiveserver2
```shell
hive --service hiveserver2 &
或者
hiveserver2 &
##### 新建maven项目并添加两个依赖
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.7.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hive/hive-jdbc -->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>1.2.1</version>
</dependency>
##### 编写JDBC代码
import java.sql.*;
public class HiveJDBC {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("org.apache.hive.jdbc.HiveDriver");
Connection conn = DriverManager.getConnection("jdbc:hive2://master:10000/test3");
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery("select * from students limit 10");
while (rs.next()) {
int id = rs.getInt(1);
String name = rs.getString(2);
int age = rs.getInt(3);
String gender = rs.getString(4);
String clazz = rs.getString(5);
System.out.println(id + "," + name + "," + age + "," + gender + "," + clazz);
}
rs.close();
stat.close();
conn.close();
}
}
本文详细介绍了Hive的基本语法,包括不同方式的建表方法,如不指定location、指定location、指定存储格式等。还讲解了如何通过HDFS命令和`load data`语句加载数据。此外,对比了内部表和外部表的区别,强调了外部表在数据管理上的灵活性。最后提到了Hive通过JDBC进行连接操作的可能性。
3万+

被折叠的 条评论
为什么被折叠?



