FileSystem fileSystem = null;
public void init(){
// new Configuration()配置文件加载步骤----core-default.xml hdfs-default.xml core-site.xml hdfs-size.xml等文件
// 1、加载classpath下jar包中的配置文件
// 2、加载src下自定义的配置文件
// 3、加载代码中的配置
Configuration configuration = new Configuration();
// 本客户端上传的文件副本数量
//configuration.set("dfs.replication","2");
// 本客户端上传文件切块的大小
configuration.set("dfs.blocksize","10m");
try {
// 构造一个可以访问HDFS的客户端对象
// 参数1:NameNode的URI
// 参数2:客户端特别指定的参数
// 参数3:客户端的身份(用户名)
fileSystem = FileSystem.get(new URI("hdfs://192.168.184.132:9000/"),configuration,"dengbh");
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 拷贝文件到HDFS
* @throws IOException
*/
@Test
public void copyToHdfs() throws IOException {
init();
fileSystem.copyFromLocalFile(new Path("D:\\迅雷下载\\BaiduNetdisk_6.1.0.exe"), new Path("/dengbh/lalala.exe"));
fileSystem.close();
}
/**
* 从HDFS拷贝文件到本地
* @throws IOException
*/
@Test
public void copyFromHdfs() throws IOException {
init();
fileSystem.copyToLocalFile(new Path("/wordcount/output/wordcount.txt"), new Path("D:/"));
fileSystem.close();
}
/**
* 在HDFS移动文件/修改文件名
* @throws IOException
*/
@Test
public void rename() throws IOException {
init();
fileSystem.rename(new Path("/workcount/input/data/RestResult.txt"), new Path("/workcount/RestResult.txt"));
fileSystem.close();
}
/**
* 在HDFS中创建文件夹
* @throws IOException
*/
@Test
public void mkdirs() throws IOException {
init();
fileSystem.mkdirs(new Path("/dengbh/shenly"));
fileSystem.close();
}
/**
* 在HDFS中删除文件/文件夹
* @throws IOException
*/
@Test
public void delete() throws IOException {
init();
// 参数一:文件夹路径
// 参数二:是否递归删除
fileSystem.delete(new Path("/dengbh/shenly"), true);
fileSystem.close();
}
/**
* 获取文件信息
* @throws IOException
*/
@Test
public void getFileMessage() throws IOException {
init();
// 参数一:文件夹路径
// 参数二:是否递归删除
RemoteIterator<LocatedFileStatus> locatedFileStatusRemoteIterator = fileSystem.listFiles(new Path("/dengbh/lalala.exe"), true);
while (locatedFileStatusRemoteIterator.hasNext()){
LocatedFileStatus locatedFileStatus = locatedFileStatusRemoteIterator.next();
System.out.println("文件全路径:" + locatedFileStatus.getPath());
System.out.println("块信息:" + Arrays.toString(locatedFileStatus.getBlockLocations()));
System.out.println("块大小:" + locatedFileStatus.getBlockSize());
System.out.println("长度:" + locatedFileStatus.getLen());
System.out.println("副本数:" + locatedFileStatus.getReplication());
System.out.println("===============================================");
}
fileSystem.close();
}
/**
* 获取文件信息/文件夹信息
* @throws IOException
*/
@Test
public void listStatus() throws IOException {
init();
FileStatus[] status = fileSystem.listStatus(new Path("/logFile"));
for(FileStatus fileStatus : status){
System.out.println("文件全路径:" + fileStatus.getPath());
System.out.println(fileStatus.isDirectory() ? "这是文件夹" : "这是文件");
System.out.println("块大小:" + fileStatus.getBlockSize());
System.out.println("长度:" + fileStatus.getLen());
System.out.println("副本数:" + fileStatus.getReplication());
System.out.println("===============================================");
}
fileSystem.close();
}
/**
* 获取HDFS上文件的内容
*/
@Test
public void readHdfsFileData() throws IOException {
init();
FSDataInputStream fileData = fileSystem.open(new Path("/logFile/20180725/service_20180712.log"));
/* 通过字节读取
byte[] buf = new byte[1024];
int read = 0;
while((read = fileData.read(buf)) > 0){
System.out.println(new String(buf));
}*/
/* 通过字符流读取
*/
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileData));
String line = "";
while ((line = bufferedReader.readLine()) != null){
System.out.println(line);
}
bufferedReader.close();
fileData.close();
fileSystem.close();
}
/**
* 读取指定范围的内容
*/
@Test
public void readFileDateRandom() throws IOException {
init();
FSDataInputStream fileData = fileSystem.open(new Path("/logFile/20180725/service_20180712.log"));
// 从第几个字节开始
fileData.seek(6);
// 读取多少个字节
byte[] buf = new byte[10];
fileData.read(buf);
System.out.println(new String(buf));
fileData.close();
fileSystem.close();
}
/**
* 输出文件到HDFS
*/
@Test
public void writeHdfsFileData() throws IOException {
init();
FSDataOutputStream fs = fileSystem.create(new Path("/logFile/dengbh.jpg"));
String localFilePath = "D:\\1.JPEG";
FileInputStream fileInputStream = new FileInputStream(new File(localFilePath));
byte[] buff = new byte[1024];
int length;
while ((length = fileInputStream.read(buff)) != -1){
fs.write(buff, 0, length);
}
fileInputStream.close();
fs.close();
fileSystem.close();
}