首页 > 编程知识 正文

hadoop权限控制api,hadoop菜鸟入门

时间:2023-05-04 23:03:00 阅读:142800 作者:786

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类库中最终为用户提供的接口类是文件系统,该类是抽象类,只能从来到类中的get方法中获得特定类。 get方法有几个重载版本,这是最常用的:

静态文件系统(configuration conf );

此类几乎封装了所有文件操作,包括mkdir和delete。 根据以上内容,基本上可以得到对文件进行操作的库框架。

操作器() )

{

得到配置对象

获得文件系统对象

进行文件操作

}

此外,如果尝试运行以下程序,则必须将该程序打包为jar包,然后以hadoop jar形式运行: 这个方法很麻烦。 另一种方法是安装eclipse的hadoop插件,这样可以将更多的时间用于打包。

1 .文件操作

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

/**uploadthelocalfiletothehds

* noticethatthepathisfulllike/tmp/test.c */publicstaticvoiduploadlocalfile2HDFS (strings,Stringd ) throwsioexceptioption

{

配置配置=new配置(;

filesystemhdfs=file system.get (config );

pathsrc=newpath(s );

pathdst=newpath(d );

HDFS.copyfromlocalfile(src,dst );

hdfs.close (;

}

1.2创建并写入新文件

/*

*createanewfileinthehdfs。

* noticethatthetocreatefilepathisthefullpath

* andwritethecontenttothehdfsfile。

*/publicstaticvoidcreatenewhdfsfile (stringtocreatefilepath,Stringcontent ) throwsIOException

{

配置配置=new配置(;

filesystemhdfs=file system.get (config );

fdataoutputstreamos=HDFS.create (new path ) (tocreatefilepath );

OS.write(content.getbytes ) (utf-8 ) );

os.close (;

hdfs.close (;

}

1.3删除文件

/* *deletethehdfsfile

* noticethatthedstisthefullpathname */publicstaticbooleandeletehdfsfile (string dst ) throwsIOException

{

配置配置=new配置(;

filesystemhdfs=file system.get (config );

pathpath=newpath(dst; 布尔is deleted=HDFS.delete (path );

hdfs.close (; 返回is deleted;

}

1.4文件读取

/* * readthehdfsfilecontent * noticethatthedstisthefullpathname

* /公共静态字节[ ] readhdfsfile (string dst ) throwsException

{

配置conf=new configuration (;

文件系统fs=文件系统. get (conf; //checkifthefileexistspathpath=new path (dst; if(fs.Exists(path ) )

{

fsdatainputstreamis=fs.open (path; //getthefileinfotocreatethebufferfilestatusstat=fs.getfilestatus (path ); //createthebufferbyte [ ] buffer=new byte [ integer.parseint (string.value of (stat.getlen ) ) ];

is.readfully(0,buffer );

is.close (;

fs.close (; 返回缓冲器;

} else { throw new exception (' thefileisnotfound.' );

}

}

2 .目录操作

2.1创建目录

/**makeanewdirinthehdfs*

*thedirmaylike'/tmp/testdir '

*/publicstaticvoidmkdir (string dir ) throwsIOException

{

配置conf=new configuration (;

文件系统fs=文件系统. get (conf;

fs.mkdirs(newpath ) dir );

fs.close (;

}

2.2删除目录

/**deleteadirinthehdfs

*

* dirmaylike '/tmp/testdir ' */publicstaticvoiddeletedir (string dir ) throwsIOException

{

配置conf=new configuration (;

文件系统fs=文件系统. get (conf;

fs.delete(newpath ) dir );

fs.close (;

}

2.3读取目录中的所有文件

publicstaticvoidlistall (string dir ) throwsIOException

{

配置conf=new configuration (;

文件系统fs=文件系统. get (conf;

filestatus [ ] stats=fs.list status (new path (dir ) ); for(inti=0; I

{if(stats[I].isfile ) )

{//regular file system.out.println (stats [ I ].getpath (.tostring ) );

}elseif(stats[I].isdirectory () )

//dirsystem.out.println (stats [ I ].getpath ().toString ) );

}elseif(stats[I].issymlink ) )

{//issymlinkinlinuxsystem.out.println (stats [ I ].getpath (.tostring ) );

}

}

fs.close (;

}

4 .下载参考资料和代码

注意:处理hdfs上的文件时,必须将hadoop-core和common-log jar包添加到classpath中,以便在本地运行。 对于mapreduce程序,必须将程序打包为jar包,然后将其上载到hdfs才能运行。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。