1. HIVE can be started in either of the following ways: 1 Start the HIVE Thrift service

1.1 Direct Hive Startup:

The hive driver is run in apps/apache-hive-1.2.1-bin/hive to start hive

[hadoop@mini1 ~]$CD apps/apache-hive-1.2.1-bin/ [hadoop@mini1 apache-hive-1.2.1-bin]$bin/hiveCopy the code

1.2 Hive Thrift Service:

Use Beeline to connect to hive as a server from another node as follows:

[hadoop@mini1 ~]$CD apps/apache-hive-1.2.1-bin/ [hadoop@mini1 apache-hive-1.2.1-bin]$bin/hiveserver2 // Start as a server firstCopy the code

Reopen a mini1 window

[hadoop@mini1 ~]$CD apps/apache-hive-1.2.1-bin/ [hadoop@mini1 apache-hive-1.2.1-bin]$bin/beeline / Beeline >! Beeline >! connect jdbc:hive2://localhost:10000 Connecting to jdbc:hive2://localhost:10000 Enter username for JDBC: hive2: / / localhost: 10000: hadoop / / the account login server user name for you Enter the password for the JDBC: hive2: / / localhost: 10000: * * * * * * / / password for the server passwordCopy the code

** These are the two startup modes of Hive! 支那

2 Hive command

2.1 Now perform the following operations in the first Hive startup mode:

1 Viewing the database:

show databases;
Copy the code

2 Use database:

Use + database name;Copy the code

3 View table:

show tables;
Copy the code

4 Create inner table

create table sust(id int,name string) row format delimited fields terminated by ','; // Separate information about the data to be imported with commasCopy the code

5 Import data to inner table:

 hadoop  fs -put  sust.tex    /user/hive/warehouse/shizhan03.db/sust
Copy the code

** The imported data content is as follows **

01,liyaozhou
02,fangjingli
03,wangjun
04,liuyang
05,wangkang
06,malin
07,qianghua
08,gaoyixing
09,huanglichang
10,zhaoliangliang
Copy the code

6: Query internal table information:

select * from sust;
Copy the code

2.2 Perform the following operations in the second Hive Startup Mode:

7: Create appearance:

Zero: JDBC: hive2: / / localhost: 10000 > create external table sust1 (int id, name string) / / create a table called sust1 > row format delimited Fields terminated by ',' // Use commas to separate > stored as textfile // Stored as text type > location '/SUST'; // Specify the directory in which the created table residesCopy the code

    

8: Import data to external: The imported data content is the same as that in the inner table

0: jdbc:hive2://localhost:10000> load data local inpath '/home/hadoop/sust.tex' into table sust1; // Inside the quotes is the path where you want to import the data, followed by the table you want to importCopy the code

9: Check the appearance information:

select * from sust;
Copy the code

      

10. The difference between the outside and the inside:

When Hive creates internal tables, data is moved to the path pointed by the data warehouse. If an external table is created, only the path of the data is recorded, and the location of the data is not changed. When a table is deleted, metadata and data of an internal table are deleted together, while metadata of an external table is deleted.Copy the code

Delete inner table:

drop table sust; Appearance: DROP Table SUST1;Copy the code

Create inner table with partition;

Zero: JDBC: hive2: / / localhost: 10000 > create table sustPart (int id, name string) / / create a table called sust1 > partitioned by country (string) // Partition by country > row format delimited fields terminated by ','; // Use commas to separate themCopy the code

                                         

13. Import data from different countries into a table partitioned by country

Data sources are as follows:

01,anbei
02,cangjingkong
03,meihuizi
04,xiaotianyilang
05,taijun
06,xiaoquan
07,riben
08,bendaohuizi
09,guangdao
10,changqi
Copy the code

Data imported to China:

 load data local inpath '/home/hadoop/sust.tex' into table sustPart partition(country='China');
Copy the code

When importing data from another country:

load data local inpath '/home/hadoop/sust.tex.japan' into table sustPart partition(country='Japan');
Copy the code
  1. Check the partition

select * from sustPart;

15. Search for names by partition

select count(1) from sustPart where country='China' group by (name='liyaozhou');
Copy the code
  1. Show partitions + table name;

show partitions sustPartion;

  1. Add partition:

alter table sustPart add partition(country='America');

  1. Reduce partitions:

alter table sustPart drop partition(country='America');

  1. Alter table name alter table name;

Alter table rename to alter table rename;