您的位置:首页 > 编程语言 > Java开发

java 操作hdfs api

2017-12-04 18:03 489 查看
package com.ecloud;

import java.io.IOException;

import java.net.URI;

import java.net.URISyntaxException;

import org.apache.commons.compress.utils.IOUtils;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.fs.FSDataOutputStream;

import org.apache.hadoop.fs.FileStatus;

import org.apache.hadoop.fs.FileSystem;

import org.apache.hadoop.fs.Path;

import com.twitter.chill.Base64.InputStream;

public class Test_hdfs {
// 创建一个新文件
public static void createFile(byte[] contents) throws IOException, URISyntaxException {
URI uri = new URI("hdfs://192.168.237.120:8020/test");

Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://192.168.237.120:8020");

FileSystem fs = FileSystem.get(conf);
Path dstPath = new Path(uri);
// 打开一个输出流
FSDataOutputStream outputStream = fs.create(dstPath);
outputStream.write(contents);
outputStream.close();
fs.close();
System.out.println("文件创建成功!");

}

// 上传本地文件
public static void uploadFile(String src, String dst) throws IOException {
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://192.168.237.120:8020");
FileSystem fs = FileSystem.get(conf);
Path srcPath = new Path(src);
Path dstPath = new Path(dst);
// 调用文件系统的文件复制函数,前面参数是指是否删除原文件,true为删除,默认为false
fs.copyFromLocalFile(false, srcPath, dstPath);

// 打印文件路径
System.out.println("Upload to " + conf.get("fs.default.name"));
System.out.println("------------list files------------" + "\n");
FileStatus[] fileStatus = fs.listStatus(dstPath);
for (FileStatus file : fileStatus) {
System.out.println(file.getPath());
}
fs.close();
}

// 文件重命名
public static void rename(String oldName, String newName) throws IOException {
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://192.168.237.120:8020");
FileSystem fs = FileSystem.get(conf);
Path oldPath = new Path(oldName);
Path newPath = new Path(newName);
boolean isok = fs.rename(oldPath, newPath);
if (isok) {
System.out.println("rename ok!");
} else {
System.out.println("rename failure");
}
fs.close();
}

// 删除文件
public static void delete(String filePath) throws IOException {
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://192.168.237.120:8020");
FileSystem fs = FileSystem.get(conf);
Path path = new Path(filePath);
boolean isok = fs.deleteOnExit(path);
if (isok) {
System.out.println("delete ok!");
} else {
System.out.println("delete failure");
}
fs.close();
}

// 创建目录
public static void mkdir(String path) throws IOException {
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://192.168.237.120:8020");
FileSystem fs = FileSystem.get(conf);
Path srcPath = new Path(path);
boolean isok = fs.mkdirs(srcPath);
if (isok) {
System.out.println("create dir ok!");
} else {
System.out.println("create dir failure");
}
fs.close();
}

public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
// 测试创建文件
createFile("ni hao ".getBytes());

// 测试上传文件
uploadFile("C:\\Users\\Administrator\\Desktop\\info_ber.txt", "/user/hdfs/test/");

// 测试重命名
rename("/user/hdfs/test/info_ber.txt", "/user/hdfs/test/dinfo_ber.txt");
// 测试删除文件
// delete("test/dinfo_ber.txt"); //使用相对路径
// delete("test1"); //删除目录
// 测试新建目录
// mkdir("test1");

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: