Hadoop文件系统
基本的文件系统命令操作, 通过hadoop fs -help可以获取所有的命令的详细帮助文件。
Java抽象类org.apache.hadoop.fs.FileSystem定义了hadoop的一个文件系统接口。该类是一个抽象类,通过以下两种静态工厂方法可以过去FileSystem实例:
public static FileSystem.get(Configuration conf) throws IOException
public static FileSystem.get(URI uri, Configuration conf) throws IOException
具体方法实现:
1、public boolean mkdirs(Path f) throws IOException
一次性新建所有目录(包括父目录), f是完整的目录路径。
2、public FSOutputStream create(Path f) throws IOException
创建指定path对象的一个文件,返回一个用于写入数据的输出流
create()有多个重载版本,允许我们指定是否强制覆盖已有的文件、文件备份数量、写入文件缓冲区大小、文件块大小以及文件权限。
3、public boolean copyFromLocal(Path src, Path dst) throws IOException
将本地文件拷贝到文件系统
4、public boolean exists(Path f) throws IOException
检查文件或目录是否存在
5、public boolean delete(Path f, Boolean recursive)
永久性删除指定的文件或目录,如果f是一个空目录或者文件,那么recursive的值就会被忽略。只有recursive=true时,一个非空目录及其内容才会被删除。
6、FileStatus类封装了文件系统中文件和目录的元数据,包括文件长度、块大小、备份、修改时间、所有者以及权限信息。
通过"FileStatus.getPath()"可查看指定HDFS中某个目录下所有文件。
package hdfsTest;
import java.io.IOException;
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.Path;
public class OperatingFiles {
//initialization
static Configuration conf = new Configuration();
static FileSystem hdfs;
static {
String path = "/usr/java/hadoop-1.0.3/conf/";
conf.addResource(new Path(path + "core-site.xml"));
conf.addResource(new Path(path + "hdfs-site.xml"));
conf.addResource(new Path(path + "mapred-site.xml"));
path = "/usr/java/hbase-0.90.3/conf/";
conf.addResource(new Path(path + "hbase-site.xml"));
try {
hdfs = FileSystem.get(conf);
} catch (IOException e) {
e.printStackTrace();
}
}
//create a direction
public void createDir(String dir) throws IOException {
Path path = new Path(dir);
hdfs.mkdirs(path);
System.out.println("new dir \t" + conf.get("fs.default.name") + dir);
}
//copy from local file to HDFS file
public void copyFile(String localSrc, String hdfsDst) throws IOException{
Path src = new Path(localSrc);
Path dst = new Path(hdfsDst);
hdfs.copyFromLocalFile(src, dst);
//list all the files in the current direction
FileStatus files[] = hdfs.listStatus(dst);
System.out.println("Upload to \t" + conf.get("fs.default.name") + hdfsDst);
for (FileStatus file : files) {
System.out.println(file.getPath());
}
}
//create a new file
public void createFile(String fileName, String fileContent) throws IOException {
Path dst = new Path(fileName);
byte[] bytes = fileContent.getBytes();
FSDataOutputStream output = hdfs.create(dst);
output.write(bytes);
System.out.println("new file \t" + conf.get("fs.default.name") + fileName);
}
//list all files
public void listFiles(String dirName) throws IOException {
Path f = new Path(dirName);
FileStatus[] status = hdfs.listStatus(f);
System.out.println(dirName + " has all files:");
for (int i = 0; i< status.length; i++) {
System.out.println(status[i].getPath().toString());
}
}
//judge a file existed? and delete it!
public void deleteFile(String fileName) throws IOException {
Path f = new Path(fileName);
boolean isExists = hdfs.exists(f);
if (isExists) { //if exists, delete
boolean isDel = hdfs.delete(f,true);
System.out.println(fileName + " delete? \t" + isDel);
} else {
System.out.println(fileName + " exist? \t" + isExists);
}
}
public static void main(String[] args) throws IOException {
OperatingFiles ofs = new OperatingFiles();
System.out.println("\n=======create dir=======");
String dir = "/test";
ofs.createDir(dir);
System.out.println("\n=======copy file=======");
String src = "/home/ictclas/Configure.xml";
ofs.copyFile(src, dir);
System.out.println("\n=======create a file=======");
String fileContent = "Hello, world! Just a test.";
ofs.createFile(dir+"/word.txt", fileContent);
}
}
//output~
=======create dir=======
new dir hdfs://sfserver20.localdomain:9000/test
=======copy file=======
Upload to hdfs://sfserver20.localdomain:9000/test
hdfs://sfserver20.localdomain:9000/test/Configure.xml
hdfs://sfserver20.localdomain:9000/test/word.txt
=======create file=======
new file hdfs://sfserver20.localdomain:9000/test/word.txt
=======before delete file=======
new dir hdfs://sfserver20.localdomain:9000/test/del
new file hdfs://sfserver20.localdomain:9000/test/del.txt
/test has all files:
hdfs://sfserver20.localdomain:9000/test/Configure.xml
hdfs://sfserver20.localdomain:9000/test/del
hdfs://sfserver20.localdomain:9000/test/del.txt
hdfs://sfserver20.localdomain:9000/test/word.txt
=======after delete file=======
/test/del delete? true
/test/del.txt delete? true
/test/no exist? false
/test has all files:
hdfs://sfserver20.localdomain:9000/test/Configure.xml
hdfs://sfserver20.localdomain:9000/test/word.txt
分享到:
相关推荐
大数据技术基础实验报告-调用Java API实现HDFS操作
大数据-java操作HDFS的增、删、查等基本操作,maven项目
java操作hdfs 修改参数配置与本机一致; 有五个示例文档,有用到springboot, Big data projects using HDFS and MapReduce
java操作hdfs文件系统的完整依赖包,如果是tomcat服务器,解压后全部放在web工程的lib文件夹下即可.
A.2实验二:熟悉常用的HDFS操作 ...(3)熟悉HDFS操作常用的Java API。 A.2.2 实验平台 (1)操作系统:Linux(建议Ubuntu 16.04)。(2) Hadoop版本:2.7.1。 (3)JDK版本:1.7或以上版本。(4) Java IDE:Eclipse。
java操作Hadoop源码之HDFS Java API操作-创建目录,附带所需全部jar包,欢迎下载学习。
java对大数据HDFS文件操作jar包和maven2个项目,有jar包,有依赖,2个项目呦
java操作hadoop的hdfs的各种操作,包括文件创建,删除,文件夹创建,上传文件,下载文件,读取文件等
Java操作HDFS.md
javaAPI操作HDFS,测试可用 window环境需要下载hadoop客户端
自己整理的hadoop的java-api。各个API写得比较简单,比较细,适合初学者参考。PS:集群请自行搭建
第七节、使用Java操作HDFS第七节、使用Java操作HDFS一、准备jar包二、解决环境冲突问题1. 在windows下添加HADOOP_HOME系统环境变
java操作Hadoop源码之HDFS Java API操作-上传文件,附带所需全部jar包,欢迎下载学习。
一、实验目的 1. 理解 HDFS 体系架构。 2. 理解 HDFS 文件存储原理和数据读写过程。 3. 熟练掌握 HDFS Web UI 界面的使用...5. 使用 HDFS Java API 编写 HDFS 文件操作程序,实现上传本地文件到 HDFS 的功能。。。。。
java操作Hbase之从Hbase中读取数据写入hdfs中源码,附带全部所需jar包,欢迎下载学习。
HDFS的JAVA接口API操作实例 可以参考使用
数据科学导论 实验2:熟悉常用的HDFS操作 1. 编程实现以下指定功能,并利用 Hadoop 提供的 Shell 命令完成相同任务: 2. 编程实现一个类“MyFSDataInputStream”,该类继承“org.apache.hadoop.fs.FSDataInputStream...
适合刚接触hadoop的学生或小白,内容包括HDFS shell操作及HDFS Java API编程 有图片有代码
hdfs代码实例 通过Java代码在HDFS上创建目录、上传文件、下载文件、删除文件、文件重命名、文件列表等操作。
hadoop入门级的代码 Java编写 eclipse可运行 包含 hdfs的文件操作 rpc远程调用的简单示例 map-reduce的几个例子:wordcount 学生平均成绩 手机流量统计