1. hdfs的操作(图形界面)
1.1 hdfs的启动流程
- 进入安全模式
- 加载fsimage
- 加载edits
- 保存检查点(融合fsimage和edits文件,生成新的fsimage)
- 退出安全模式
1.2 通过浏览器访问
http://namenode:50070
2. hdfs的操作(shell操作)
hdfs dfs
hadoop fs
hdfs dfs -put text01.txt /text01.txt --上传文件
hdfs dfs -copyFromLocal text01.txt /test01
hdfs dfs -get /text02.txt /root/test01.txt -- 下载文件
hdfs dfs -copyToLocal /test01 /test03
hdfs dfs -rm /text01.txt -- 删除文件
hdfs dfs -rm -r /input/out --删除目录
hdfs dfs -mkdir -p /input/out --创建文件夹
hdfs dfs -ls /input --查看文件
hdfs dfs -mv /text02.txt /input --移动文件
hdfs dfs -appendToFile text01.txt /input/text02.txt -本地文件追加到hdfs上
hdfs dfs -text /input/text02.txt --查看远程文件
hdfs dfs -df --查看文件系统
3. hdfs的操作(API操作)
3.1 依赖POM
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.6.4</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.6.4</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.6.4</version>
</dependency>
3.2 hdfs读写文件
import org.apache.commons.compress.utils.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.Test;
public class TestHdfs {
@Test
public void testWrite() throws Exception {
//创建配置对象
Configuration con = new Configuration();
con.set("fs.defaultFS","hdfs://mini:8020");
//创建文件系统对象
FileSystem fs = FileSystem.get(con);
//文件路径
Path path = new Path("/test02");
//创建文件输出流
FSDataOutputStream fsd = fs.create(path, true);
//写文件
fsd.write("hello".getBytes());
//刷新关闭
fsd.flush();
fsd.close();
}
@Test
public void testRead() throws Exception {
//创建配置对象
Configuration con = new Configuration();
con.set("fs.defaultFS","hdfs://mini:8020");
//创建文件系统对象
FileSystem fs = FileSystem.get(con);
//文件路径
Path path = new Path("/test02");
//创建文件输入流
FSDataInputStream fsd = fs.open(path);
IOUtils.copy(fsd,System.out);
}
@Test
public void testUpload() throws Exception {
//创建配置对象
Configuration con = new Configuration();
con.set("fs.defaultFS","hdfs://mini:8020");
//创建文件系统对象
FileSystem fs = FileSystem.get(con);
//目标文件路径
Path toPath = new Path("/t.txt");
//源文件路径
Path fromPath = new Path("d://t.txt");
fs.copyFromLocalFile(false,fromPath,toPath);//fasle 表示保留源文件
}
@Test
public void testDownload() throws Exception {
//创建配置对象
Configuration con = new Configuration();
con.set("fs.defaultFS","hdfs://mini:8020");
//创建文件系统对象
FileSystem fs = FileSystem.get(con);
//远程文件路径
Path fromPath = new Path("/t.txt");
//本地文件路径
Path toPath = new Path("file:///e:/t.txt");
fs.copyToLocalFile(false, fromPath, toPath,true);
}
//删除文件
@Test
public void testDelete() throws Exception {
//创建配置对象
Configuration con = new Configuration();
con.set("fs.defaultFS","hdfs://mini:8020");
//创建文件系统对象
FileSystem fs = FileSystem.get(con);
//远程文件路径
Path fromPath = new Path("/t.txt");
// public boolean delete(Path f, Boolean recursive)
// 只有recursive=true时,一个非空目录及其内容才会被删除
fs.delete(fromPath,false);
}
//block信息
@Test
public void testBlock() throws Exception {
//创建配置对象
Configuration con = new Configuration();
con.set("fs.defaultFS","hdfs://mini:8020");
//创建文件系统对象
FileSystem fs = FileSystem.get(con);
//文件路径
Path fromPath = new Path("/test02");
//获得block信息
BlockLocation[] lo = fs.getFileBlockLocations(fromPath, 0, 20);
}
}