跳到主要内容

Hadoop 速查表

本文档提供 Hadoop 常用命令、配置和代码片段的快速参考。

HDFS 命令速查

文件操作

命令说明示例
hdfs dfs -ls <path>列出目录内容hdfs dfs -ls /user/hadoop
hdfs dfs -ls -R <path>递归列出hdfs dfs -ls -R /
hdfs dfs -mkdir <path>创建目录hdfs dfs -mkdir /user/test
hdfs dfs -mkdir -p <path>递归创建目录hdfs dfs -mkdir -p /a/b/c
hdfs dfs -put <local> <hdfs>上传文件hdfs dfs -put local.txt /user/
hdfs dfs -get <hdfs> <local>下载文件hdfs dfs -get /user/file.txt ./
hdfs dfs -cat <file>查看文件内容hdfs dfs -cat /user/file.txt
hdfs dfs -tail <file>查看文件末尾hdfs dfs -tail /user/file.txt
hdfs dfs -head <file>查看文件开头hdfs dfs -head /user/file.txt
hdfs dfs -rm <path>删除文件hdfs dfs -rm /user/file.txt
hdfs dfs -rm -r <path>递归删除hdfs dfs -rm -r /user/dir
hdfs dfs -rm -skipTrash <path>跳过回收站删除hdfs dfs -rm -skipTrash /file
hdfs dfs -mv <src> <dst>移动文件hdfs dfs -mv /a/file /b/
hdfs dfs -cp <src> <dst>复制文件hdfs dfs -cp /a/file /b/
hdfs dfs -du <path>查看大小hdfs dfs -du -h /user/
hdfs dfs -du -s <path>汇总大小hdfs dfs -du -s -h /user/
hdfs dfs -count <path>统计文件数量hdfs dfs -count /user/
hdfs dfs -touchz <file>创建空文件hdfs dfs -touchz /user/empty.txt
hdfs dfs -test -e <path>检查文件是否存在hdfs dfs -test -e /user/file.txt

权限管理

命令说明示例
hdfs dfs -chmod <mode> <path>修改权限hdfs dfs -chmod 755 /user/dir
hdfs dfs -chmod -R <mode> <path>递归修改权限hdfs dfs -chmod -R 755 /user/
hdfs dfs -chown <owner> <path>修改所有者hdfs dfs -chown hadoop /file
hdfs dfs -chown <owner>:<group> <path>修改所有者和组hdfs dfs -chown hdfs:hdfs /file
hdfs dfs -chgrp <group> <path>修改所属组hdfs dfs -chgrp hadoop /file

副本管理

命令说明示例
hdfs dfs -setrep <num> <path>设置副本数hdfs dfs -setrep 3 /user/file
hdfs dfs -setrep -R <num> <path>递归设置副本数hdfs dfs -setrep -R 2 /user/

管理命令

命令说明
hdfs dfsadmin -report查看集群状态报告
hdfs dfsadmin -safemode get查看安全模式状态
hdfs dfsadmin -safemode enter进入安全模式
hdfs dfsadmin -safemode leave离开安全模式
hdfs dfsadmin -refreshNodes刷新节点列表
hdfs dfsadmin -metasave <file>保存元数据信息
hdfs fsck /检查文件系统健康状态
hdfs fsck / -files检查文件详情
hdfs fsck / -blocks检查块信息
hdfs fsck / -locations检查块位置
hdfs fsck / -list-corruptfileblocks列出损坏的块
hdfs getconf -confKey <key>获取配置值

YARN 命令速查

应用管理

命令说明
yarn application -list列出所有应用
yarn application -list -appStates RUNNING列出运行中的应用
yarn application -status <appId>查看应用状态
yarn application -kill <appId>杀死应用
yarn application -moveToQueue <appId> -queue <queue>移动应用到队列

节点管理

命令说明
yarn node -list列出所有节点
yarn node -status <nodeId>查看节点状态
yarn rmadmin -refreshNodes刷新节点
yarn rmadmin -refreshQueues刷新队列配置

日志查看

命令说明
yarn logs -applicationId <appId>查看应用日志
yarn logs -applicationId <appId> -containerId <containerId>查看容器日志
yarn logs -applicationId <appId> -appOwner <owner>指定应用所有者

队列管理

命令说明
yarn queue -status <queueName>查看队列状态

核心配置属性

core-site.xml

属性默认值说明
fs.defaultFSfile:///默认文件系统,如 hdfs://namenode:9000
hadoop.tmp.dir/tmp/hadoop-${user.name}Hadoop 临时目录
io.file.buffer.size4096读写缓冲区大小
io.compression.codecs-压缩编解码器列表
hadoop.security.authenticationsimple认证方式:simple 或 kerberos

hdfs-site.xml

属性默认值说明
dfs.replication3默认副本数
dfs.blocksize134217728 (128MB)数据块大小
dfs.namenode.name.dir${hadoop.tmp.dir}/dfs/nameNameNode 元数据目录
dfs.datanode.data.dir${hadoop.tmp.dir}/dfs/dataDataNode 数据目录
dfs.namenode.http-address0.0.0.0:9870NameNode Web UI 地址
dfs.namenode.handler.count10NameNode RPC 处理线程数
dfs.datanode.handler.count10DataNode 处理线程数
dfs.datanode.max.transfer.threads4096DataNode 最大传输线程数
dfs.heartbeat.interval3sDataNode 心跳间隔
dfs.namenode.safemode.threshold-pct0.999f安全模式退出阈值
dfs.permissions.enabledtrue是否启用权限检查
dfs.webhdfs.enabledtrue是否启用 WebHDFS

yarn-site.xml

属性默认值说明
yarn.resourcemanager.hostname0.0.0.0ResourceManager 主机名
yarn.resourcemanager.address${yarn.resourcemanager.hostname}:8032RM 地址
yarn.resourcemanager.webapp.address${yarn.resourcemanager.hostname}:8088RM Web UI 地址
yarn.nodemanager.resource.memory-mb8192NodeManager 可用内存
yarn.nodemanager.resource.cpu-vcores8NodeManager 可用 CPU 核心数
yarn.scheduler.minimum-allocation-mb1024容器最小内存
yarn.scheduler.maximum-allocation-mb8192容器最大内存
yarn.nodemanager.aux-services-辅助服务,如 mapreduce_shuffle
yarn.nodemanager.vmem-check-enabledtrue是否启用虚拟内存检查
yarn.log-aggregation-enablefalse是否启用日志聚合

mapred-site.xml

属性默认值说明
mapreduce.framework.namelocal运行框架:local/yarn
mapreduce.jobtracker.addresslocalJobTracker 地址
mapreduce.jobhistory.address0.0.0.0:10020历史服务器地址
mapreduce.jobhistory.webapp.address0.0.0.0:19888历史服务器 Web UI
mapreduce.map.memory.mb1024Map 任务内存
mapreduce.reduce.memory.mb1024Reduce 任务内存
mapreduce.map.java.opts-Xmx820mMap JVM 参数
mapreduce.reduce.java.opts-Xmx820mReduce JVM 参数
mapreduce.task.io.sort.mb100Map 输出缓冲区大小
mapreduce.task.io.sort.factor10合并因子
mapreduce.reduce.shuffle.parallelcopies5Reduce 拉取并行数

启动和停止命令

服务启动

命令说明
start-dfs.sh启动 HDFS 集群
stop-dfs.sh停止 HDFS 集群
start-yarn.sh启动 YARN 集群
stop-yarn.sh停止 YARN 集群
start-all.sh启动所有服务
stop-all.sh停止所有服务
mr-jobhistory-daemon.sh start historyserver启动历史服务器

单组件启动

命令说明
hdfs --daemon start namenode启动 NameNode
hdfs --daemon start datanode启动 DataNode
hdfs --daemon start secondarynamenode启动 SecondaryNameNode
yarn --daemon start resourcemanager启动 ResourceManager
yarn --daemon start nodemanager启动 NodeManager

Web UI 端口

服务默认端口URL
NameNode9870http://namenode:9870
SecondaryNameNode9868http://secondary:9868
DataNode9864http://datanode:9864
ResourceManager8088http://rm:8088
NodeManager8042http://nm:8042
JobHistory Server19888http://history:19888

MapReduce 编程模板

基本作业模板

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class MyJob {

public static class MyMapper
extends Mapper<LongWritable, Text, Text, IntWritable> {

@Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
// Map 逻辑
String line = value.toString();
// 处理数据...
context.write(new Text("key"), new IntWritable(1));
}
}

public static class MyReducer
extends Reducer<Text, IntWritable, Text, IntWritable> {

@Override
protected void reduce(Text key, Iterable<IntWritable> values,
Context context) throws IOException, InterruptedException {
// Reduce 逻辑
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
context.write(key, new IntWritable(sum));
}
}

public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "my job");

job.setJarByClass(MyJob.class);
job.setMapperClass(MyMapper.class);
job.setReducerClass(MyReducer.class);

job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);

FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));

System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}

常用 Writable 类型

Hadoop 类型Java 类型说明
IntWritableint整型
LongWritablelong长整型
FloatWritablefloat浮点型
DoubleWritabledouble双精度
TextString文本字符串
BooleanWritableboolean布尔型
NullWritablenull空值
BytesWritablebyte[]字节数组
ArrayWritable数组数组类型
MapWritableMap映射类型

常见运维命令

格式化和初始化

# 格式化 NameNode(仅首次)
hdfs namenode -format

# 格式化并指定集群 ID
hdfs namenode -format -clusterId mycluster

均衡数据

# 启动数据均衡
hdfs balancer

# 指定带宽均衡
hdfs balancer -Ddfs.datanode.balance.bandwidthPerSec=50m

# 指定阈值均衡
hdfs balancer -threshold 5

快照管理

# 允许目录创建快照
hdfs dfsadmin -allowSnapshot /user/data

# 创建快照
hdfs dfs -createSnapshot /user/data snapshot1

# 删除快照
hdfs dfs -deleteSnapshot /user/data snapshot1

# 列出快照
hdfs lsSnapshottableDir

配额管理

# 设置空间配额
hdfs dfsadmin -setSpaceQuota 1T /user/data

# 设置文件数量配额
hdfs dfsadmin -setQuota 1000 /user/data

# 查看配额使用
hdfs dfs -count -q /user/data

故障排查

常见错误

错误信息可能原因解决方案
NameNode is not formattedNameNode 未格式化执行 hdfs namenode -format
SafeModeException集群处于安全模式等待或手动退出安全模式
NotReplicatedYetException副本数不足检查 DataNode 状态
NoRouteToHostException网络不通检查网络和防火墙
DiskOutOfSpaceException磁盘空间不足清理空间或扩容

日志位置

组件日志路径
NameNode$HADOOP_HOME/logs/hadoop--namenode-.log
DataNode$HADOOP_HOME/logs/hadoop--datanode-.log
ResourceManager$HADOOP_HOME/logs/yarn--resourcemanager-.log
NodeManager$HADOOP_HOME/logs/yarn--nodemanager-.log

检查命令

# 检查 Java 进程
jps

# 检查端口占用
netstat -tlnp | grep 9000

# 检查磁盘空间
df -h

# 检查内存使用
free -m

# 查看 NameNode 日志
tail -f $HADOOP_HOME/logs/hadoop-*-namenode-*.log

性能调优参数

内存配置建议

集群规模NameNode 内存DataNode 内存ResourceManager 内存
小型(少于 50 节点)8-16GB4-8GB4-8GB
中型(50-200 节点)16-32GB8-16GB8-16GB
大型(超过 200 节点)32-64GB16-32GB16-32GB

关键调优参数

<!-- NameNode 处理线程数 -->
<property>
<name>dfs.namenode.handler.count</name>
<value>100</value>
</property>

<!-- Map 输出缓冲区 -->
<property>
<name>mapreduce.task.io.sort.mb</name>
<value>256</value>
</property>

<!-- Reduce 拉取并行数 -->
<property>
<name>mapreduce.reduce.shuffle.parallelcopies</name>
<value>10</value>
</property>

<!-- YARN 容器最小内存 -->
<property>
<name>yarn.scheduler.minimum-allocation-mb</name>
<value>1024</value>
</property>

<!-- YARN 容器最大内存 -->
<property>
<name>yarn.scheduler.maximum-allocation-mb</name>
<value>32768</value>
</property>

压缩编解码器

编解码器类名特点
Gziporg.apache.hadoop.io.compress.GzipCodec压缩率高,CPU 消耗高
Snappyorg.apache.hadoop.io.compress.SnappyCodec压缩速度快,压缩率中等
LZOcom.hadoop.compression.lzo.LzoCodec压缩速度快,可分割
Bzip2org.apache.hadoop.io.compress.BZip2Codec压缩率高,可分割
LZ4org.apache.hadoop.io.compress.Lz4Codec压缩速度快

启用压缩配置

<!-- Map 输出压缩 -->
<property>
<name>mapreduce.map.output.compress</name>
<value>true</value>
</property>
<property>
<name>mapreduce.map.output.compress.codec</name>
<value>org.apache.hadoop.io.compress.SnappyCodec</value>
</property>

<!-- Reduce 输出压缩 -->
<property>
<name>mapreduce.output.fileoutputformat.compress</name>
<value>true</value>
</property>
<property>
<name>mapreduce.output.fileoutputformat.compress.codec</name>
<value>org.apache.hadoop.io.compress.GzipCodec</value>
</property>

参考资料