1 JAVA-HDFS-API
1.1 maven快速入門 (專案構建工具)
1 構建專案
2 管理專案的依賴的jar包


使用本地的一個檔案夾來存盤jar包 稱為本地倉庫

在d盤創建檔案夾

將settting.xml檔案復制到和repository平級的目錄下

修改setting檔案的55行 , 改成自己的目錄

創建一個專案 然后修改本地倉庫的位置


點擊重繪按鈕 等待下載插件完畢

添加專案的依賴
<dependencies>
<!-- mysql的驅動包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
</dependencies>
1.2 創建maven專案 操作HDFS
1 添加依賴
2 創建類 修改類的模板
按照順序打開File–>settings–>Editor–>File and Code Templates–>Includes
/** * @Classname ${NAME} * @Description TODO * @Date ${DATE} ${TIME} * @Created by ${USER} */

1,3 入門程式
1.3.1 添加依賴
<build>
<plugins>
<!--編譯插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!--添加hdfs的客戶端依賴-->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-core</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-common</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-jobclient</artifactId>
<version>3.1.1</version>
</dependency>
</dependencies>
1.3.2 入門程式
/**
* @Classname Demo2
* @Date 2020/9/20 0020 11:03
* @Created by 多易教育-DOIT18
* @Description:
* 使用hadoop提供的就java API 操作HDFS系統
* 1 匯入HDFS的依賴
* 2 獲取一個代表 HDFS檔案系統的物件
* 1) URI hdfs://linux01:8020
* 2) 用戶自定義設定引數
* 3 物件.方法操作
* 4 釋放資源
* ls
* put
* get
* rm
* mkdir
*/
public class Demo2 {
public static void main(String[] args) throws Exception {
// 1創建一個配置物件
Configuration conf = new Configuration();
// 2 創建一個代碼分布式檔案系統的物件
/**
* 1 不能直接new 有創建實體的靜態方法
* 2三個引數
* 2.1 引數一 指定的HDFS檔案系統的URI
* 2.2 配置資訊物件
* 2.3 windows上運行程式 , 用戶名 root
*/
FileSystem fs = FileSystem.newInstance(new URI("hdfs://linux01:8020"), conf, "root");
// 3 呼叫方法
// 創建層級檔案夾
fs.mkdirs(new Path("/aaa/bbb/ccc/ddd"));
//4 釋放資源
fs.close();
}
}
1.4 創建檔案夾
/**
* 創建檔案夾
* @throws Exception
*/
private static void testMkdirs() throws Exception {
FileSystem fs = DoitUtils.getHDFSfs();
fs.mkdirs(new Path("/a/b/c")) ;
fs.close();
}
1.5 洗掉內容
private static void testDelete2(FileSystem fs) throws IOException {
fs.delete(new Path("/a"), true);
}
/**
* 洗掉檔案夾
* @throws Exception
*/
private static void testDelete() throws Exception {
FileSystem fs = DoitUtils.getHDFSfs();
testDelete2(fs);
fs.close();
}
1.6 判斷路徑是否存在
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
URI uri = new URI("hdfs://linux01:8020");
String user = "root" ;
FileSystem fs = FileSystem.newInstance(uri, conf, user);
boolean exists = fs.exists(new Path("/aaa"));
System.out.println(exists);
fs.close();
}
1.7 工具類封裝
/**
* @Classname DoitUtils
* @Date 2020/9/20 0020 11:49
* @Created by 多易教育-DOIT18
* @Description:
* 1 獲取 hdfs://linux01:8020 檔案系統的客戶端物件
*/
public class DoitUtils {
/**
* 獲取 分布式檔案系統客戶端
* @return
* @throws Exception
*/
public static FileSystem getHDFSfs() throws Exception {
Configuration conf = new Configuration();
URI uri = new URI("hdfs://linux01:8020");
String user = "root" ;
FileSystem fs = FileSystem.newInstance(uri, conf, user);
return fs ;
}
}
1.8 上傳
private static void put(FileSystem fs) throws IOException {
// 將本地的檔案上傳到HDFS中
/**
* 引數一是否洗掉源檔案
* 引數二 是否覆寫已有的檔案
* 引數三 要上傳的檔案 Path[]
* 引數四 HDFS的目標位置
*/
Path[] ps = new Path[]{new Path("d://word.txt") , new Path("d://a.txt")} ;
fs.copyFromLocalFile(false , true , ps,new Path("/"));
}
1.9 下載
在下載檔案的時候需要在本地配置HADOOP的系統環境變數
解壓壓縮檔案,配置系統環境變數

private static void get(FileSystem fs) throws IOException {
// 下載 從HDFS 下載內容到windows 需要在windows上配置HDP的環境
/**
* 引數一 是否洗掉HDFS上要下載的檔案
* 引數二 HDFS上要下載的檔案
* 引數三 本地的路徑
* 引數四 是否生成校驗檔案 默認是false true 不會生成校驗檔案
*/
fs.copyToLocalFile(false , new Path("/a.txt") , new Path("f://"),true);
}
1.10 配置詳解

/**
* @Classname Config
* @Date 2020/9/20 0020 14:45
* @Created by 多易教育-DOIT18
* @Description:
* 用戶想自定義配置引數 Configuration用于用戶的定制化設定
* 上傳 6個副本
* 1 可以使用Configuration來自定義設定引數
* 2 不設定采用默認的配置引數
* 3 如果用戶在代碼中沒有設定引數 但是在專案的resources檔案夾下有hdfs-site.xml core-site.xml hdfs-default.xml
* 就會自動的讀取配置引數
* 順序
* 代碼中 >>> 組態檔 >>>>默認的
*
* 范圍
* 默認的 >>> 組態檔 >>> 代碼中
*/
public class Config {
public static void main(String[] args) throws Exception {
//
Configuration conf = new Configuration();
//設定存盤副本的個數
// conf.set("dfs.replication" ,"2" );
// 設定物理切塊的大小
// conf.set("dfs.blocksize","64M");
FileSystem fs = FileSystem.newInstance(new URI("hdfs://linux01:8020"), conf, "root");
fs.copyFromLocalFile(new Path("d://mrdata.zip") ,new Path("/aa/a.zip"));
fs.close();
}
}
1.11 讀取資料
/**
* @Classname ReadHdfsData
* @Date 2020/9/20 0020 15:24
* @Created by 多易教育-DOIT18
* @Description: open() 輸入的位元組流
* 使用java讀取hdfs上的檔案資料(輸入流)
* 1 輸入流
* 位元組流
* 字符流
* 轉換流
* 2 讀取資料 隨機讀 skip seek
* 3 記錄讀取資料的長度
* 4 line.length()不包含換行符 +1 / +2
*
*/
public class ReadHdfsData {
public static void main(String[] args) throws Exception {
FileSystem fs = DoitUtils.getHDFSfs();
FSDataInputStream fis = fs.open(new Path("/word.txt"));
fis.seek(3); // skip
/* fis.read();
fis.read();
fis.read();
int i = fis.read();
System.out.println(i); //97*/
// 使用緩沖字符流
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
long len = 0 ;
String line = null ;
while((line=br.readLine())!=null){
// line.length() 一行資料的內容長度 每行需要+ 換行長度 在windows中+2 \r\n 在linux中的檔案 +1 \r
len+= line.length()+ 2 ;
if(len >= 100){
break ;
}
System.out.println(line);
}
System.out.println(len);
fis.close();
fs.close();
}
}
1.12 寫資料
不能隨機寫 ,創建一個檔案寫 , 向檔案的后面追加寫
private static void append() throws Exception {
FileSystem fs = DoitUtils.getHDFSfs();
//append 向檔案的末尾追加內容
FSDataOutputStream fout = fs.append(new Path("/word.txt"));
fout.write("nihao".getBytes());
fout.close();
fs.close();
}
private static void testCreate(FileSystem fs) throws IOException {
// create 創建一個檔案獲取輸出流
//引數1 hdfs的檔案
// 引數2 是否要覆寫寫 默認是true false 不允許覆寫寫
FSDataOutputStream fout = fs.create(new Path("/a.txt"),true);
fout.write("hello".getBytes());
fout.close();
}
1.13 遍歷路徑下所有的檔案
/**
* @Classname ListDemo
* @Date 2020/9/20 0020 16:30
* @Created by 多易教育-DOIT18
* @Description: fs.listFiles
* 遍歷指定路徑寫下所有的檔案
* 1 獲取檔案名
* 2 獲取檔案的路徑
* 3 獲取檔案的長度
* 4 檔案的副本個數
* 5 獲取檔案的物理切塊
* 6 獲取修改時間 訪問時間 獲取擁有者 組 權限
* & ****獲取檔案的元資料資訊
*/
public class ListDemo {
public static void main(String[] args) throws Exception {
FileSystem fs = DoitUtils.getHDFSfs();
/**
* 列出指定目錄下所有的檔案
* 引數一路徑
* 引數二 是否遞回遍歷
*/
RemoteIterator<LocatedFileStatus> iterator = fs.listFiles(new Path("/"), false);
while(iterator.hasNext()){
// 代表一個檔案
LocatedFileStatus fileStatus = iterator.next();
// 獲取檔案路徑和檔案名
Path path = fileStatus.getPath();
String name = path.getName();
long len = fileStatus.getLen();
//System.out.println(path+"-->"+name+":"+len);
// 檔案的物理切塊大小
long blockSize = fileStatus.getBlockSize();
short replication = fileStatus.getReplication();
System.out.println(name+"--"+blockSize/1024/1024+"M"+"--:"+replication) ;
}
fs.close();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/102790.html
標籤:其他
上一篇:【機器學習15】決策樹模型詳解

