package com.hlbdx;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.fs.permission.FsPermission;
import org.testng.annotations.Test;
import java.io.IOException;
/**
* @program: bigdata-demo01
* @description: 大数据实例1
* @author: Mr.ZhaoYong
* @create: 2020-01-13 11:50
**/
public class BigDataDemo1 {
@Test
public void mkdirToHdfs() throws IOException {
Configuration configuration = new Configuration();
configuration.set("fs.defaultFS","hdfs://node01:8020");
FileSystem fileSystem = FileSystem.get(configuration);
fileSystem.mkdirs(new Path("/kkb/dir"));
fileSystem.close();
}
@Test
public void uploadFile() throws IOException {
Configuration configuration = new Configuration();
configuration.set("fs.defaultFS","hdfs://node01:8020");
FileSystem fileSystem = FileSystem.get(configuration);
fileSystem.copyFromLocalFile(new Path("F:\testfile\aa.txt"),
new Path("hdfs://node01:8020/kkb/dir"));
fileSystem.close();
}
@Test
public void downloadFile() throws IOException {
Configuration configuration = new Configuration();
configuration.set("fs.defaultFS","hdfs://node01:8020");
FileSystem fileSystem = FileSystem.get(configuration);
java打代码基础 fileSystem.copyToLocalFile(new Path("hdfs://node01:8020/kkb/dir/aa.txt"),
new Path("F:\testfile\bb.txt"));
fileSystem.close();
}
@Test
public void delFile() throws IOException {
Configuration configuration = new Configuration();
configuration.set("fs.defaultFS","hdfs://node01:8020");
FileSystem fileSystem = FileSystem.get(configuration);
fileSystem.deleteOnExit(new Path("hdfs://node01:8020/kkb/dir/aa.txt"));
fileSystem.close();
}
@Test
public void renameFile() throws IOException {
Configuration configuration = new Configuration();
configuration.set("fs.defaultFS","hdfs://node01:8020");
FileSystem fileSystem = FileSystem.get(configuration);
fileSystem.rename(new Path("hdfs://node01:8020/kkb/dir/aa.txt"),
new Path("hdfs://node01:8020/kkb/dir/bb.xml"));
fileSystem.close();
}
@Test
public void listFiles() throws IOException {
Configuration configuration = new Configuration();
configuration.set("fs.defaultFS","hdfs://node01:8020");
FileSystem fileSystem = FileSystem.get(configuration);
RemoteIterator<LocatedFileStatus> locatedFileStatusRemoteIterator = fileSystem.listFiles(new Path("/"), true);
while (locatedFileStatusRemoteIterator.hasNext()){
LocatedFileStatus next = locatedFileStatusRemoteIterator.next();
String name = next.getPath().getName();
System.out.println("name = " + name);
long len = next.getLen();
System.out.println("len = " + len);
FsPermission permission = next.getPermission();
System.out.println("permission = " + permission);
String group = next.getGroup();
System.out.println("group = " + group);
BlockLocation[] blockLocations = next.getBlockLocations();
for (BlockLocation blk:blockLocations){
String[] cachedHosts = blk.getHosts();
for(String host:cachedHosts){
System.out.println("host = " + host);
}
}
}
fileSystem.close();
}
版权声明:
本文来源网络,所有图片文章版权属于原作者,如有侵权,联系删除。
本文网址:https://www.bianchenghao6.com/h6javajc/26143.html