一、HDFS獲取文件系統(tǒng)
image.png
image.png
/**
* 打印本地hadoop地址值
* IO的方式寫代碼
*/
@Test
public void intiHDFS() throws IOException {
//F2 可以快速的定位錯誤
// alt + enter自動找錯誤
//1.創(chuàng)建配信信息對象 ctrl + alt + v 后推前 ctrl + shitl + enter 補全
Configuration conf = new Configuration();
//2.獲取文件系統(tǒng)
FileSystem fs = FileSystem.get(conf);
//3.打印文件系統(tǒng)
System.out.println(fs.toString());
}
image.png
跑一下:
image.png
二诗舰、HDFS文件上傳
/**
* 上傳代碼
* 注意:如果上傳的內(nèi)容大于128MB,則是2塊
*/
@Test
public void putFileToHDFS() throws Exception {
//注:import org.apache.hadoop.conf.Configuration;
//ctrl + alt + v 推動出對象
//1.創(chuàng)建配置信息對象
Configuration conf = new Configuration();
//2.設置部分參數(shù)
conf.set("dfs.replication","2");
//3.找到HDFS的地址
FileSystem fs = FileSystem.get(new URI("hdfs://bigdata131:9000"), conf, "root");
//4.上傳本地Windows文件的路徑
Path src = new Path("F:\\hello2.txt");
//5.要上傳到HDFS的路徑
Path dst = new Path("hdfs://bigdata131:9000/");
//6.以拷貝的方式上傳查近,從src -> dst
fs.copyFromLocalFile(src,dst);
//7.關閉
fs.close();
System.out.println("上傳成功");
}
需要的包為
import org.apache.hadoop.fs.Path;
import java.net.URI;
image.png
查看一下嘶炭,上傳成功了:
image.png
三、HDFS文件下載
/**
* hadoop fs -get /HDFS文件系統(tǒng)
* @throws Exception
*/
@Test
public void getFileFromHDFS() throws Exception {
//1.創(chuàng)建配置信息對象 Configuration:配置
Configuration conf = new Configuration();
//2.找到文件系統(tǒng)
//final URI uri :HDFS地址
//final Configuration conf:配置信息
// String user :Linux用戶名
FileSystem fs = FileSystem.get(new URI("hdfs://bigdata131:9000"), conf, "root");
//3.下載文件
//boolean delSrc:是否將原文件刪除
//Path src :要下載的路徑
//Path dst :要下載到哪
//boolean useRawLocalFileSystem :是否校驗文件
fs.copyToLocalFile(false,new Path("hdfs://bigdata131:9000/hello1.txt"),new Path("E:\\hello3.txt"),true);
//4.關閉fs
//alt + enter 找錯誤
//ctrl + alt + o 可以快速的去除沒有用的導包
fs.close();
System.out.println("下載成功");
}
image.png
可以在本地查看了:
image.png
注意點:
image.png
四歹鱼、HDFS目錄創(chuàng)建
/**
* hadoop fs -mkdir /xinshou
*/
@Test
public void mkmdirHDFS() throws Exception {
//1.創(chuàng)新配置信息對象
Configuration configuration = new Configuration();
//2.鏈接文件系統(tǒng)
//final URI uri 地址
//final Configuration conf 配置
//String user Linux用戶
FileSystem fs = FileSystem.get(new URI("hdfs://bigdata131:9000"), configuration, "root");
//3.創(chuàng)建目錄
fs.mkdirs(new Path("hdfs://bigdata131:9000/AncientMing"));
//4.關閉
fs.close();
System.out.println("創(chuàng)建文件夾成功");
}
image.png
查看一下:
image.png
五胆剧、HDFS文件夾刪除
/**
* hadoop fs -rm -r /文件
*/
@Test
public void deleteHDFS() throws Exception {
//1.創(chuàng)建配置對象
Configuration conf = new Configuration();
//2.鏈接文件系統(tǒng)
//final URI uri, final Configuration conf, String user
//final URI uri 地址
//final Configuration conf 配置
//String user Linux用戶
FileSystem fs = FileSystem.get(new URI("hdfs://bigdata131:9000"), conf, "root");
//3.刪除文件
//Path var1 : HDFS地址
//boolean var2 : 是否遞歸刪除
fs.delete(new Path("hdfs://bigdata131:9000/a"),false);
//4.關閉
fs.close();
System.out.println("刪除成功啦");
}
image.png
查看一下,果然沒了:
image.png
六、HDFS文件名更改
@Test
public void renameAtHDFS() throws Exception{
// 1 創(chuàng)建配置信息對象
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://bigdata131:9000"),configuration, "root");
//2 重命名文件或文件夾
fs.rename(new Path("hdfs://bigdata131:9000/AncientMing"), new Path("hdfs://bigdata131:9000/AncientMing3"));
fs.rename(new Path("hdfs://bigdata131:9000/hello2.txt"), new Path("hdfs://bigdata131:9000/hello3.txt"));
fs.close();
System.out.println("重命名一下~");
}
image.png
查看一下改了沒:
image.png
七秩霍、HDFS文件詳情查看
查看文件名稱篙悯、權(quán)限、長度铃绒、塊信息
/**
* 查看【文件】名稱鸽照、權(quán)限等
*/
@Test
public void readListFiles() throws Exception {
//1.創(chuàng)建配置對象
Configuration conf = new Configuration();
//2.鏈接文件系統(tǒng)
FileSystem fs = FileSystem.get(new URI("hdfs://bigdata131:9000"), conf, "root");
//3.迭代器
//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.
RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
//4.遍歷迭代器
while (listFiles.hasNext()){
//一個一個出
LocatedFileStatus fileStatus = listFiles.next();
//名字
System.out.println("文件名:" + fileStatus.getPath().getName());
//塊大小
System.out.println("大小:" + fileStatus.getBlockSize());
//權(quán)限
System.out.println("權(quán)限:" + fileStatus.getPermission());
System.out.println(fileStatus.getLen());
BlockLocation[] locations = fileStatus.getBlockLocations();
for (BlockLocation bl:locations){
System.out.println("block-offset:" + bl.getOffset());
String[] hosts = bl.getHosts();
for (String host:hosts){
System.out.println(host);
}
}
}
System.out.println("------------------我就看看----------------");
}
需要包:
import org.apache.hadoop.fs.RemoteIterator;
import org.apache.hadoop.fs.*;
image.png
八颠悬、HDFS文件和文件夾判斷
/**
* 判斷是否是個文件還是目錄矮燎,然后打印
* @throws Exception
*/
@Test
public void judge() throws Exception {
//1.創(chuàng)建配置文件信息
Configuration conf = new Configuration();
//2.獲取文件系統(tǒng)
FileSystem fs = FileSystem.get(new URI("hdfs://bigdata131:9000"), conf, "root");
//3.遍歷所有的文件
//List the statuses of the files/directories in the given path if the path is a directory.
FileStatus[] liststatus = fs.listStatus(new Path("/"));
for(FileStatus status :liststatus)
{
//判斷是否是文件
if (status.isFile()){
//ctrl + d:復制一行
//ctrl + x 是剪切一行,可以用來當作是刪除一行
System.out.println("文件:" + status.getPath().getName());
} else {
System.out.println("目錄:" + status.getPath().getName());
}
}
}
image.png
九赔癌、HDFS文件上傳
- 通過IO流操作HDFS
/**
* IO流方式上傳
*
* @throws URISyntaxException
* @throws FileNotFoundException
* @throws InterruptedException
*/
@Test
public void putFileToHDFSIO() throws URISyntaxException, IOException, InterruptedException {
//1.創(chuàng)建配置文件信息
Configuration conf = new Configuration();
//2.獲取文件系統(tǒng)
FileSystem fs = FileSystem.get(new URI("hdfs://bigdata131:9000"), conf, "root");
//3.創(chuàng)建輸入流
FileInputStream fis = new FileInputStream(new File("F:\\hello2.txt"));
//4.輸出路徑
//注意:不能/Andy 記得后邊寫個名 比如:/Andy/Sogou.txt
Path writePath = new Path("hdfs://bigdata131:9000/hello5.txt");
FSDataOutputStream fos = fs.create(writePath);
//5.流對接
//InputStream in 輸入
//OutputStream out 輸出
//int buffSize 緩沖區(qū)
//boolean close 是否關閉流
try {
IOUtils.copyBytes(fis,fos,4 * 1024,false);
} catch (IOException e) {
e.printStackTrace();
}finally {
IOUtils.closeStream(fos);
IOUtils.closeStream(fis);
System.out.println("上傳成功啦");
}
}
十诞外、HDFS文件下載
- 通過IO流操作HDFS
/**
* IO讀取HDFS到控制臺
*
* @throws URISyntaxException
* @throws IOException
* @throws InterruptedException
*/
@Test
public void getFileToHDFSIO() throws URISyntaxException, IOException, InterruptedException {
//1.創(chuàng)建配置文件信息
Configuration conf = new Configuration();
//2.獲取文件系統(tǒng)
FileSystem fs = FileSystem.get(new URI("hdfs://bigdata131:9000"), conf, "root");
//3.讀取路徑
Path readPath = new Path("hdfs://bigdata131:9000/hello5.txt");
//4.輸入
FSDataInputStream fis = fs.open(readPath);
//5.輸出到控制臺
//InputStream in 輸入
//OutputStream out 輸出
//int buffSize 緩沖區(qū)
//boolean close 是否關閉流
IOUtils.copyBytes(fis,System.out,4 * 1024 ,true);
}
十一、定位文件讀取
- 通過IO流操作HDFS
1灾票、下載第一塊
/**
* IO讀取第一塊的內(nèi)容
*
* @throws Exception
*/
@Test
public void readFlieSeek1() throws Exception {
//1.創(chuàng)建配置文件信息
Configuration conf = new Configuration();
//2.獲取文件系統(tǒng)
FileSystem fs = FileSystem.get(new URI("hdfs://bigdata131:9000"), conf, "root");
//3.輸入
Path path = new Path("hdfs://bigdata131:9000/hello5.txt");
FSDataInputStream fis = fs.open(path);
//4.輸出
FileOutputStream fos = new FileOutputStream("E:\\hello5.txt");
//5.流對接
byte[] buf = new byte[1024];
for (int i = 0; i < 128 * 1024; i++) {
fis.read(buf);
fos.write(buf);
}
//6.關閉流
IOUtils.closeStream(fos);
IOUtils.closeStream(fis);
}
2峡谊、下載第二塊
/**
* IO讀取第二塊的內(nèi)容
*
* @throws Exception
*/
@Test
public void readFlieSeek2() throws Exception {
//1.創(chuàng)建配置文件信息
Configuration conf = new Configuration();
//2.獲取文件系統(tǒng)
FileSystem fs = FileSystem.get(new URI("hdfs://bigdata131:9000"), conf, "root");
//3.輸入
Path path = new Path("hdfs://bigdata131:9000/hello5.txt");
FSDataInputStream fis = fs.open(path);
//4.輸出
FileOutputStream fos = new FileOutputStream("E:\\hello5.txt");
//5.定位偏移量/offset/游標/讀取進度 (目的:找到第一塊的尾巴,第二塊的開頭)
fis.seek(128 * 1024 * 1024);
//6.流對接
IOUtils.copyBytes(fis, fos, 1024);
//7.關閉流
IOUtils.closeStream(fos);
IOUtils.closeStream(fis);
}
3刊苍、合并文件
在window命令窗口中執(zhí)行
type A2 >> A1 然后更改后綴為rar即可
注意:需要塊夠大既们,才好測試。