中国最大的博客网站,全媒体运营师报考官网在哪里,菜单 wordpress,美萍企业管理软件目录1、搭建开发环境2、获取api中的客户端对象3、DistributedFileSystem实例对象所具备的方法4、HDFS客户端操作数据代码示例  目录 
1、搭建开发环境 
window下开发的说明:  A、在windows的某个目录下解压一个hadoop的安装包  B、将安装包下的lib和bin目录用对应windows版本平…目录1、搭建开发环境2、获取api中的客户端对象3、DistributedFileSystem实例对象所具备的方法4、HDFS客户端操作数据代码示例  目录 
1、搭建开发环境 
window下开发的说明:  A、在windows的某个目录下解压一个hadoop的安装包  B、将安装包下的lib和bin目录用对应windows版本平台编译的本地库替换  这里我的环境是win10、hadoop2.6.4。  对应的win10的编译的本地库下载地址http://pan.baidu.com/s/1dFzYVKH  密码:pdj9  C、在window系统中配置HADOOP_HOME指向你解压的安装包  D、在windows系统的path变量中加入hadoop的bin目录  E、根据需要将hadoop下的jar包导入对应的工程中中生成新的Jar包开发什么程序打成什么jar包。如开发HDFS程序就把hadoop下hdfs下对应的jar包打成一个新的jar包供工程使用 
2、获取api中的客户端对象 
在java中操作hdfs首先要获得一个客户端实例 
Configuration conf  new Configuration() 
FileSystem fs  FileSystem.get(conf) 
而我们的操作目标是HDFS所以获取到的fs对象应该是DistributedFileSystem的实例  get方法是从何处判断具体实例化那种客户端类呢  ——从conf中的一个参数 fs.defaultFS的配置值判断 
如果我们的代码中没有指定fs.defaultFS并且工程classpath下也没有给定相应的配置conf中的默认值就来自于hadoop的jar包中的core-default.xml默认值为 file:///则获取的将不是一个DistributedFileSystem的实例而是一个本地文件系统的客户端对象 
3、DistributedFileSystem实例对象所具备的方法 4、HDFS客户端操作数据代码示例 
4.1、文件的增删改查 
/*** * 客户端去操作hdfs时是有一个用户身份的* 默认情况下hdfs客户端api会从jvm中获取一个参数来作为自己的用户身份
-DHADOOP_USER_NAMEhadoop* 也可以在构造客户端fs对象时通过参数传递进去* author**/
public class HdfsClient {FileSystem fs  null;Beforepublic void init() throws Exception {// 构造一个配置参数对象设置一个参数我们要访问的hdfs的URI// 从而FileSystem.get()方法就知道应该是去构造一个访问hdfs文件系统的客户端以及hdfs的访问地址// new Configuration();的时候它就会去加载jar包中的hdfs-default.xml// 然后再加载classpath下的hdfs-site.xmlConfiguration conf  new Configuration();conf.set(fs.defaultFS, hdfs://hdp-node01:9000);/*** 参数优先级 1、客户端代码中设置的值 2、classpath下的用户自定义配置文件 3、然后是服务器的默认配置*/// 获取一个hdfs的访问客户端根据参数这个实例应该是DistributedFileSystem的实例// fs  FileSystem.get(conf);// 如果这样去获取那conf里面就可以不要配fs.defaultFS参数而且这个客户端的身份标识已经是hadoop用户// fs  FileSystem.get(new URI(hdfs://hdp-node01:9000), conf, hadoop);}/*** 往hdfs上传文件** throws Exception*/Testpublic void testAddFileToHdfs() throws Exception {// 要上传的文件所在的本地路径Path src  new Path(g:/redis-recommend.zip);// 要上传到hdfs的目标路径Path dst  new Path(/aaa);fs.copyFromLocalFile(src, dst);fs.close();}/*** 从hdfs中复制文件到本地文件系统** throws IOException* throws IllegalArgumentException*/Testpublic void testDownloadFileToLocal() throws IllegalArgumentException, IOException {fs.copyToLocalFile(new Path(/jdk-7u65-linux-i586.tar.gz), new Path(d:/));fs.close();}Testpublic void testMkdirAndDeleteAndRename() throws IllegalArgumentException, IOException {// 创建目录fs.mkdirs(new Path(/a1/b1/c1));// 删除文件夹 如果是非空文件夹参数2必须给值truefs.delete(new Path(/aaa), true);// 重命名文件或文件夹fs.rename(new Path(/a1), new Path(/a2));}/*** 查看目录信息只显示文件** throws IOException* throws IllegalArgumentException* throws FileNotFoundException*/Testpublic void testListFiles() throws FileNotFoundException, IllegalArgumentException, IOException {RemoteIteratorLocatedFileStatus listFiles  fs.listFiles(new Path(/), true);while (listFiles.hasNext()) {LocatedFileStatus fileStatus  listFiles.next();System.out.println(fileStatus.getPath().getName());System.out.println(fileStatus.getBlockSize());System.out.println(fileStatus.getPermission());System.out.println(fileStatus.getLen());BlockLocation[] blockLocations  fileStatus.getBlockLocations();for (BlockLocation bl : blockLocations) {System.out.println(block-length:  bl.getLength()  --  block-offset:  bl.getOffset());String[] hosts  bl.getHosts();for (String host : hosts) {System.out.println(host);}}System.out.println(--------------为angelababy打印的分割线--------------);}}/*** 查看文件及文件夹信息** throws IOException* throws IllegalArgumentException* throws FileNotFoundException*/Testpublic void testListAll() throws FileNotFoundException, IllegalArgumentException, IOException {FileStatus[] listStatus  fs.listStatus(new Path(/));String flag  d--             ;for (FileStatus fstatus : listStatus) {if (fstatus.isFile())  flag  f--         ;System.out.println(flag  fstatus.getPath().getName());}}
}