《Hadoop系列》Hadoop实操记录

7 篇文章 0 订阅
3 篇文章 6 订阅

Hadoop实操记录

一、shell脚本配置伪分布式Hadoop

1.hadoop安装脚本=>install_hadoop.sh

## !/bin/bash

## 安装jdk
./install_java.sh
##安装前的路径
INSTALL_PROFIX=/opt/apps
## 环境变量
ETC_PROFILE=/etc/profile
## hadoop安装路径
HADOOP_DIR=${INSTALL_PROFIX}/hadoop-2.8.1
## 安装包的前缀路径
HADOOP_PROFIX=/opt/software
## hadoop的安装包路径
HADOOP_TAR=${HADOOP_PROFIX}/hadoop-2.8.1.tar.gz

## 判断安装路径是否存在,如果存在说明此软件已经安装过了
if [ -e "${HADOOP_DIR}" ];then
        echo "warning:${HADOOP_DIR} is exists"
        exit 0
fi

## 判断安装包是否存在,如果不存在,就不用安装了
if [ ! -e "${HADOOP_TAR}" ];then
        echo "warving:${HADOOP_TAR} is not exists"
        exit -1
fi

tar -zxvf ${HADOOP_TAR} -C ${INSTALL_PROFIX}

cat << EOF >> ${ETC_PROFILE}
export HADOOP_HOME=${HADOOP_DIR}
export PATH=\$PATH:\${HADOOP_HOME}/bin:\${HADOOP_HOME}/sbin
EOF

## 修改hadoop-env.sh配置文件
./hadoop-env.sh

## 修改core-site.xml配置文件
./core-site.sh

## 修改hdfs-site.xml配置文件
./hdfs-site.sh

## 免密操作
ssh-keygen
ssh-copy-id 192.168.130.130

## 格式化namenode
source /etc/profile
hdfs namenode -format

## 关闭防火墙
systemctl stop firewalld

2.jdk安装脚本=>install_java.sh

## !/bin/bash

## 安装前缀路径
INSTALL_PREFIX=/opt/apps
## 配置环境变量
ETC_PROFILE=/etc/profile
## jdk安装路径
JAVA_DIR=${INSTALL_PREFIX}/jdk1.8.0_261
## 安装包的路径
TAR_PROFIX=/opt/software
## JDK的路径
JAVA_TAR=${TAR_PROFIX}/jdk-8u261-linux-x64.tar.gz

## 判断安装路径是否存在,如果存在说明我们已经安装过此应用
if [ -e "${JAVA_DIR}" ];then
	echo "warning:${JAVA_DIR} is exists"
	exit 0
fi

## 判断安装包是否存在,如果不存在,就不用安装
if [ ! -e "${JAVA_TAR}" ];then
	echo "warning:${JAVA_TAR} is not exists"
	exit -1
fi

tar -zxvf ${JAVA_TAR} -C ${INSTALL_PREFIX}

cat << EOF >> ${ETC_PROFILE}
 export JAVA_HOME=${JAVA_DIR}
 export PATH=\$PATH:\${JAVA_HOME}/bin
 export CLASS_PATH=./\${JAVA_HOME}/lib
EOF

3.修改Hadoop配置文件

3.1 hadoop-env.sh

## 更新配置文件
 source /etc/profile

## 更改Hadoop配置文件

echo '# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Set Hadoop-specific environment variables here.

# The only required environment variable is JAVA_HOME.  All others are
# optional.  When running a distributed configuration it is best to
# set JAVA_HOME in this file, so that it is correctly defined on
# remote nodes.

# The java implementation to use.
export JAVA_HOME=/opt/apps/jdk1.8.0_261

# The jsvc implementation to use. Jsvc is required to run secure datanodes
# that bind to privileged ports to provide authentication of data transfer
# protocol.  Jsvc is not required if SASL is configured for authentication of
# data transfer protocol using non-privileged ports.
#export JSVC_HOME=${JSVC_HOME}

export HADOOP_CONF_DIR=${HADOOP_CONF_DIR:-"/etc/hadoop"}

# Extra Java CLASSPATH elements.  Automatically insert capacity-scheduler.
    export HADOOP_CLASSPATH=$HADOOP_CLASSPATH:$f
  else
done

# The maximum amount of heap to use, in MB. Default is 1000.
#export HADOOP_HEAPSIZE=
#export HADOOP_NAMENODE_INIT_HEAPSIZE=""

# Kerberos security.
# export HADOOP_JAAS_DEBUG=true

# Extra Java runtime options.  Empty by default.
# For Kerberos debugging, an extended option set logs more invormation
# export HADOOP_OPTS="-Djava.net.preferIPv4Stack=true -Dsun.security.krb5.debug=true -Dsun.security.spnego.debug"
export HADOOP_OPTS="$HADOOP_OPTS -Djava.net.preferIPv4Stack=true"

# Command specific options appended to HADOOP_OPTS when specified
export HADOOP_DATANODE_OPTS="-Dhadoop.security.logger=ERROR,RFAS $HADOOP_DATANODE_OPTS"


export HADOOP_NFS3_OPTS="$HADOOP_NFS3_OPTS"
export HADOOP_PORTMAP_OPTS="-Xmx512m $HADOOP_PORTMAP_OPTS"

# The following applies to multiple commands (fs, dfs, fsck, distcp etc)
export HADOOP_CLIENT_OPTS="$HADOOP_CLIENT_OPTS"
# set heap args when HADOOP_HEAPSIZE is empty
if [ "$HADOOP_HEAPSIZE" = "" ]; then
  export HADOOP_CLIENT_OPTS="-Xmx512m $HADOOP_CLIENT_OPTS"
fi
#HADOOP_JAVA_PLATFORM_OPTS="-XX:-UsePerfData $HADOOP_JAVA_PLATFORM_OPTS"

# On secure datanodes, user to run the datanode as after dropping privileges.
# This **MUST** be uncommented to enable secure HDFS if using privileged ports
# to provide authentication of data transfer protocol.  This **MUST NOT** be
# defined if SASL is configured for authentication of data transfer protocol
# using non-privileged ports.
export HADOOP_SECURE_DN_USER=${HADOOP_SECURE_DN_USER}

# Where log files are stored.  $HADOOP_HOME/logs by default.
#export HADOOP_LOG_DIR=${HADOOP_LOG_DIR}/$USER

# Where log files are stored in the secure data environment.
#export HADOOP_SECURE_DN_LOG_DIR=${HADOOP_LOG_DIR}/${HADOOP_HDFS_USER}

###
# HDFS Mover specific parameters
###
# Specify the JVM options to be used when starting the HDFS Mover.
# These options will be appended to the options specified as HADOOP_OPTS
# and therefore may override any similar flags set in HADOOP_OPTS
#
# export HADOOP_MOVER_OPTS=""

###
# Advanced Users Only!
###

# The directory where pid files are stored. /tmp by default.
# NOTE: this should be set to a directory that can only be written to by
#       the user that will run the hadoop daemons.  Otherwise there is the
#       potential for a symlink attack.
export HADOOP_PID_DIR=${HADOOP_PID_DIR}
export HADOOP_SECURE_DN_PID_DIR=${HADOOP_PID_DIR}

# A string representing this instance of hadoop. $USER by default.
export HADOOP_IDENT_STRING=$USER ' > ${HADOOP_HOME}/etc/hadoop/hadoop-env.sh

3.2 core-site.sh

## 更新配置文件
 source /etc/profile

## 更改Hadoop配置文件

echo '<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<!--
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License. See accompanying LICENSE file.
-->

<!-- Put site-specific property overrides in this file. -->

<configuration>

        <!-- 配置的namenode的内部通信地址  -->
        <property>
                <name>fs.defaultFS</name>
                <value>hdfs://192.168.130.130:9000</value>
        </property>

        <!-- hadoop的内部的数据或者元数据真是的存放路径 -->
        <property>
                <name>hadoop.tmp.dir</name>
                <value>/opt/apps/hadoop-2.8.1/hdpdata</value>
        </property>
</configuration> ' > ${HADOOP_HOME}/etc/hadoop/core-site.xml 

3.3 hdfs-site.sh

## 更新配置文件
 source /etc/profile

## 更改Hadoop配置文件

echo '<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<!--
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License. See accompanying LICENSE file.
-->

<!-- Put site-specific property overrides in this file. -->

<configuration>
        <property>
                <name>dfs.replication</name>
                <value>1</value>
        </property>
</configuration> ' > ${HADOOP_HOME}/etc/hadoop/hdfs-site.xml

4.帮助文档

启动命令:sh install_hadoop.sh
启动内容:
1.调用install_jdk.sh,安装jdk和配置环境
2.安装hadoop和配置hadoop环境
3.调用hadoop-env.sh,修改jdk的路径
4.调用core-site.sh和hdfs-site.sh修改hadoop文件中的core-site.xml和hdfs-site.xml
完成操作:启动sh install_hadoop.sh成功后
通过start-dfs.sh启动
通过http://192.168.130.130:50070查看webGUI 

二、数据直接上传到分区目录上,让分区表和数据产生关联的三种方式

1.上传数据后修复

  • 上传数据

dfs -mkdir -p /user/dept_partition/day=20210725/hour=8

dfs -put /opt/apps/dept_20210725.log /user/dept_partition/day=20210725/hour=8

  • 查询数据

select * from dept_partition where day=‘20210725’ and hour=‘8’

查询不到

  • 修复数据

msck repair table dept_partition;

  • 再次查询数据

select * from dept_partition where day=‘20210725’ and hour=‘8’

2.上传数据后添加分区

  • 上传数据

dfs -mkdir -p /user/dept_partition/day=20210725/hour=8

dfs -put /opt/apps/dept_20210725.log /user/dept_partition/day=20210725/hour=8

  • 添加分区

alter table ddept_partition add partition(day=‘20210725’,hour=‘8’)

  • 查询数据

select * from dept_partition where day=‘20210725’ and hour =‘8’

3.创建文件夹后load数据到分区

  • 上传数据

dfs -mkdir -p /user/dept_partition/day=20210725/hour=8

  • 上传数据

load data local inpath ‘/opt/apps/dept_20210725.log’ into table dept_partiton partition(day=‘20210725’,hour=‘8’)

  • 查询数据

select * from dept_partition where day=‘20210725’ and hour =‘8’

  • 8
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 13
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DATA数据猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值