1. 程式人生 > >CentOS系統下簡單的分散式(HDFS)資料夾建立,檔案上傳、下載等簡單操作

CentOS系統下簡單的分散式(HDFS)資料夾建立,檔案上傳、下載等簡單操作

目錄

Linux中eclipse建立分散式普通專案的基本操作

建立工具類及測試類


       這裡基於上一篇部落格(https://blog.csdn.net/gaofengyan/article/details/85790825)進行學習,hadoop和開發工具eclipse都已經安裝好並配置好了,接下來就簡單的資料夾建立、檔案上傳、下載、更改等操作簡單體驗一下分散式的普通專案玩法。因為也是初步學習,所以就以工具類建立為依託,以後也可以在這上面進行更改使用。

       首先開啟Linux桌面的eclipse,看到 eclipse 左邊的目錄是 Package Explorer ,這個目錄看不到我們的分散式(DFS Localtions)檔案目錄,需要開啟:Windows --> Show View --> Project Explorer ,啟動這個視角的目錄才能看到分散式的目錄結構:

         

Linux中eclipse建立分散式普通專案的基本操作

     New --> others --> Map/Reduce Project -- next

    

  填寫工程名:project name --> next --> finish --> yes  

   

  建立的hdfs001分散式專案如下圖所示,下面還有很多jar包(增加jar包,新建的mapreduce 專案帶所有jar包):

   

      在工程下面建立一個資原始檔 右鍵工程名 --> new --> source floder -->floder name --> finish:

 

       增加配置檔案:log4j.properties (日誌的配置檔案)、core-site.xml(分散式的工作主機等資訊配置檔案)  這兩個配置檔案在hadoop目錄下都有(這裡有個常識,有大部分配置檔案一般都在我們安裝的程式 【主目錄/etc/程式命令/下】下面可以直接找到,不用自己去寫,也可以直接引用):

[[email protected] hadoop]$ cp etc/hadoop/log4j.properties  /home/hduser/workspace/hdfs001/resources/
[[email protected] hadoop]$ cp etc/hadoop/core-site.xml  ~/workspace/hdfs001/resources/

這裡是第一次,展示一下core-site.xml配置檔案的資訊:

      core-site.xml(主要有分散式的主機工作名node1及埠號9000和分散式的目錄以及臨時檔案存放路徑)

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<!--
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License. See accompanying LICENSE file.
-->

<!-- Put site-specific property overrides in this file. -->

<configuration>
   <property>
       <name>fs.defaultFS</name>
       <value>hdfs://node1:9000</value>
   </property>
   <property>
       <name>hadoop.tmp.dir</name>
       <value>file:/home/hduser/hadoop/tmp</value>
   </property>
</configuration>

建立工具類及測試類

          這裡的操作跟在Windows下面的操作是大同小異的,沒多大區別,建立目錄,建立類,我這裡直接給程式碼。

       HdfsUtils.java 工具類

package org.kgc1803.netdisk.util;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

/**
 * hdfs 的 io 操作工具類
 * @author hduser
 *
 */
public class HdfsUtils {
	//提取公共的全域性靜態變數
	private static FileSystem fs;
	//靜態程式碼快(類第一次呼叫的時候產生物件)
	static{
		Configuration conf = new Configuration();
		try {
			fs = FileSystem.get(conf);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	//查詢一個目錄下的所有字檔案資訊 hadoop  dfs  -ls /
	public static FileStatus[] getFileInfo(Path rootPath){
		//FileStatus 顯示當前目錄子檔案資訊
		FileStatus[] files = null;
		try {
			//listStatus 顯示當前目錄子檔案資訊
				files = fs.listStatus(rootPath);
			 }catch (IOException e) {
				e.printStackTrace();
			}
		return files;
	}
	
	//新建一個目錄  hadoop fs  -mkdir /**
	public static void mkdir(Path p){
		try {
			if(!fs.exists(p)){
				fs.mkdirs(p);
			}else{
				System.out.println("目錄已存在");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	//上傳本地檔案到hdfs上
	//hadoop  dfs  -put  本地檔案  hdfs目錄
	//hadoop  dfs  -copyFromLocal  本地檔案  hdfs目錄
	public static void copyFromLocal(Path localPath,Path hdfsPath){
		try {
			fs.copyFromLocalFile(localPath, hdfsPath);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	//下載hdfs上的檔案到本地
	//hadoop  dfs  -get  本地檔案  hdfs目錄
	//hadoop  dfs  -copyToLocal  hdfs檔案  本地檔案
	public static void copyToLocal(Path hdfsPath,Path localPath){
		try {
			fs.copyToLocalFile(hdfsPath, localPath);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	//刪除 檔案或目錄
	//hadoop  dfs  -rmr hdfs檔案
	public static void remove(Path p){
		try {
			fs.delete(p, true);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	//重新命名檔案或目錄
	public static void rename(Path oldPath,Path newPath){
		try {
			fs.rename(oldPath, newPath);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

   測試類:

package cn.hdfs.demo;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.Before;
import org.junit.Test;

import cn.hdfs.demo.utils.HdfsUtils;

/**
 * 測試hdfs 工具類
 * @author hduser
 *
 */
public class TestHdfs {
	//定義全域性變數
	private FileSystem fs;
	
	//抽取公共的部分
	@Before
	public void before() throws IOException{
		//載入hdfs  核心配置檔案core-site.xml
		Configuration  conf  = new Configuration();
		//獲取檔案系統物件 FileSystem
		fs = FileSystem.get(conf);
	}
	
	//測試 檢視檔案資訊
	@Test
	public void fun1(){
			//檢視 hadoop dfs  -ls  / | hdfs://node1:9000/.....
			Path rootPath = new Path("hdfs://node1:9000/output/wc1");
			//listStatus 顯示當前目錄子檔案資訊
			HdfsUtils.getFileInfo(fs, rootPath);

	}
	
	//測試mkdir
	@Test
	public void fun2(){
		    //建立檔案 hadoop dfs -mkdir /  | hdfs://node1:9000/.....
			Path p = new Path("hdfs://node1:9000/hfile/");
			//listStatus 顯示當前目錄子檔案資訊
			HdfsUtils.mkdir(fs, p);
			//檢視建立的檔案
			HdfsUtils.getFileInfo(fs, new Path("hdfs://node1:9000/"));
	}
	
	//測試copyFromLocal 上傳本地檔案到hdfs上
	@Test
	public void fun3(){
		//hadoop  dfs  -copyFromLocal  本地檔案  hdfs目錄
		   //dfs目錄
			Path p = new Path("hdfs://node1:9000/hfile/");
			//本地檔案
			Path file = new Path("file:/home/hduser/桌面/aa.txt");
			HdfsUtils.copyFromLocal(fs, file, p);
			//檢視資訊
			HdfsUtils.getFileInfo(fs, new Path("hdfs://node1:9000/hfile"));
	}
	
	//測試copyToLocal 下載hdfs上的檔案到本地
	@Test
	public void fun4(){
		//hadoop  dfs  -copyToLocal  hdfs檔案  本地檔案
		//本地檔案路徑
		Path rootPath = new Path("file:/home/hduser/桌面/bbb");
		//hdfs的檔案
		Path hdfsfile = new Path("hdfs://node1:9000/input/1.txt");
		HdfsUtils.copyToLocal(fs, hdfsfile, rootPath);
		//檢視資訊
		HdfsUtils.getFileInfo(fs, rootPath);
		}
	
	//刪除 檔案或目錄
	@Test
	public void fun5(){
		//hadoop  dfs  -rmr hdfs檔案
		//hdfs的檔案
		Path hdfsfile = new Path("hdfs://node1:9000/hfile/");
		HdfsUtils.remove(fs, hdfsfile);
		//檢視資訊
		HdfsUtils.getFileInfo(fs, new Path("hdfs://node1:9000/"));
		}

}

測試之後,eclipse 左側的分散式目錄就會出現我們操作的資料夾目錄及檔案: