0
点赞
收藏
分享

微信扫一扫

HIVE CLI数据库实操

乌龙茶3297 2022-05-09 阅读 54


1、建立基本数据

hadoop@ddai-desktop:~$ sudo vim /score.txt

#一个tab键分割字符
610213 Tom 85 79
610215 John 80 85
610222 Marry 75 87

2、创建score表

hive> CREATE TABLE score (
> sno int, name String,
> java decimal(10,2),
> python decimal(10,2)
> )
> ROW FORMAT DELIMITED
> FIELDS TERMINATED BY '\t'
> LINES TERMINATED BY '\n'
> STORED AS TEXTFILE;

3、导入数据

hive> load data local inpath 'score.txt' overwrite into table score;
Loading data to table default.score
OK
Time taken: 2.435 seconds

4、查询score表

hive> select * from score;
OK
610213 Tom 85.00 79.00
610215 John 80.00 85.00
610222 Marry 75.00 87.00
Time taken: 0.355 seconds, Fetched: 3 row(s)

5、查看hdfs数据

hive> dfs -ls /hive/warehouse/score;
Found 1 items
-rwxr-xr-x 3 hadoop supergroup 54 2021-08-12 13:43 /hive/warehouse/score/score.txt

hive> dfs -cat /hive/warehouse/score/score.txt;

610213 Tom 85 79
610215 John 80 85
610222 Marry 75 87

6、查看元数据信息

hadoop@ddai-master:~$ mysql -hddai-master -uhive -pDai@123456


mysql> use hive
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select TBL_ID,OWNER,TBL_NAME,TBL_TYPE,SD_ID from TBLS;
+--------+--------+----------+---------------+-------+
| TBL_ID | OWNER | TBL_NAME | TBL_TYPE | SD_ID |
+--------+--------+----------+---------------+-------+
| 2 | hadoop | score | MANAGED_TABLE | 2 |
+--------+--------+----------+---------------+-------+
1 row in set (0.01 sec)

mysql> select * from DBS;
+-------+-----------------------+----------------------------------------+---------+------------+------------+
| DB_ID | DESC | DB_LOCATION_URI | NAME | OWNER_NAME | OWNER_TYPE |
+-------+-----------------------+----------------------------------------+---------+------------+------------+
| 1 | Default Hive database | hdfs://ddai-master:9000/hive/warehouse | default | public | ROLE |
+-------+-----------------------+----------------------------------------+---------+------------+------------+
1 row in set (0.00 sec)

7、删除表

drop table score;

8、查看hdfs

dfs -ls /hive/warehouse;

外部表

1、基本数据建立

hadoop@ddai-desktop:~$ more test_ext.txt 
1,master
2,node1
3,node2

2、上传hdfs

hive> dfs -mkdir /hive/warehouse/test_ext;
hive> dfs -put test_ext.txt /hive/warehouse/test_ext;

3、创建外部表

hive> create external table test_external ( 
> id int,
> name string
> )
> row format delimited
> fields terminated by ','
> location '/hive/warehouse/test_ext';
OK
Time taken: 2.24 seconds

4、查询test_external

hive> select * from test_external;
OK
1 master
2 node1
3 node2
Time taken: 0.238 seconds, Fetched: 3 row(s)

5、删除

hive> drop table test_external;

6、查看hdfs

hive> dfs -ls /hive/warehouse/test_ext;
Found 1 items
-rw-r--r-- 3 hadoop supergroup 25 2021-08-12 14:09 /hive/warehouse/test_ext/test_ext.txt

Beeline命令

去hadoop配置文件core-site.xml添加,已经有的不再需要

<property>
<name>hadoop.proxyuser.hadoop.hosts</name>
<value>*</value>
</property>
<property>
<name>hadoop.proxyuser.hadoop.groups</name>
<value>*</value>
</property>
hadoop@ddai-master:~$ hdfs dfs -chmod -R 777 /tmp

连接hiveserver2

hadoop@ddai-desktop:~$ beeline


beeline> !connect jdbc:hive2://ddai-master:10000 hive Dai@123456
Connecting to jdbc:hive2://ddai-master:10000
Connected to: Apache Hive (version 2.3.6)
Driver: Hive JDBC (version 2.3.6)
Transaction isolation: TRANSACTION_REPEATABLE_READ
0: jdbc:hive2://ddai-master:10000>
0: jdbc:hive2://ddai-master:10000> create external table test_external2 ( 
. . . . . . . . . . . . . . . . .> id int,
. . . . . . . . . . . . . . . . .> name string
. . . . . . . . . . . . . . . . .> )
. . . . . . . . . . . . . . . . .> row format delimited
. . . . . . . . . . . . . . . . .> fields terminated by ','
. . . . . . . . . . . . . . . . .> location '/hive/warehouse/test_ext';
No rows affected (0.199 seconds)
0: jdbc:hive2://ddai-master:10000> select * from test_external;
+-------------------+---------------------+
| test_external.id | test_external.name |
+-------------------+---------------------+
| 1 | master |
| 2 | node1 |
| 3 | node2 |
+-------------------+---------------------+
3 rows selected (2.806 seconds)

关闭连接退出

!closeall
!quit



举报

相关推荐

0 条评论