1.初始化元数据

bin/schematool -dbType derby -initSchema;#元数据是dedrby
bin/schematool -initSchema -dbType mysql - verbose;#元数据是mysql

配置:

<!-- jdbc 连接的 URL -->
<property>
<name>javax.jdo.option.ConnectionURL</name>
<value>jdbc:mysql://hadoop1:3306/metastore?useSSL=false</value>
</property>

<!-- jdbc 连接的 Driver-->
<property>
<name>javax.jdo.option.ConnectionDriverName</name>
<value>com.mysql.jdbc.Driver</value>
</property>


<!-- jdbc 连接的 username-->
<property>
<name>javax.jdo.option.ConnectionUserName</name>
<value>root</value>
</property>

<!-- jdbc 连接的 password -->
<property>
<name>javax.jdo.option.ConnectionPassword</name>
<value>root</value>
</property>

<!-- Hive 元数据存储版本的验证 -->
<property>
<name>hive.metastore.schema.verification</name>
<value>false</value>
</property>

<!--元数据存储授权-->
<property>
<name>hive.metastore.event.db.notification.api.auth</name>
<value>false</value>
</property>

<!-- Hive 默认在 HDFS 的工作目录 -->
<property>
<name>hive.metastore.warehouse.dir</name>
<value>/user/hive/warehouse</value>
</property>
</configuration>

<!-- 指定存储元数据要连接的地址 使用元数据服务的方式访问 Hive 
启动命令 hive --service metastore  -->
<property>
<name>hive.metastore.uris</name>
<value>thrift://hadoop2:9083</value>
</property>

<!-- 使用 JDBC 方式访问 Hive指定 hiveserver2 连接的 host 启动命令
 bin/hive --service hiveserver2 
 客户端连接 ./beeline  ;
! connect jdbc:hive2://hadoop2:10000;  如果报root is not allowed 需开启webhdfs及设置代理用户-->
<property>
<name>hive.server2.thrift.bind.host</name>
<value>hadoop102</value>
</property>

<!-- 使用 JDBC 方式访问 Hive指定 hiveserver2 连接的端口号 -->
<property>
<name>hive.server2.thrift.port</name>
<value>10000</value>
</property>
<!--以上为hive-site.xml配置-->

<!-- 配置hdfs.site.xml webhdfs -->
<property>
<name>dfs.hdfs.enabled</name>
<value>true</value>
</property>
<!-- 配置core.site.xml 设置代理用户-->
<property>
<name>hadoop.proxyuser.potter.hosts</name>
<value>*</value>
<name>hadoop.proxyuser.potter.groups</name>
<value>*</value>
</property>

2.hive启动

(1)./hiveserver2
 (2)./hive
 (3)nohup bin/hiveserver2 1>/var/log/hive.log  2>/var/log/hive.err&
 (4)nohup hive --service hiveserver2 2>&1 &;
 (5)hive脚本启动hiveservices.sh    hiveservices.sh start
#!/bin/bash HIVE_LOG_DIR=$HIVE_HOME/logs if [ ! -d $HIVE_LOG_DIR ] then
mkdir -p $HIVE_LOG_DIR
fi
#检查进程是否运行正常,参数 1 为进程名,参数 2 为进程端口
function check_process()
{
pid=$(ps -ef 2>/dev/null | grep -v grep | grep -i $1 | awk '{print
$2}')
ppid=$(netstat -nltp 2>/dev/null | grep $2 | awk '{print $7}' | cut - d '/' -f 1)
echo $pid
[[ "$pid" =~ "$ppid" ]] && [ "$ppid" ] && return 0 || return 1
}
function hive_start()
{
metapid=$(check_process HiveMetastore 9083)
cmd="nohup hive --service metastore >$HIVE_LOG_DIR/metastore.log 2>&1
&"
[ -z "$metapid" ] && eval $cmd || echo "Metastroe 服务已启动" server2pid=$(check_process HiveServer2 10000)
cmd="nohup hiveserver2 >$HIVE_LOG_DIR/hiveServer2.log 2>&1 &"
[ -z "$server2pid" ] && eval $cmd || echo "HiveServer2 服务已启动"
}
function hive_stop()
{
metapid=$(check_process HiveMetastore 9083)
[ "$metapid" ] && kill $metapid || echo "Metastore 服务未启动" server2pid=$(check_process HiveServer2 10000)
[ "$server2pid" ] && kill $server2pid || echo "HiveServer2 服务未启动"
}

case $1 in "start")
hive_start
;;
"stop")
hive_stop
;;
"restart")
hive_stop sleep 2 hive_start
;;
"status")
check_process HiveMetastore 9083 >/dev/null && echo "Metastore 服务运行 正常" || echo "Metastore 服务运行异常"
check_process HiveServer2 10000 >/dev/null && echo "HiveServer2 服务运 行正常" || echo "HiveServer2 服务运行异常"
;;
*)
echo Invalid Args!
echo 'Usage: '$(basename $0)' start|stop|restart|status'
;;
esac

3.derby只支持单session

Caused by: ERROR XSDB6: Another instance of Derby may have already booted the database /opt/module/hive/metastore_db.
at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
at org.apache.derby.iapi.error.StandardException.newException(Unknown

Hive默认使用的元数据库为derby,开启 Hive 之后就会占用元数据库,且不与 其他客户端共享数据,需要将 Hive的元数据换为其他数据库。

4.执行sql

(1)hive --database mydatabase -e 'select * from stu';
(2)hive -e 'select * from mydatabase.stu';
(3) hive -f test.sql
test.sql内容:use database mydatabase;
select * from stu;

查看执行历史sql cat .hivehistory

5.数据类型

基本数据类型

hive数据类型 java数据类型 长度 例子
TINYINT byte 1byte 有符号整数 20
SMALINT short 2byte 有符号整数 20
INT int 4byte 有符号整数 20
BIGINT long 8byte 有符号整数 20
BOOLEAN boolean 布尔类型,true 或者false TRUE FALSE
FLOAT float 单精度浮点数 3.14159
DOUBLE double 双精度浮点数 3.14159
STRING string 字符系列。可以指定字 符集可以使用单引号或者双 引号。 ‘wrold’ “hello”
TIMESTAMP 时间类型
BINARY 字节数组

String 类型相当于数据库的 varchar 类型,是一个可变的字符串,它不能声明其中最多能存储多少个字符,理论上它可以存储 2GB 的字符数

集合数据类型(复杂数据类型)

hive数据类型 描述 例子
STRUCT 可以通过“点”符号访 问元素内容。如果某个列的数据类型是 STRUCT<name STRING, address STRING>,那么第 1 个元素可以通过字段.name来 引用。 struct<street:string, city:string>
MAP MAP 是一组键-值对元组集合,使用数组表示法可以访问数据.键->值对是’first’->’John’和’last’->’Doe’,那么可以 通过字段名[‘last’]获取最后一个元素 map<string, int>
ARRAY 数组是一组具有相同类型和名称的变量的集合。这些变量称为数组的元素,每个数组元素都有一个编号,编号从零开始。数组值为[‘John’, ‘Doe’],那么第 2 个元素可以通过数组名[1]进行引用 array< int>
复杂数据类型允许任意层次的嵌套。

6.分割符

row format delimited fields terminated by ','	--  列分隔符
collection items terminated by '_'  --MAP STRUCT和ARRAY的分隔符(数据分割符号) 
map keys terminated by ':'	    --MAP 中的 key 与 value 的分隔符
lines terminated by '\n';	    --行分隔符

7.内部表和外部表
(1)内部表

create table if not exists t_user 
(id int comment 'id',
name string  comment '姓名'
) comment '用户表';

(2)外部表

create external table if not exists t_stu
(id int ,name string) 
location 'HDFS://xxx';

内部表创建时会创建在相应的库的目录中,外部表创建时会创建在指定的location目录下(如果没有指定则创建在相应的库目录下)
创建表是系统会有两个操作
a.hdfs创建相应目录
b.在元数据中插入描述数据
drop时的区别:
a.都会清除元数据
b.内部表的目录会被删除,外部表的目录不会被删除(数据仓库最底层表会使用外部表)

8.克隆表
(1)不带数据克隆
create table if not exists t_3 like t_1;
(2)带数据克隆
create table if not exists t_2 like t_1 location '/t_user';
create table if not exists t_2 as select * from t_1;

9.修改
alter table t_1 rename to t_112;
alter table t_1 change column id idd int ;
修改idd字段位置:
alter table t_1 change column idd idd int after name;
alter table t_1 change column idd idd int first;
alter table t_1 add column (age int , addr string);

删除字段的话就只能把要保留的字段给写出来,用replace实现,相当于删了重新建了
alter table t_1 replace columns (id int ,name string);

内部表转外部表,true需要大写
alter table t_2 set tblproperties('EXTERNAL'='TRUE');

外部表转内部表,false大小写无所谓
alter table t_2 set tblproperties('EXTERNAL'='false');

posted on 2022-05-16 18:05  xc川  阅读(0)  评论(0)    收藏  举报