知识点
相关文章
更多最近更新
更多使用Java API操作Hadoop文件
2019-03-28 14:19|来源: 网络
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 上传本地文件到文件系统
* upload the local file to the hds
* notice that the path is full like /tmp/test.c
*/
public static void uploadLocalFile2HDFS(String s, String d)
throws IOException
{
Configuration config = new Configuration();
FileSystem hdfs = FileSystem.get(config);
Path src = new Path(s);
Path dst = new Path(d);
hdfs.copyFromLocalFile(src, dst);
hdfs.close();
}
1.2 创建新文件,并写入
* create a new file in the hdfs.
* notice that the toCreateFilePath is the full path
* and write the content to the hdfs file.
*/
public static void createNewHDFSFile(String toCreateFilePath, String content) throws IOException
{
Configuration config = new Configuration();
FileSystem hdfs = FileSystem.get(config);
FSDataOutputStream os = hdfs.create( new Path(toCreateFilePath));
os.write(content.getBytes( " UTF-8 " ));
os.close();
hdfs.close();
}
1.3 删除文件
* delete the hdfs file
* notice that the dst is the full path name
*/
public static boolean deleteHDFSFile(String dst) throws IOException
{
Configuration config = new Configuration();
FileSystem hdfs = FileSystem.get(config);
Path path = new Path(dst);
boolean isDeleted = hdfs.delete(path);
hdfs.close();
return isDeleted;
}
1.4 读取文件
* notice that the dst is the full path name
*/
public static byte [] readHDFSFile(String dst) throws Exception
{
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
// check if the file exists
Path path = new Path(dst);
if ( fs.exists(path) )
{
FSDataInputStream is = fs.open(path);
// get the file info to create the buffer
FileStatus stat = fs.getFileStatus(path);
// create the buffer
byte [] buffer = new byte [Integer.parseInt(String.valueOf(stat.getLen()))];
is.readFully( 0 , buffer);
is.close();
fs.close();
return buffer;
}
else
{
throw new Exception( " the file is not found . " );
}
}
<2>. 目录操作
2.1 创建目录
*
* the dir may like '/tmp/testdir'
*/
public static void mkdir(String dir) throws IOException
{
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
fs.mkdirs( new Path(dir));
fs.close();
}
2.2 删除目录
*
* dir may like '/tmp/testdir'
*/
public static void deleteDir(String dir) throws IOException
{
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
fs.delete( new Path(dir));
fs.close();
}
2.3 读取某个目录下的所有文件
{
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
FileStatus[] stats = fs.listStatus( new Path(dir));
for ( int i = 0 ; i < stats.length; ++ i)
{
if (stats[i].isFile())
{
// regular file
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())
{
// is s symlink in linux
System.out.println(stats[i].getPath().toString());
}
}
fs.close();
}
<4>. 参考资料及代码下载
免费下载地址在 http://linux.linuxidc.com/
用户名与密码都是www.linuxidc.com
具体下载目录在 /pub/2011/11/12/使用Java API操作Hadoop文件/
相关问答
更多-
众所周知,java在处理数据量比较大的时候,加载到内存必然会导致内存溢出,而在一些数据处理中我们不得不去处理海量数据,在做数据处理中,我们常见的手段是分解,压缩,并行,临时文件等方法; 例如,我们要将数据库(不论是什么数据库)的数据导出到一个文件,一般是Excel或文本格式的CSV;对于Excel来讲,对于POI和JXL的接口,你很多时候没有办法去控制内存什么时候向磁盘写入,很恶心,而且这些API在内存构造的对象大小将比数据原有的大小要大很多倍数,所以你不得不去拆分Excel,还好,POI开始意识到这个问题 ...
-
两个问题,16行提示命令不存在,你看看你写的是什么 第二个,找不到java文件夹,你看下是不是路径写错了
-
恭喜啊,学习hadoop需要先学好命令啊。 想学Linux命令就去www.linuxsky.cn,里面也可以学习脚本和svn的命令,哈哈
-
hadoop的MapReduce程序运行操作问题[2022-03-24]
都可以,简单的直接用txt打开java文件,写好后打包成class文件,就可以运行了。你看他原来在哪里放class文件的,你就放在那里 -
java.api操作hadoop的hdfs需要什么权限吗?[2022-06-15]
不知道你说的“调用Filesystem命令”是什么意思;使用hadoop fs -rm可以正常执行,MapReduce也可以正常读写HDFS文件,说明HDFS文件系统没有问题。你不妨试一试hadoop fs -chmod a+rwx设置rwx权限后,再试一次。 -
求大神指导Java操作Hadoop的一个问题[2021-12-07]
java.lang.OutOfMemoryError: Java heap space at org.apache.hadoop.mapred.MapTask$MapOutputBuffer 减小输入文件的大小试试 -
java.api操作hadoop的hdfs需要什么权限吗?[2021-11-10]
不知道你说的“调用Filesystem命令”是什么意思;使用hadoop fs -rm可以正常执行,MapReduce也可以正常读写HDFS文件,说明HDFS文件系统没有问题。你不妨试一试hadoop fs -chmod a+rwx 设置rwx权限后,再试一次。 -
您在寻找这种地图信息吗? 每个shell命令的Java类: factory.addClass(Merge.class, "-getmerge"); factory.addClass(Cp.class, "-cp"); factory.addClass(CopyFromLocal.class, "-copyFromLocal"); factory.addClass(CopyToLocal.class, "-copyToLocal"); factory.addClass(Get.class, "-get"); ...
-
我认为你需要实施 你自己的输出格式类和 你自己的RecordWriter会将不同的值写入不同的地方 所以你的SomeOutputWriter将在其getRecordWriter()方法中返回new SomeRecordWriter("SomeOutputPrefix") ,而SomeRecordWriter会将不同的值写入不同的文件夹。 I think you need to implement your own output format class and your own RecordWriter w ...
-
如何使用Java有效地读取Hadoop(HDFS)文件中的第一行?(How to read first line in Hadoop (HDFS) file efficiently using Java?)[2022-11-06]
您可以使用FileSystem类和朋友访问hdfs文件: import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URI; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataInputStream; import org.apache.hadoop.fs.Path; import org.apa ...