【注】该系列课程是应邀实验楼整理编写的,这里需要赞一下实验楼提供了学习的新方式,可以边看博客边上机实验,课程地址为 https://www.shiyanlou.com/courses/237
1 环境说明
部署节点操作系统为CentOS,防火墙和SElinux禁用,创建了一个shiyanlou用户并在系统根目录下创建/app目录,用于存放Hadoop等组件运行包。因为该目录用于安装hadoop等组件程序,用户对shiyanlou必须赋予rwx权限(一般做法是root用户在根目录下创建/app目录,并修改该目录拥有者为shiyanlou(chown –R shiyanlou:shiyanlou /app)。
Hadoop搭建环境:
2 HDFS原理
HDFS(Hadoop Distributed File System)是一个分布式文件系统,是谷歌的GFS山寨版本。它具有高容错性并提供了高吞吐量的数据访问,非常适合大规模数据集上的应用,它提供了一个高度容错性和高吞吐量的海量数据存储解决方案。
- 高吞吐量访问:HDFS的每个Block分布在不同的Rack上,在用户访问时,HDFS会计算使用最近和访问量最小的服务器给用户提供。由于Block在不同的Rack上都有备份,所以不再是单数据访问,所以速度和效率是非常快的。另外HDFS可以并行从服务器集群中读写,增加了文件读写的访问带宽。
- 高容错性:系统故障是不可避免的,如何做到故障之后的数据恢复和容错处理是至关重要的。HDFS通过多方面保证数据的可靠性,多份复制并且分布到物理位置的不同服务器上,数据校验功能、后台的连续自检数据一致性功能都为高容错提供了可能。
- 线性扩展:因为HDFS的Block信息存放到NameNode上,文件的Block分布到DataNode上,当扩充的时候仅仅添加DataNode数量,系统可以在不停止服务的情况下做扩充,不需要人工干预。
2.1 HDFS架构
如上图所示HDFS是Master和Slave的结构,分为NameNode、Secondary NameNode和DataNode三种角色。
- NameNode:在Hadoop1.X中只有一个Master节点,管理HDFS的名称空间和数据块映射信息、配置副本策略和处理客户端请求;
- Secondary NameNode:辅助NameNode,分担NameNode工作,定期合并fsimage和fsedits并推送给NameNode,紧急情况下可辅助恢复NameNode;
- DataNode:Slave节点,实际存储数据、执行数据块的读写并汇报存储信息给NameNode;
2.2 HDFS读操作
2.3 HDFS写操作
2.4 HDFS中常用到的命令
1. hadoop fs
2. hadoop fsadmin
- hadoop dfsadmin -report
- hadoop dfsadmin -safemode enter | leave | get | wait
- hadoop dfsadmin -setBalancerBandwidth 1000
3. hadoop fsck
4. start-balancer.sh
相关HDFS API可以到Apache官网进行查看:
3 测试例子1
3.1 测试例子1内容
在Hadoop集群中编译并运行《权威指南》中的例3.2,读取HDFS文件内容。
3.2 运行代码
import java.io.InputStream;
import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils;
public class FileSystemCat {
public static void main(String[] args) throws Exception {
String uri = args[0];
Configuration conf = new Configuration();
FileSystem fs = FileSystem. get(URI.create (uri), conf);
InputStream in = null;
try {
in = fs.open( new Path(uri));
IOUtils.copyBytes(in, System.out, 4096, false);
} finally {
IOUtils.closeStream(in);
}
}
}
3.3 实现过程
3.3.1 创建代码目录
配置本机主机名为hadoop,sudo时需要输入shiyanlou用户的密码:shiyanlou。将hadoop添加到第一行的最后。
sudo vim /etc/hosts
# 将hadoop添加到第一行末尾,修改后类似:
# 172.17.2.98 f738b9456777 hadoop
ping hadoop
使用如下命令启动Hadoop
cd /app/hadoop-1.1.2/bin
./start-all.sh
在/app/hadoop-1.1.2目录下使用如下命令建立myclass和input目录:
cd /app/hadoop-1.1.2
rm -rf myclass
rm -rf input
mkdir myclass
mkdir input
3.3.2 建立例子文件上传到HDFS中
进入/app/hadoop-1.1.2/input目录,在该目录中建立quangle.txt文件
cd /app/hadoop-1.1.2/input
touch quangle.txt
vi quangle.txt
内容为:
On the top of the Crumpetty Tree
The Quangle Wangle sat,
But his face you could not see,
On account of his Beaver Hat.
使用如下命令在hdfs中建立目录/class4
(如果需要直接使用hadoop命令,需要把/app/hadoop-1.1.2加入到Path路径中)
把例子文件上传到hdfs的/class4文件夹中
3.3.3 配置本地环境
对/app/hadoop-1.1.2/conf目录中的hadoop-env.sh进行配置,如下如所示:
加入对HADOOP_CLASPATH变量值,值为/app/hadoop-1.1.2/myclass,设置完毕后编译该配置文件,使配置生效
3.3.4 编写代码
进入/app/hadoop-1.1.2/myclass目录,在该目录中建立FileSystemCat.java代码文件,命令如下:
输入代码内容:
3.3.5 编译代码
在/app/hadoop-1.1.2/myclass目录中,使用如下命令编译代码:
3.3.6 使用编译代码读取HDFS文件
使用如下命令读取HDFS中/class4/quangle.txt内容:
4 测试例子2
4.1 测试例子2内容
在本地文件系统生成一个大约100字节的文本文件,写一段程序读入这个文件并将其第101-120字节的内容写入HDFS成为一个新文件。
4.2 运行代码
注意:在编译前请先删除中文注释!
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.util.Progressable;
public class LocalFile2Hdfs {
public static void main(String[] args) throws Exception {
// 获取读取源文件和目标文件位置参数
String local = args[0];
String uri = args[1];
FileInputStream in = null;
OutputStream out = null;
Configuration conf = new Configuration();
try {
// 获取读入文件数据
in = new FileInputStream(new File(local));
// 获取目标文件信息
FileSystem fs = FileSystem.get(URI.create(uri), conf);
out = fs.create(new Path(uri), new Progressable() {
@Override
public void progress() {
System.out.println("*");
}
});
// 跳过前100个字符
in.skip(100);
byte[] buffer = new byte[20];
// 从101的位置读取20个字符到buffer中
int bytesRead = in.read(buffer);
if (bytesRead >= 0) {
out.write(buffer, 0, bytesRead);
}
} finally {
IOUtils.closeStream(in);
IOUtils.closeStream(out);
}
}
}
4.3 实现过程
4.3.1 编写代码
进入/app/hadoop-1.1.2/myclass目录,在该目录中建立LocalFile2Hdfs.java代码文件,命令如下:
输入代码内容:
4.3.2 编译代码
在/app/hadoop-1.1.2/myclass目录中,使用如下命令编译代码:
4.3.3 建立测试文件
进入/app/hadoop-1.1.2/input目录,在该目录中建立local2hdfs.txt文件
内容为:
Washington (CNN) -- Twitter is suing the U.S. government in an effort to loosen restrictions on what the social media giant can say publicly about the national security-related requests it receives for user data.
The company filed a lawsuit against the Justice Department on Monday in a federal court in northern California, arguing that its First Amendment rights are being violated by restrictions that forbid the disclosure of how many national security letters and Foreign Intelligence Surveillance Act court orders it receives -- even if that number is zero.
Twitter vice president Ben Lee wrote in a blog post that it's suing in an effort to publish the full version of a "transparency report" prepared this year that includes those details.
The San Francisco-based firm was unsatisfied with the Justice Department's move in January to allow technological firms to disclose the number of national security-related requests they receive in broad ranges.
4.3.4 使用编译代码上传文件内容到HDFS
使用如下命令读取local2hdfs第101-120字节的内容写入HDFS成为一个新文件:
4.3.5 验证是否成功
使用如下命令读取local2hdfs_part.txt内容:
5 测试例子3
5.1 测试例子3内容
测试例子2的反向操作,在HDFS中生成一个大约100字节的文本文件,写一段程序读入这个文件,并将其第101-120字节的内容写入本地文件系统成为一个新文件。
5.2 程序代码
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
public class Hdfs2LocalFile {
public static void main(String[] args) throws Exception {
String uri = args[0];
String local = args[1];
FSDataInputStream in = null;
OutputStream out = null;
Configuration conf = new Configuration();
try {
FileSystem fs = FileSystem.get(URI.create(uri), conf);
in = fs.open(new Path(uri));
out = new FileOutputStream(local);
byte[] buffer = new byte[20];
in.skip(100);
int bytesRead = in.read(buffer);
if (bytesRead >= 0) {
out.write(buffer, 0, bytesRead);
}
} finally {
IOUtils.closeStream(in);
IOUtils.closeStream(out);
}
}
}
5.3 实现过程
5.3.1 编写代码
进入/app/hadoop-1.1.2/myclass目录,在该目录中建立Hdfs2LocalFile.java代码文件,命令如下:
输入代码内容:
5.3.2 编译代码
在/app/hadoop-1.1.2/myclass目录中,使用如下命令编译代码:
5.3.3 建立测试文件
进入/app/hadoop-1.1.2/input目录,在该目录中建立hdfs2local.txt文件
内容为:
The San Francisco-based firm was unsatisfied with the Justice Department's move in January to allow technological firms to disclose the number of national security-related requests they receive in broad ranges.
"It's our belief that we are entitled under the First Amendment to respond to our users' concerns and to the statements of U.S. government officials by providing information about the scope of U.S. government surveillance -- including what types of legal process have not been received," Lee wrote. "We should be free to do this in a meaningful way, rather than in broad, inexact ranges."
在/app/hadoop-1.1.2/input目录下把该文件上传到hdfs的/class4/文件夹中
5.3.4 使用编译代码把文件内容从HDFS输出到文件系统中
使用如下命令读取hdfs2local.txt第101-120字节的内容写入本地文件系统成为一个新文件:
5.3.5 验证是否成功
使用如下命令读取hdfs2local_part.txt内容: