您的位置:首页 > 编程语言 > Java开发

一脸懵逼学习hadoop之HDFS的java客户端编写

2017-09-11 18:30 489 查看
1:eclipse创建一个项目,然后导入对应的jar包:

鼠标右击项目,点击properties或者alt+enter快捷键--->java build path--->libraries--->add library--->user library--->next--->user libraries--->new--->hdfsLib(根据自己的需要填写)---》add external jars(添加自己的需求包):

2:开始添加自己的需求包,路径如

  hadoop-2.4.1\share\hadoop\hdfs的hadoop-hdfs-2.4.1.jar和hadoop-2.4.1\share\hadoop\hdfs\lib下面的全部包;

  hadoop-2.4.1\share\hadoop\common的hadoop-common-2.4.1.jar和hadoop-2.4.1\share\hadoop\common\lib下面的全部包;

package com.master01;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.URISyntaxException;

import org.apache.commons.io.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;

public class HdfsTest {

//public FileSystem fs = null;
/*
@Before
public void init() throws IOException, InterruptedException, URISyntaxException{
//读配置文件
Configuration conf = new Configuration();
//这里直接拷贝配置或者直接设置值
conf.set("fs.defaultFS", "hdfs://master:9000/");

//获取配置文件里面的内容
fs = FileSystem.get(conf);
//fs = FileSystem.get(new URI("hdfs://master:9000"), conf, "root");
}
*/

/**
* 上传文件
* @throws IOException
*/
public static void upload() throws IOException{
//读配置文件
//读取classpath下的core-site.xml配置文件,并且解析其的内容,封装到conf的对象中;
Configuration conf = new Configuration();
//这里直接拷贝配置或者直接设置值
//也可以在代码中对conf的配置信息进行手动设置,会覆盖配置文件中的配置信息
conf.set("fs.defaultFS", "hdfs://master:9000");

//获取配置文件里面的内容
//根据配置信息,去获取一个具体文件系统的客户端操作实例对象
FileSystem fs = FileSystem.get(conf);
//本地文件是输入流,hdfs是输出流

//先搞出路径
Path src = new Path("hdfs://master:9000/aa/test.txt");
//搞出输出流,即向hdfs上面写内容
FSDataOutputStream create = fs.create(src);

//输入流就是读,本地文件,输入流
FileInputStream fileInputStream = new FileInputStream("d:/test.txt");

//将文件fileInputStream到create即完成上传到hdfs
IOUtils.copy(fileInputStream, create);
}

//最快的上传文件的方法
public void upload02() throws IllegalArgumentException, IOException, InterruptedException, URISyntaxException{
//读配置文件
Configuration conf = new Configuration();
//这里直接拷贝配置或者直接设置值
conf.set("fs.defaultFS", "hdfs://master:9000");

//获取配置文件里面的内容
FileSystem fs = FileSystem.get(conf);
//FileSystem fs = FileSystem.get(new URI("hdfs://master:9000"), conf, "root");
fs.copyFromLocalFile(new Path("d:/test.txt"), new Path("hdfs://master:9000/aa/test.txt"));
}

/**
* 下载文件
* @throws IOException
* @throws IllegalArgumentException
*/
public void download02() throws IllegalArgumentException, IOException{
//去配置文件
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://master:9000");

//获取配置文件里面的内容
FileSystem fs = FileSystem.get(conf);
fs.copyToLocalFile(new Path("hdfs://master:9000/aa/test.txt"), new Path("d:/test2.txt"));

}

/***
* 创建文件夹的方法
* @throws IOException
*/
public void mkdir02() throws IOException{
//主配置文件
Configuration conf = new Configuration();
//设置配置文件的值
conf.set("fs.defaultFS", "hdfs://master:9000");
//获取配置文件里面的内容
FileSystem fs = FileSystem.get(conf);

//文件夹的创建
fs.mkdirs(new Path("hdfs://master:9000/aaa/bbb/ccc"));
}

/**
* 删除文件
* @throws IOException
*/
public void remove02() throws IOException{
//主配置文件
Configuration conf = new Configuration();
//设置值
conf.set("fs.defaultFS", "hdfs://master:9000");
//获取配置文件里面的内容
FileSystem fs = FileSystem.get(conf);

//执行删除操作
fs.delete(new Path("hdfs://master:9000/aaa/bbb/ccc"), true);
}

/**
* 文件的移动
* @throws IOException
*/
public void move() throws IOException{
//主配置文件
Configuration conf = new Configuration();
//设置值
conf.set("fs.defaultFS", "hdfs://master:9000");
//获取配置文件里面的内容
FileSystem fs = FileSystem.get(conf);

//移动操作
fs.rename(new Path("hdfs://master:9000/aa/test.txt"), new Path("hdfs://master:9000/aaa/bbb"));
}

/***
* 查看文件的信息
* @throws IOException
*/
public void listFiles() throws IOException{
//主配置文件
Configuration conf = new Configuration();
//设置值
conf.set("fs.defaultFS", "hdfs://master:9000");
//获取配置文件里面的内容
FileSystem fs = FileSystem.get(conf);

//查看的是文件,不是文件夹
//listFiles列出的是文件信息,而且提供递归遍历
RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("hdfs://master:9000/"), true);
//迭代输出信息
while(listFiles.hasNext()){
LocatedFileStatus file = listFiles.next();
//文件路径
Path path = file.getPath();
System.out.println(path.getName());
}

System.out.println("=============================================");
//listStatus列出文件和文件夹的信息,但是不提供自带的递归遍历
FileStatus[] listStatus = fs.listStatus(new Path("hdfs://master:9000/"));
/*for(int i = 0 ; i<listStatus.length; i++){
System.out.println(listStatus[i]);
}*/
for(FileStatus fileStatus : listStatus){
//根据获取的路径获取文件夹的名称
Path path = fileStatus.getPath();
System.out.println(path.getName());
}

}

public static void main(String[] args) {
HdfsTest hdfsTest = new HdfsTest();
try {
//上传文件的调用
//hdfsTest.upload02();

//下载文件的调用
//hdfsTest.download02();

//文件夹的创建
//hdfsTest.mkdir02();

//删除操作
//hdfsTest.remove02();

//移动文件的操作
//hdfsTest.move();

//查看文件信息
hdfsTest.listFiles();
} catch (Exception e) {
e.printStackTrace();
}
}

}


3:NameNode的职责


(1):维护元数据的信息;

(2):维护hdfs的目录树;

(3):响应客户端的请求;

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: