将本地/opt/module/data/student.txt 这个目录下的数据导入到 hive 的 student(id int, name string)表中。
1.数据准备
在/opt/module/data 这个目录下准备数据
mkdir data
在/opt/module/datas/目录下创建 student.txt 文件并添加数据
touch student.txt 注意以 tab 键间隔
vi student.txt
1001 zhangshan
1002 lishi
1003 zhaoliu
启动 hive
[atguigu@hadoop102 hive]$ bin/hive
显示数据库
hive> show databases;
使用 default 数据库 hive> use default;
显示 default 数据库中的表 hive> show tables;
删除已创建的 student 表 hive> drop table student;
创建 student 表, 并声明文件分隔符’\t’
加载/opt/module/data/student.txt 文件到 student 数据库表中。
Hive 查询结果
遇到的问题
再打开一个客户端窗口启动 hive,会产生 java.sql.SQLException 异常。
[atguigu@hadoop102 datas]$ touch student.txt
[atguigu@hadoop102 datas]$ vi student.txt
1001 zhangshan
1002 lishi
1003 zhaoliu
hive> create table student(id int, name string) ROW FORMAT DELIMITED FIELDS TERMINATED
BY '\t';
hive> load data local inpath '/opt/module/data/student.txt' into table student;
hive> select * from student; OK 1001 zhangshan 1002 lishi 1003 zhaoliu Time taken: 0.266 seconds, Fetched: 3 row(s)
Exception in thread "main" java.lang.RuntimeException: java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClien |
tat org.apache.hadoop.hive.ql.session.SessionState.start(Session State.java:522) at org.apache.hadoop.hive.cli.CliDriver.run(CliDriver.java:677) at org.apache.hadoop.hive.cli.CliDriver.main(CliDriver.java:621 ) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAcce |
ssorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMe thodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) Caused by: java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClien t at org.apache.hadoop.hive.metastore.MetaStoreUtils.newInstance( MetaStoreUtils.java:1523) at org.apache.hadoop.hive.metastore.RetryingMetaStoreClient.<in |
it>(RetryingMetaStoreClient.java:86) at org.apache.hadoop.hive.metastore.RetryingMetaStoreClient.get Proxy(RetryingMetaStoreClient.java:132) at org.apache.hadoop.hive.metastore.RetryingMetaStoreClient.get Proxy(RetryingMetaStoreClient.java:104) at org.apache.hadoop.hive.ql.metadata.Hive.createMetaStoreClien t(Hive.java:3005) at org.apache.hadoop.hive.ql.metadata.Hive.getMSC(Hive.java:302 4) at org.apache.hadoop.hive.ql.session.SessionState.start(Session State.java:503) |
原因是,Metastore 默认存储在自带的 derby 数据库中,推荐使用 MySQL 存储 Metastore;