HDFS怎么利用JAVA进行操作

  介绍

这篇文章给大家介绍HDFS怎么利用JAVA进行操作,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

Jar包引入,砰的一声。xml:

& lt; dependency>,   ,& lt; groupId> org.apache.hadoop,   ,& lt; artifactId> hadoop-common,   ,& lt; version> 2.8.0,   时间/dependency> & lt;   & lt; dependency>,   ,& lt; groupId> org.apache.hadoop,   ,& lt; artifactId> hadoop-hdfs,   ,& lt; version> 2.8.0,   & lt;/dependency>

将本地文件上传到hdfs服务器:

/* *,   ,*上传文件到hdfs上,   ,*/@Test    public  void 上传(),throws  IOException  {,   Configuration 才能;conf =, new 配置(),,   conf.set才能(“fs.defaultFS",“hdfs://hzq: 9000“),,   FileSystem 才能;fs =, FileSystem.get(会议),,   fs.copyFromLocalFile才能(new 路径(“/home/hzq/jdk1.8.tar.gz"), new 路径(“/demo")),,   }

解析:

在开发中我没有引入“core-site.xml”配置文件,所以在本地调用时使用配置进行配置“conf.set (“fs.defaultFS",“hdfs://hzq: 9000“);“,下面雷同。

将hdfs上文件下载到本地:

/* *,   ,*将hdfs上文件下载到本地,   ,*/@Test    public  void 下载(),throws  IOException  {,   Configuration 才能;conf =, new 配置(),,   conf.set才能(“fs.defaultFS",“hdfs://hzq: 9000“),,   FileSystem 才能;fs =, FileSystem.newInstance(会议),,   fs.copyToLocalFile才能(new 路径(“/java/jdk1.8.tar.gz") new 路径(“/home/hzq/?),,   }

删除hdfs上指定文件:

/* *,   ,*删除hdfs上的文件,   ,* @throws  IOException    ,*/@Test    public  void  removeFile (), throws  IOException  {,   Configuration 才能;conf =, new 配置(),,   conf.set才能(“fs.defaultFS",“hdfs://hzq: 9000“),,   FileSystem 才能;fs =, FileSystem.newInstance(会议),,   fs.delete才能(new 路径(“/演示/jdk1.8.tar.gz"),真的),,   }

在hdfs上创建文件夹:

/* *,   ,*在hdfs更目录下面创建test1文件夹,   ,* @throws  IOException    ,*/@Test    public  void  mkdir (), throws  IOException  {,   Configuration 才能;conf =, new 配置(),,   conf.set才能(“fs.defaultFS",“hdfs://hzq: 9000“),,   FileSystem 才能;fs =, FileSystem.newInstance(会议),,   fs.mkdirs才能(new 路径(“/test1")),,   }

列出hdfs上所有的文件或文件夹:

@Test    public 才能;void  listFiles (), throws  IOException  {,   ,,,Configuration  conf =, new 配置();,   ,,,conf.set (“fs.defaultFS",“hdfs://hzq: 9000“),,   ,,,FileSystem  fs =, FileSystem.newInstance(参看);,   ,,,//,true 表示递归查找,false 不进行递归查找,   ,,,RemoteIterator, iterator =, fs.listFiles (new 路径(“/?,真的),,   ,,,while  (iterator.hasNext ()) {,   ,,,,,LocatedFileStatus  next =, iterator.next (),,   ,,,,,System.out.println (next.getPath ()),,   ,,,},   ,,,System.out.println (“- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -“),,   ,,,FileStatus [], fileStatuses =, fs.listStatus (new 路径(“/?),,   ,,,for  (int 小姐:=,0;,小姐:& lt;, fileStatuses.length;,我+ +),{,   ,,,,,FileStatus  FileStatus =, fileStatuses[我];,   ,,,,,System.out.println (fileStatus.getPath ()),,   ,,,},   以前,,}

运行结果:

 HDFS怎么利用JAVA进行操作“>,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,</p> <p> <强>结果分析:<br/> </强> </p> <p>“listFiles”列出的是HDFS上所有文件的路径,不包括文件夹。根据你的设置,支持递归查找。<br/> </p> <p>,null<h2 class=HDFS怎么利用JAVA进行操作