package hadoop;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.permission.FsPermission;
import org.junit.Test;
import java.io.IOException;
import java.util.Date;
public class test {
/*
* 上传下载
* linux命令:hdfs dfs -copyToLocal /baidu/demo.tar.gz D:/
* */
@Test
public void copyToLocalFile() throws IOException {
//1. 初始化配置文件
Configuration entries = new Configuration();
entries.set("fs.defaultFS", "hdfs://192.168.72.10:9000");
//2. 获得hdfs操作客户端
FileSystem fileSystem = FileSystem.get(entries);
fileSystem.copyToLocalFile(new Path("/baidu"), new Path("D:/"));// 拷贝到本地。
//3. 释放资源
fileSystem.close();
}
/**
* 文件上传
* linux命令: hdfs dfs -copyFromLocal F:/test.json /baidu
*/
@Test
public void put() throws IOException {
//1. 初始化配置
Configuration entries = new Configuration();
// nameNode 的入口地址
entries.set("fs.defaultFS","hdfs://192.168.72.10:9000");
// 副本因子
entries.set("dfs.replication", "1");
// 2. 获得操作hdfs的客户端。
FileSystem fileSystem = FileSystem.get(entries);
fileSystem.copyFromLocalFile(new Path("F:\\百知经历\\大数据\\大数据2006\\MapReduce\\作业\\day2\\Flowdata.txt"), new Path("/Flowdata.txt"));
fileSystem.close();
}
/**
* 创建目录
* 命令: hdfs dfs -mkdir -p /目录a/目录b
*/
@Test
public void mkbir() throws IOException {
//1. 初始化配置
Configuration entries = new Configuration();
// nameNode 的入口地址
entries.set("fs.defaultFS","hdfs://192.168.72.10:9000");
// 2. 获得操作hdfs的客户端。
FileSystem fileSystem = FileSystem.get(entries);
boolean mkdirs = fileSystem.mkdirs(new Path("/JSON"));
System.out.println(mkdirs ? "创建成功" : "创建失败");
fileSystem.close();
}
/**
* 删除目录或文件
* 命令: hdfs dfs -rm -r hdfs 文件夹 hdfs dfs -rm hdfs 文件
*/
@Test
public void rm() throws IOException {
//1. 初始化配置
Configuration entries = new Configuration();
// nameNode 的入口地址
entries.set("fs.defaultFS","hdfs://192.168.72.10:9000");
// 2. 获得操作hdfs的客户端。
FileSystem fileSystem = FileSystem.get(entries);
boolean delete = fileSystem.delete(new Path("/OutNameCount"), true);// true为递归删除
System.out.println(delete ? "删除成功" : "删除失败");
fileSystem.close();
}
/**
* 判断文件是否存在
* 命令:
*/
@Test
public void exists() throws IOException {
//1. 初始化配置
Configuration entries = new Configuration();
// nameNode 的入口地址
entries.set("fs.defaultFS","hdfs://192.168.72.10:9000");
// 2. 获得操作hdfs的客户端。
FileSystem fileSystem = FileSystem.get(entries);
boolean exists = fileSystem.exists(new Path("/baidu"));
System.out.println(exists ? "存在" : "不存在");
fileSystem.close();
}
/**
* 查看文件元数据信息
* 命令: hdfs dfs -ls [-R] hdfs文件路径
*/
@Test
public void listStatus() throws IOException {
//1. 初始化配置
Configuration entries = new Configuration();
// nameNode 的入口地址
entries.set("fs.defaultFS","hdfs://192.168.72.10:9000");
// 2. 获得操作hdfs的客户端。
FileSystem fileSystem = FileSystem.get(entries);
// 获得目录下所有文件的
FileStatus[] fileStatuses = fileSystem.listStatus(new Path("/OutNameCount"));
for (FileStatus fileStatus : fileStatuses) {
System.err.println("fileStatus = " + fileStatus);
Path path = fileStatus.getPath();// 获得文件路径
System.out.println("文件路径 path = " + path);
FsPermission permission = fileStatus.getPermission();// 获得文件权限
System.out.println("文件权限 permission = " + permission);
short replication = fileStatus.getReplication();// 获取文件副本数
System.out.println("文件副本数 replication = " + replication);
long modificationTime = fileStatus.getModificationTime();// 获得文件修改时间,long的时间戳
System.out.println("long的时间戳 modificationTime = " + new Date(modificationTime));
long len = fileStatus.getLen();// 获得文件大小
System.out.println("文件大小 len = " + len);
}
fileSystem.close();
}
}