1, data import
1.1 to load data into the table (Load)
1. grammar
hive> load data [local] inpath '/opt/module/datas/student.txt' [overwrite] into table student [partition (partcol1=val1,…)];
(1) load data: loading data represents
(2) local: indicates the data loaded from the local table to the hive (copy); otherwise, the data is loaded into the hive from HDFS table (movement)
(3) inpath: loading data represents the path
(4) overwrite into: covering the table represent existing data, or that additional
(5) into table: where represents the loading tables
(6) student: shows a specific table
(7) partition: represent uploaded to the specified partition
2. Practical operation case
(0) create a table
hive > create table student(id string, name string) row format delimited fields terminated by '\t';
(1) load a local file into the hive
hive > load data local inpath '/opt/module/datas/student.txt' into table default.student;
(2) loading the hive file into HDFS
Upload files to HDFS
hive > dfs -put /opt/module/datas/student.txt /user/atguigu/hive;
Load the data HDFS
hive > load data inpath '/user/atguigu/hive/student.txt' into table default.student;
(3) loading the data in the existing data overlay table
Upload files to HDFS
hive > dfs -put /opt/module/datas/student.txt /user/atguigu/hive;
Load data covering the existing data in the table
hive > load data inpath '/user/atguigu/hive/student.txt' overwrite into table default.student;
1.2 by inserting data into the table query (the Insert)
1. Create a partition table
hive > create table student(id int, name string) partitioned by (month string) row format delimited fields terminated by '\t';
2. Basic data inserted
hive > insert into table student partition(month='201709') values(1,'wangwu');
3. Basic Mode insert (query results based on a single table)
hive > insert overwrite table student partition(month='201708')
select id, name from student where month='201709';
4. Multi-insert mode (query results according to multiple tables)
hive > from student
insert overwrite table student partition(month='201707')
select id, name where month='201709'
insert overwrite table student partition(month='201706')
select id, name where month='201709';
1.3 query statement to create the table and load the data (As Select)
Create a table based on query results (the results of the query will be added to the table in the newly created)
create table if not exists student3 as select id, name from student;
Loading data path through the Location specified when creating a table 1.4
1. Create a table, and specify a location on the hdfs
hive > create table if not exists student5(
id int, name string
)
row format delimited fields terminated by '\t'
location '/user/hive/warehouse/student5';
2. Upload data to the hdfs
hive > dfs -put /opt/module/datas/student.txt /user/hive/warehouse/student5;
3. Query data
hive > select * from student5;
1.5 Import table data to the specified Hive
Note : after the first use export export and then import the data.
hive > import table student2 partition(month='201709') from '/user/hive/warehouse/export/student';
2, data export
2.1 Insert Export
1. The results of the query to the local export
hive > insert overwrite local directory '/opt/module/datas/export/student'
select * from student;
2. The results of the query to a local format export
hive >insert overwrite local directory '/opt/module/datas/export/student1'
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' select * from student;
3. The results of the query is exported to the HDFS (not local)
hive > insert overwrite directory '/user/atguigu/student2'
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
select * from student;
2.2 Hadoop command to export the local
hive > dfs -get /user/hive/warehouse/student/month=201709/000000_0 /opt/module/datas/export/student3.txt;
2.3 Hive Shell command to export
The basic syntax: (hive -f / -e statement or execute script> file)
[root@master hive]$ bin/hive -e 'select * from default.student;' > /opt/module/datas/export/student4.txt;
2.4 Export Export to HDFS
(defahiveult)>export table default.student to '/user/hive/warehouse/export/student';
3, to clear data in the table (Truncate)
Note: Truncate can only remove management table, you can not delete the data in an external table
hive > truncate table student;