使用java api操作Hadoop文件

本文详细介绍Hadoop文件系统的操作方法,包括文件的上传、创建、删除与读取,以及目录的创建、删除和文件列表读取等功能。适用于初学者快速上手。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 概述

2. 文件操作

2.1上传本地文件到hadoop fs

2.2 在hadoop fs中新建文件,并写入

2.3 删除hadoop fs上的文件

2.4读取文件

3. 目录操作

3.1 在hadoop fs上创建目录

3.2 删除目录

3.3 读取某个目录下的所有文件

4. 参考资料接代码下载

<1>. 概述

hadoop中关于文件操作类基本上全部是在org.apache.hadoop.fs包中,这些api能够支持的操作包含:打开文件,读写文件,删除文件等。

hadoop类库中最终面向用户提供的接口类是FileSystem,该类是个抽象类,只能通过来类的get方法得到具体类。get方法存在几个重载版本,常用的是这个:

static FileSystem get(Configuration conf);

该类封装了几乎所有的文件操作,例如mkdir,delete等。综上基本上可以得出操作文件的程序库框架:

operator()
{
得到Configuration对象
得到FileSystem对象
进行文件操作
}

另外需要注意的是,如果想要运行下面的程序的话,需要将程序达成jar包,然后通过hadoop jar的形式运行,这种方法比较麻烦,另外一种方法就是安装eclipse的hadoop插件,这样能够很多打包的时间。

<1>. 文件操作

1.1 上传本地文件到文件系统

/*
*uploadthelocalfiletothehds
*noticethatthepathisfulllike/tmp/test.c
*/
public static void uploadLocalFile2HDFS(Strings,Stringd)
throws IOException
{
Configurationconfig
= new Configuration();
FileSystemhdfs
= FileSystem.get(config);

Pathsrc
= new Path(s);
Pathdst
= new Path(d);

hdfs.copyFromLocalFile(src,dst);

hdfs.close();
}

1.2 创建新文件,并写入

/*
*createanewfileinthehdfs.

*noticethatthetoCreateFilePathisthefullpath
*andwritethecontenttothehdfsfile.
*/
public static void createNewHDFSFile(StringtoCreateFilePath,Stringcontent) throws IOException
{
Configurationconfig
= new Configuration();
FileSystemhdfs
= FileSystem.get(config);

FSDataOutputStreamos
= hdfs.create( new Path(toCreateFilePath));

os.write(content.getBytes(
" UTF-8 " ));

os.close();

hdfs.close();
}

1.3 删除文件

/*
*deletethehdfsfile
*noticethatthedstisthefullpathname
*/
public static boolean deleteHDFSFile(Stringdst) throws IOException
{
Configurationconfig
= new Configuration();
FileSystemhdfs
= FileSystem.get(config);

Pathpath
= new Path(dst);
boolean isDeleted = hdfs.delete(path);

hdfs.close();

return isDeleted;
}

1.4 读取文件

/**readthehdfsfilecontent
*noticethatthedstisthefullpathname
*/
public static byte []readHDFSFile(Stringdst) throws Exception
{
Configurationconf
= new Configuration();
FileSystemfs
= FileSystem.get(conf);

// checkifthefileexists
Pathpath = new Path(dst);
if (fs.exists(path))
{
FSDataInputStreamis
= fs.open(path);
// getthefileinfotocreatethebuffer
FileStatusstat = fs.getFileStatus(path);

// createthebuffer
byte []buffer = new byte [Integer.parseInt(String.valueOf(stat.getLen()))];
is.readFully(
0 ,buffer);

is.close();
fs.close();

return buffer;
}
else
{
throw new Exception( " thefileisnotfound. " );
}
}

<2>. 目录操作

2.1 创建目录

/**makeanewdirinthehdfs
*
*thedirmaylike'/tmp/testdir'
*/
public static void mkdir(Stringdir) throws IOException
{
Configurationconf
= new Configuration();
FileSystemfs
= FileSystem.get(conf);

fs.mkdirs(
new Path(dir));

fs.close();
}

2.2 删除目录

/**deleteadirinthehdfs
*
*dirmaylike'/tmp/testdir'
*/
public static void deleteDir(Stringdir) throws IOException
{
Configurationconf
= new Configuration();
FileSystemfs
= FileSystem.get(conf);

fs.delete(
new Path(dir));

fs.close();
}

2.3 读取某个目录下的所有文件

public static void listAll(Stringdir) throws IOException
{
Configurationconf = new Configuration();
FileSystemfs
= FileSystem.get(conf);

FileStatus[]stats
= fs.listStatus( new Path(dir));

for ( int i = 0 ;i < stats.length; ++ i)
{
if (stats[i].isFile())
{
// regularfile
System.out.println(stats[i].getPath().toString());
}
else if (stats[i].isDirectory())
{
// dir
System.out.println(stats[i].getPath().toString());
}
else if (stats[i].isSymlink())
{
// isssymlinkinlinux
System.out.println(stats[i].getPath().toString());
}

}
fs.close();
}

<4>. 参考资料及代码下载

/Files/xuqiang/HadoopFSOperations.rar


     
     

1. 本博客中的文章均是个人在学习和项目开发中总结。其中难免存在不足之处 ,欢迎留言指正。 2. 本文版权归作者和博客园共有,转载时,请保留本文链接。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值