Hive安装和使用
一.安装:
1.上传hive解压到到自己的指定目录 /opt/modules/apache tar -zxf apach-hive-1.2.1-bin.tar.gz -C /opt/modules/apache
2.重新给hive命名简单 mv apache -hive -1.2.1-bin hive -1.2.1(可选)
3.cd hive-1.2.1 进入到hive的安装目录
4.cd conf/
5. 把hive -default.xml.template 重命名 mv hive -default.xml.template hive -env.sh
6.修改配置
打开conf目录下的hive-env.sh
HADOOP_HOME=/opt/modules/apache/hadoop-2.7.3(Hadoop的安装目录)
export HIVE_CONF_DIR=/opt/modules/apache/hive-1.2.1/conf(Hive的conf目录)
7.启动hive之前:
先把hadoop 的相关服务开启:sbin/start-all.sh ()历史服务不用开启;
2.重新给hive命名简单 mv apache -hive -1.2.1-bin hive -1.2.1(可选)
3.cd hive-1.2.1 进入到hive的安装目录
4.cd conf/
5. 把hive -default.xml.template 重命名 mv hive -default.xml.template hive -env.sh
6.修改配置
打开conf目录下的hive-env.sh
HADOOP_HOME=/opt/modules/apache/hadoop-2.7.3(Hadoop的安装目录)
export HIVE_CONF_DIR=/opt/modules/apache/hive-1.2.1/conf(Hive的conf目录)
7.启动hive之前:
先把hadoop 的相关服务开启:sbin/start-all.sh ()历史服务不用开启;
创建元数据存储文件和权限修改:
bin/hdfs dfs -mkdir /tmp
bin/hdfs dfs -mkdir /user/hive/warehouse
bin/hdfs dfs -chmod g+w /tmp
bin/hdfs dfs -chmod g +w /user/hive/warehouse
8. 复制模板: cp hive-default.xml.template hive-site.xml
先删除原有的3000多行,把hive.metastore.warehouse.dir配置进去
<property>
<name>hive.metastore.warehouse.dir</name>
<value>/user/hive/warehouse</value>
<description>location of default database for the warehouse</description>
</property>
-------------------------------------------------------------------------------------------------------------------
bin/hdfs dfs -mkdir /tmp
bin/hdfs dfs -mkdir /user/hive/warehouse
bin/hdfs dfs -chmod g+w /tmp
bin/hdfs dfs -chmod g +w /user/hive/warehouse
8. 复制模板: cp hive-default.xml.template hive-site.xml
先删除原有的3000多行,把hive.metastore.warehouse.dir配置进去
<property>
<name>hive.metastore.warehouse.dir</name>
<value>/user/hive/warehouse</value>
<description>location of default database for the warehouse</description>
</property>
-------------------------------------------------------------------------------------------------------------------
二.Hive客户端的基础语句:
1、进入到hive的客户端:bin/hive
2、查看数据库:show databases;
3、创建数据库:create database test;
4、进入到数据库:use test;
5、查看表:show tables;
6、数据类型:
tinyint、smallint、int、bigint -》int
float、double、date
string、vachar、char -》string
7、create table hive_table(
id int,
name string
);
8.加载数据:
load data local inpath '/opt/datas/hive_test.txt ' into table hive_table;
local :指定本地的数据文件存放路径
不加local:指定数据在hdfs的路径
在hive目录下创建文件 vi hive_test.txt
把创建的文件剪切到datas目录下 mv hive_test.txt /opt/datas/
9.查询语句:
select *from hive_table;
10.hive 的默认数据分隔符是\001,已就是^A,分隔符" ","," “\t”等等
如果说数据的分隔符与表的数据分隔符不一致的话,读取数据为null
ctrl+v 然后按ctrl+a就会出来^A(\001)
create table row_table(
id int ,
name string
)ROW FORMAT DELIMITED FIELDS TERMINATED BY " ";
load data local inpath ' /opt/datas/hive_test.txt' into table row_table;
三 、hive在hdfs上的文件结构
1、进入到hive的客户端:bin/hive
2、查看数据库:show databases;
3、创建数据库:create database test;
4、进入到数据库:use test;
5、查看表:show tables;
6、数据类型:
tinyint、smallint、int、bigint -》int
float、double、date
string、vachar、char -》string
7、create table hive_table(
id int,
name string
);
8.加载数据:
load data local inpath '/opt/datas/hive_test.txt ' into table hive_table;
local :指定本地的数据文件存放路径
不加local:指定数据在hdfs的路径
在hive目录下创建文件 vi hive_test.txt
把创建的文件剪切到datas目录下 mv hive_test.txt /opt/datas/
9.查询语句:
select *from hive_table;
10.hive 的默认数据分隔符是\001,已就是^A,分隔符" ","," “\t”等等
如果说数据的分隔符与表的数据分隔符不一致的话,读取数据为null
ctrl+v 然后按ctrl+a就会出来^A(\001)
create table row_table(
id int ,
name string
)ROW FORMAT DELIMITED FIELDS TERMINATED BY " ";
load data local inpath ' /opt/datas/hive_test.txt' into table row_table;
三 、hive在hdfs上的文件结构
/user/hive/warehouse /test.db /row_table /hive_test.txt
/user/hive/warehouse hive元数据在hdfs上的路径 默认数据仓库的位置
/test.db 数据库目录
/row_table 表目录
/hive_test.txt表的数据文件
/user/hive/warehouse hive元数据在hdfs上的路径 默认数据仓库的位置
/test.db 数据库目录
/row_table 表目录
/hive_test.txt表的数据文件