最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java?API操作Hdfs的示例詳解

 更新時(shí)間:2022年08月24日 11:35:49   作者:bitcarmanlee  
這篇文章主要介紹了Java?API操作Hdfs詳細(xì)示例,遍歷當(dāng)前目錄下所有文件與文件夾,可以使用listStatus方法實(shí)現(xiàn)上述需求,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下

1.遍歷當(dāng)前目錄下所有文件與文件夾

可以使用listStatus方法實(shí)現(xiàn)上述需求。
listStatus方法簽名如下

  /**
   * List the statuses of the files/directories in the given path if the path is
   * a directory.
   * 
   * @param f given path
   * @return the statuses of the files/directories in the given patch
   * @throws FileNotFoundException when the path does not exist;
   *         IOException see specific implementation
   */
  public abstract FileStatus[] listStatus(Path f) throws FileNotFoundException, 
                                                         IOException;

可以看出listStatus只需要傳入?yún)?shù)Path即可,返回的是一個(gè)FileStatus的數(shù)組。
而FileStatus包含有以下信息

/** Interface that represents the client side information for a file.
 */
@InterfaceAudience.Public
@InterfaceStability.Stable
public class FileStatus implements Writable, Comparable {

  private Path path;
  private long length;
  private boolean isdir;
  private short block_replication;
  private long blocksize;
  private long modification_time;
  private long access_time;
  private FsPermission permission;
  private String owner;
  private String group;
  private Path symlink;
  ....

從FileStatus中不難看出,包含有文件路徑,大小,是否是目錄,block_replication, blocksize…等等各種信息。

import org.apache.hadoop.fs.{FileStatus, FileSystem, Path}
import org.apache.spark.sql.SparkSession
import org.apache.spark.{SparkConf, SparkContext}
import org.slf4j.LoggerFactory

object HdfsOperation {
	
	val logger = LoggerFactory.getLogger(this.getClass)
	
	def tree(sc: SparkContext, path: String) : Unit = {
		val fs = FileSystem.get(sc.hadoopConfiguration)
		val fsPath = new Path(path)
		val status = fs.listStatus(fsPath)
		for(filestatus:FileStatus <- status) {
			logger.error("getPermission is: {}", filestatus.getPermission)
			logger.error("getOwner is: {}", filestatus.getOwner)
			logger.error("getGroup is: {}", filestatus.getGroup)
			logger.error("getLen is: {}", filestatus.getLen)
			logger.error("getModificationTime is: {}", filestatus.getModificationTime)
			logger.error("getReplication is: {}", filestatus.getReplication)
			logger.error("getBlockSize is: {}", filestatus.getBlockSize)
			if (filestatus.isDirectory) {
				val dirpath = filestatus.getPath.toString
				logger.error("文件夾名字為: {}", dirpath)
				tree(sc, dirpath)
			} else {
				val fullname = filestatus.getPath.toString
				val filename = filestatus.getPath.getName
				logger.error("全部文件名為: {}", fullname)
				logger.error("文件名為: {}", filename)
			}
		}
	}
}

如果判斷fileStatus是文件夾,則遞歸調(diào)用tree方法,達(dá)到全部遍歷的目的。

2.遍歷所有文件

上面的方法是遍歷所有文件以及文件夾。如果只想遍歷文件,可以使用listFiles方法。

	def findFiles(sc: SparkContext, path: String) = {
		val fs = FileSystem.get(sc.hadoopConfiguration)
		val fsPath = new Path(path)
		val files = fs.listFiles(fsPath, true)
		while(files.hasNext) {
			val filestatus = files.next()
			val fullname = filestatus.getPath.toString
			val filename = filestatus.getPath.getName
			logger.error("全部文件名為: {}", fullname)
			logger.error("文件名為: {}", filename)
			logger.error("文件大小為: {}", filestatus.getLen)
		}
	}
  /**
   * List the statuses and block locations of the files in the given path.
   * 
   * If the path is a directory, 
   *   if recursive is false, returns files in the directory;
   *   if recursive is true, return files in the subtree rooted at the path.
   * If the path is a file, return the file's status and block locations.
   * 
   * @param f is the path
   * @param recursive if the subdirectories need to be traversed recursively
   *
   * @return an iterator that traverses statuses of the files
   *
   * @throws FileNotFoundException when the path does not exist;
   *         IOException see specific implementation
   */
  public RemoteIterator<LocatedFileStatus> listFiles(
      final Path f, final boolean recursive)
  throws FileNotFoundException, IOException {
  ...

從源碼可以看出,listFiles 返回一個(gè)可迭代的對(duì)象RemoteIterator<LocatedFileStatus>,而listStatus返回的是個(gè)數(shù)組。同時(shí),listFiles返回的都是文件。

3.創(chuàng)建文件夾

	def mkdirToHdfs(sc: SparkContext, path: String) = {
		val fs = FileSystem.get(sc.hadoopConfiguration)
		val result = fs.mkdirs(new Path(path))
		if (result) {
			logger.error("mkdirs already success!")
		} else {
			logger.error("mkdirs had failed!")
		}
	}

4.刪除文件夾

	def deleteOnHdfs(sc: SparkContext, path: String) = {
		val fs = FileSystem.get(sc.hadoopConfiguration)
		val result = fs.delete(new Path(path), true)
		if (result) {
			logger.error("delete already success!")
		} else {
			logger.error("delete had failed!")
		}
	}

5.上傳文件

	def uploadToHdfs(sc: SparkContext, localPath: String, hdfsPath: String): Unit = {
		val fs = FileSystem.get(sc.hadoopConfiguration)
		fs.copyFromLocalFile(new Path(localPath), new Path(hdfsPath))
		fs.close()
	}

6.下載文件

	def downloadFromHdfs(sc: SparkContext, localPath: String, hdfsPath: String) = {
		val fs = FileSystem.get(sc.hadoopConfiguration)
		fs.copyToLocalFile(new Path(hdfsPath), new Path(localPath))
		fs.close()
	}

到此這篇關(guān)于Java API操作Hdfs詳細(xì)示例的文章就介紹到這了,更多相關(guān)Java API操作Hdfs內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java中斷機(jī)制實(shí)例講解

    java中斷機(jī)制實(shí)例講解

    這篇文章主要介紹了java中斷機(jī)制實(shí)例講解,用了風(fēng)趣幽默的講法,有對(duì)這方面不太懂的同學(xué)可以研究下
    2021-01-01
  • Java線程池ThreadPoolExecutor原理及使用實(shí)例

    Java線程池ThreadPoolExecutor原理及使用實(shí)例

    這篇文章主要介紹了Java線程池ThreadPoolExecutor原理及使用實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • Spring Boot快速入門教程

    Spring Boot快速入門教程

    本篇文章主要介紹了Spring Boot快速入門教程,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-01-01
  • Java 將Excel轉(zhuǎn)為OFD格式(方法步驟)

    Java 將Excel轉(zhuǎn)為OFD格式(方法步驟)

    OFD是一種開放版式文檔是我國國家版式文檔格式標(biāo)準(zhǔn),本文通過Java后端程序代碼展示如何將Excel轉(zhuǎn)為OFD格式,分步驟給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧
    2021-12-12
  • Java Web開發(fā)項(xiàng)目中中文亂碼解決方法匯總

    Java Web開發(fā)項(xiàng)目中中文亂碼解決方法匯總

    這篇文章主要為大家詳細(xì)匯總了Java Web開發(fā)項(xiàng)目中中文亂碼的解決方法,分析了5種Java Web中文亂碼情況,感興趣的小伙伴們可以參考一下
    2016-05-05
  • Java hashCode() 方法詳細(xì)解讀

    Java hashCode() 方法詳細(xì)解讀

    Java.lang.Object 有一個(gè)hashCode()和一個(gè)equals()方法,這兩個(gè)方法在軟件設(shè)計(jì)中扮演著舉足輕重的角色,本文對(duì)hashCode()方法深入理解,希望能幫助大家
    2016-07-07
  • javaweb實(shí)現(xiàn)注冊登錄頁面

    javaweb實(shí)現(xiàn)注冊登錄頁面

    這篇文章主要為大家詳細(xì)介紹了javaweb實(shí)現(xiàn)注冊登錄頁面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • 詳解SpringMVC中使用Interceptor攔截器

    詳解SpringMVC中使用Interceptor攔截器

    SpringMVC 中的Interceptor 攔截器也是相當(dāng)重要和相當(dāng)有用的,它的主要作用是攔截用戶的請(qǐng)求并進(jìn)行相應(yīng)的處理,這篇文章主要介紹了詳解SpringMVC中使用Interceptor攔截器,有興趣的可以了解一下。
    2016-12-12
  • netty對(duì)proxy protocol代理協(xié)議的支持詳解

    netty對(duì)proxy protocol代理協(xié)議的支持詳解

    這篇文章主要為大家介紹了netty對(duì)proxy protoco代理協(xié)議的支持詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • Springboot集成Springbrick實(shí)現(xiàn)動(dòng)態(tài)插件的步驟詳解

    Springboot集成Springbrick實(shí)現(xiàn)動(dòng)態(tài)插件的步驟詳解

    這篇文章主要介紹了Springboot集成Springbrick實(shí)現(xiàn)動(dòng)態(tài)插件的詳細(xì)過程,文中的流程通過代碼示例介紹的非常詳細(xì),感興趣的同學(xué)可以參考一下
    2023-06-06

最新評(píng)論

吴堡县| 始兴县| 柘城县| 吐鲁番市| 清镇市| 阜新市| 深水埗区| 濮阳县| 剑川县| 交城县| 竹山县| 石柱| 永嘉县| 宁德市| 明溪县| 紫云| 宁晋县| 海淀区| 基隆市| 乌鲁木齐县| 胶南市| 天台县| 肃北| 新平| 郴州市| 北海市| 泌阳县| 浦县| 定襄县| 福泉市| 林州市| 贵定县| 瑞安市| 达孜县| 常熟市| 盈江县| 海门市| 西和县| 正宁县| 临朐县| 麻栗坡县|