日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDFS的API操作-获取FileSystem方式

發布時間:2024/4/13 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDFS的API操作-获取FileSystem方式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

HDFS 的 API 操作

導入 Maven 依賴

<repositories><repository><id>cloudera</id><url>https://repository.cloudera.com/artifactory/cloudera-repos/</url></repository> </repositories> <dependencies><dependency><groupId>jdk.tools</groupId><artifactId>jdk.tools</artifactId><version>1.8</version><scope>system</scope><systemPath>${JAVA_HOME}/lib/tools.jar</systemPath></dependency><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-common</artifactId><version>3.0.0</version><scope>provided</scope></dependency><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-hdfs</artifactId><version>3.0.0</version></dependency><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-hdfs-client</artifactId><version>3.0.0</version><scope>provided</scope></dependency><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-client</artifactId><version>3.0.0</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency> </dependencies>

概述

在 Java 中操作 HDFS, 主要涉及以下 Class:

  • Configuration

    • 該類的對象封轉了客戶端或者服務器的配置
  • FileSystem

    • 該類的對象是一個文件系統對象, 可以用該對象的一些方法來對文件進行操作, 通過 FileSystem 的靜態方法 get 獲得該對象

      ? ? FileSystem fs = FileSystem.get(conf)
      • get 方法從 conf 中的一個參數 fs.defaultFS 的配置值判斷具體是什么類型的文件系統
      • 如果我們的代碼中沒有指定 fs.defaultFS, 并且工程 ClassPath 下也沒有給定相應的配置, conf 中的默認值就來自于 Hadoop 的 Jar 包中的 core-default.xml
      • 默認值為 file:///, 則獲取的不是一個 DistributedFileSystem 的實例, 而是一個本地文件系統的客戶端對象

獲取 FileSystem 的幾種方式

第一種方式

@Test public void getFileSystem() throws URISyntaxException, IOException {Configuration configuration = new Configuration();FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.52.250:8020"), configuration);System.out.println(fileSystem.toString()); }

第二種方式

@Test public void getFileSystem2() throws URISyntaxException, IOException {Configuration configuration = new Configuration();configuration.set("fs.defaultFS","hdfs://192.168.52.250:8020");FileSystem fileSystem = FileSystem.get(new URI("/"), configuration);System.out.println(fileSystem.toString()); }

第三種方式

@Test public void getFileSystem3() throws URISyntaxException, IOException {Configuration configuration = new Configuration();FileSystem fileSystem = FileSystem.newInstance(new URI("hdfs://192.168.52.250:8020"), configuration);System.out.println(fileSystem.toString()); }

第四種方式

@Test public void getFileSystem4() throws Exception{Configuration configuration = new Configuration();configuration.set("fs.defaultFS","hdfs://192.168.52.250:8020");FileSystem fileSystem = FileSystem.newInstance(configuration);System.out.println(fileSystem.toString()); }

?

?

?

?

總結

以上是生活随笔為你收集整理的HDFS的API操作-获取FileSystem方式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。