(作者:陈玓玏)
一、前提
- 部署好了k8s;
- 部署好了hadoop;
二、镜像封装
因为hive是基于hadoop的,而我们已经部署好了hadoop,所以直接通过现有hadoop的镜像来封装hive的镜像。helm上也有可以直接使用的chart,但是版本可能和我们需要的不一样,所以还是自己封装比较好。
首先查一下我们的镜像,因为我的hadoop是用helm部署的,需要先明确一下。
sudo docker images |grep hadoop
用的是3.3.2版本,镜像名是farberg/apache-hadoop。
编写dockerfile如下:
FROM farberg/apache-hadoop:3.3.2
RUN rm -f /etc/localtime && ln -sv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo "Asia/Shanghai">/etc/timezone
RUN export LANG=zh_CN.UTF-8
RUN wget https://mirrors.aliyun.com/apache/hive/hive-3.1.3/apache-hive-3.1.3-bin.tar.gz
ENV HIVE_VERSION 3.1.3
RUN tar -zxvf apache-hive-3.1.3-bin.tar.gz -C /opt/&& mv /opt/apache-hive-3.1.3-bin /opt/hive
ENV HIVE_HOME=/opt/hive
ENV PATH=$HIVE_HOME/bin:$PATH
#这里mysql的jar包的版本要和后面部署的mysql一致
RUN wget https://repo1.maven.org/maven2/com/mysql/mysql-connector-j/8.0.32/mysql-connector-j-8.0.32.jar && mv mysql-connector-j-8.0.32.jar /opt/hive/lib/
WORKDIR /opt/
# Hdfs ports
EXPOSE 500105002050070500755009080209000
# Mapred ports
EXPOSE 19888
#Yarn ports
EXPOSE 8030803180328033804080428088
#Other ports
EXPOSE 497072122
hive的镜像在这里下载比较快:
https://mirrors.aliyun.com/apache/
dockerfile编写好之后,通过以下命令构建镜像:
sudo docker build -t ccr.ccs.tencentyun.com/cube-studio/hadoop-hive:v3.3.2-3.1.3.
镜像构建后通过以下命令推送:
sudo docker push ccr.ccs.tencentyun.com/cube-studio/hadoop-hive:v3.3.2-3.1.3
推送好之后,在cube studio平台上,新建一个pipeline,使用自定义镜像模板,填写镜像为ccr.ccs.tencentyun.com/cube-studio/hadoop-hive:v3.3.2-3.1.3,debug,检查目录对不对,检查环境变量是否正确,在命令行输入hive命令,看是否能正常执行。
都正常则表明镜像是没问题的,可以进行下一步了。
三、部署mysql
这里之前写过了怎么在k8s部署mysql,所以直接用就可以了。文章链接:https://blog.csdn.net/weixin_39750084/article/details/136366800
四、部署hive
apiVersion: v1
kind: ConfigMap
metadata:
name: hive-custom-config-cm
labels:
app: hive
data:
bootstrap.sh:|-
#!/bin/bash
cd /root/bootstrap
# Apply custom config file context
cp hive-site.xml $HIVE_HOME/conf/
cp core-site.xml $HADOOP_HOME/etc/hadoop/
cp yarn-site.xml $HADOOP_HOME/etc/hadoop/
# initSchema
$HADOOP_HOME/bin/hadoop fs -test -e '/user/hive/warehouse'if[! $?-eq 0]; then
$HADOOP_HOME/bin/hadoop fs -mkdir -p /tmp
$HADOOP_HOME/bin/hadoop fs -mkdir -p /user/hive/warehouse
$HADOOP_HOME/bin/hadoop fs -chmod g+w /tmp
$HADOOP_HOME/bin/hadoop fs -chmod g+w /user/hive/warehouse
$HIVE_HOME/bin/schematool -dbType mysql -initSchema --verbose &
#等待初始化运行结束,否则会报错FAILED: HiveException java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClient
#若出现此报错,需要进入hive的pod中,手动执行命令hive --service metastore &
sleep 10
fi
$HIVE_HOME/bin/hiveserver2 &
$HIVE_HOME/bin/hive --service metastore &
cp $HIVE_HOME/conf/hive-env.sh.template $HIVE_HOME/conf/hive-env.sh && echo "export HADOOP_CLIENT_OPTS=\"-Xmx512m -XX:MaxPermSize=1024m \$HADOOP_CLIENT_OPTS\"">> $HIVE_HOME/conf/hive-env.sh
# keep running
sleep infinity
hive-site.xml:|-<?xml version="1.0" encoding="UTF-8" standalone="no"?><?xml-stylesheet type="text/xsl" href="configuration.xsl"?><configuration><property><name>hive.server2.enable.doAs</name><value>false</value></property><property><name>javax.jdo.option.ConnectionUserName</name><value>root</value></property><property><name>javax.jdo.option.ConnectionPassword</name><value>admin</value></property><property><name>javax.jdo.option.ConnectionURL</name><value>jdbc:mysql://mysql-service.infra:3306/metastore?createDatabaseIfNotExist=true&useSSL=false</value></property><property><name>javax.jdo.option.ConnectionDriverName</name><value>com.mysql.jdbc.Driver</value></property><property><name>system:java.io.tmpdir</name><value>/tmp</value></property><property><name>system:user.name</name><value>hive</value></property><property><name>hive.server2.authentication</name><value>NOSASL</value></property><!--设置HDFS地址--><property><name>fs.defaultFS</name><value>hdfs://hadoop-hadoop-hdfs-nn.default:9000</value></property></configuration>
core-site.xml:|-<?xml version="1.0" encoding="UTF-8" standalone="no"?><?xml-stylesheet type="text/xsl" href="configuration.xsl"?><configuration><property><name>fs.defaultFS</name><value>hdfs://hadoop-hadoop-hdfs-nn.default:9000</value></property></configuration>
yarn-site.xml:|-<?xml version="1.0" encoding="UTF-8" standalone="no"?><?xml-stylesheet type="text/xsl" href="configuration.xsl"?><configuration><!-- Site specific YARN configuration properties --><property><name>yarn.resourcemanager.hostname</name><value>hadoop-hadoop-yarn-rm-0.hadoop-hadoop-yarn-rm.default</value></property></configuration>
deployment及service等:
apiVersion: apps/v1
kind: Deployment
metadata:
name: hive
spec:
replicas:1
revisionHistoryLimit:10
selector:
matchLabels:
app: hive
template:
metadata:
labels:
app: hive
spec:
containers:- name: hive
image: ccr.ccs.tencentyun.com/cube-studio/hadoop-hive:v3.3.2-3.1.3
command:["bash","-c","bash /root/bootstrap/bootstrap.sh"]
ports:- containerPort:10000- containerPort:10002- containerPort:9083
env:- name: HADOOP_CONF_DIR
value:/opt/hadoop/etc/hadoop
volumeMounts:- name: hive-custom-config-volume
mountPath:/root/bootstrap
readinessProbe:
initialDelaySeconds:20
periodSeconds:5
tcpSocket:
port:10000
volumes:- name: hive-custom-config-volume
configMap:
name: hive-custom-config-cm
---
kind: Service
apiVersion: v1
metadata:
labels:
app: hive
name: hive-service
spec:
ports:- port:10000
targetPort:10000
name: thrift
- port:10002
targetPort:10002
name: webui
- port:9083
targetPort:9083
name: metastore
selector:
app: hive
type: NodePort
两个奇怪的bug:
部署好之后,我们可以进pod查看一下hive的日志,像我碰到一个情况就是,hive能用,可以查数据库列表,表列表,但不能插入数据,看pod日志发现报错,重启pod时,再次初始化出现错误
Error: Table ‘CTLGS‘ already exists (state=42S01,code=1050)
,需要进入mysql,删除metastore这个库,再重启hive的pod进行初始化。如果再次进入hive时,不能执行命令,报错
java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.ql.metadata.Session
,表示metastore没重启成功,需要进入hive的pod手动执行一下
hive --service metastore &
。
不能插入数据的问题,后来发现是没有配置hdfs的地址,在yaml里面加上这个配置就没问题了!必要的配置项一定不能漏掉!!不然就会出现本地能查到数据,但hdfs中查不到的情况,这样换一台机器就不能用了!
五、验证
登录ui界面,可以验证是否成功。
六、客户端使用hive
- 验证jdk,通过
java -version
验证已安装jdk,但没有设置JAVA_HOME,vi ~/.bashrc
,设置一下JAVA_HOME,export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
,再source ~/.bashrc
让其生效,再次验证,jdk版本只能是8,如果是别的版本,记得更换;jdk最好是用java8,如果版本不对,可以先卸载,再安装正确的版本,再设置环境变量。如果不想总修改环境变量,那么设置/etc/profile比设置~/.bashrc更好。 - 下载hive,跟服务端下载同一个版本:https://mirrors.aliyun.com/apache/hive/hive-3.1.3/apache-hive-3.1.3-bin.tar.gz
- 解压,
tar -zxvf apache-hive-3.1.3-bin.tar.gz
- 编辑配置项:
cd apache-hive-3.1.3-bin/conf
vi hive-site.xml
把下面的配置项粘到hive-site.xml:
<?xml version="1.0" encoding="UTF-8" standalone="no"?><?xml-stylesheet type="text/xsl" href="configuration.xsl"?><configuration><property><name>hive.metastore.warehouse.dir</name><value>/user/hive/warehouse</value></property><property><name>hive.metastore.local</name><value>false</value></property><property><name>hive.metastore.uris</name><value>thrift://hive-service.default:9083</value></property></configuration>
- 配置hive的环境变量
vi /etc/profile
export HIVE_HOME=/mnt/admin/apache-hive-3.1.3-bin
export HADOOP_HOME=/mnt/admin/hadoop-3.3.6
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/jre
export HADOOP_CONF_DIR=/mnt/admin/hadoop-3.3.6/etc/hadoop
export PATH=$PATH::$HIVE_HOME/bin:$HADOOP_HOME/bin:$JAVA_HOME/bin
source /etc/profile
环境变量写在/etc/profile更好,因为~/.bashrc是临时的,重启一下pod就复原了,但/etc/profile是全局的,避免一直要重复设置。
- 配置hadoop的yarn 把/mnt/admin/hadoop-3.3.6/etc/hadoop/yarn-site.xml配置成以下这样:
<configuration><property><name>yarn.nodemanager.aux-services</name><value>mapreduce_shuffle</value></property><property><name>yarn.resourcemanager.hostname</name><value>hadoop-hadoop-yarn-rm-0.hadoop-hadoop-yarn-rm.default</value></property><property><name>yarn.resourcemanager.address</name><value>hadoop-hadoop-yarn-rm-0.hadoop-hadoop-yarn-rm.default:8032</value></property><property><name>yarn.resourcemanager.scheduler.address</name><value>hadoop-hadoop-yarn-rm-0.hadoop-hadoop-yarn-rm.default:8030</value></property><property><name>yarn.nodemanager.env-whitelist</name><value>JAVA_HOME,HADOOP_COMMON_HOME,HADOOP_HDFS_HOME,HADOOP_CONF_DIR,CLASSPATH_PREPEND_DISTCACHE,HADOOP_YARN_HOME,HADOOP_HOME,PATH,LANG,TZ,HADOOP_MAPRED_HOME</value></property></configuration>
操作完之后,执行hive,可以进入就算成功。
执行hive时可能出现一个错误:
Caused by: com.ctc.wstx.exc.WstxParsingException: Illegal character entity: expansion character (code 0x8
at [row,col,system-id]:[3219,96,"file:/mnt/admin/apache-hive-3.1.3-bin/conf/hive-site.xml"],
这是因为xml模板中有个特殊字符,找到对应位置,去掉即可;
测试语句示例:
CREATE TABLE test(
name string,
num int)
PARTITIONED BY (`date` string)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' stored as textfile;
insert into test partition (`date`='20240523')(name,num) values ('cdl',100);
insert into test partition (`date`='20240522')(name,num) values ('cdl',100);
看是否能正确建表,正确插入数据,正确查询,hdfs中也能正确显示。
参考地址
https://www.cnblogs.com/liugp/p/16747557.html#1%E6%9E%84%E5%BB%BA%E9%95%9C%E5%83%8F
https://blog.csdn.net/chenleiking/article/details/82492282
版权归原作者 小白白白又白cdllp 所有, 如有侵权,请联系我们删除。