public class TestHDFS {
public static void main(String[] args) throws IOException {
Configuration conf = new Configuration(true);
FileSystem fs = FileSystem.get(conf);
List list = listFileSystem(fs,"/");
createDir(fs,"/test/abc");
uploadFileToHDFS(fs,"d:/wc","/test/abc/");
downLoadFileFromHDFS(fs,"/test/abc/wc","d:/");
renameFile(fs,"/test/abc/wc","/test/abc/Angelababy");
innerCopyAndMoveFile(fs,conf,"/test/abc/Angelababy","/");
createNewFile(fs,"/test/abc/hanhong");
writeToHDFSFile(fs,"/test/abc/hanhong","hello world");
appendToHDFSFile(fs,"/test/abc/hanhong","\nhello world");
readFromHDFSFile(fs,"/test/abc/hanhong");
getFileLocation(fs,"/install.log");
}
private static void getFileLocation(FileSystem fs, String string) throws IOException {
FileStatus fileStatus = fs.getFileStatus(new Path(string));
long len = fileStatus.getLen();
BlockLocation[] fileBlockLocations = fs.getFileBlockLocations(fileStatus, 0, len);
String[] hosts = fileBlockLocations[0].getHosts();
for (String string2 : hosts) {
System.out.println(string2);
}
HdfsBlockLocation blockLocation = (HdfsBlockLocation)fileBlockLocations[0];
long blockId = blockLocation.getLocatedBlock().getBlock().getBlockId();
System.out.println(blockId);
}
private static void readFromHDFSFile(FileSystem fs, String string) throws IllegalArgumentException, IOException {
FSDataInputStream inputStream = fs.open(new Path(string));
FileStatus fileStatus = fs.getFileStatus(new Path(string));
long len = fileStatus.getLen();
byte[] b = new byte[(int)len];
int read = inputStream.read(b);
while(read != -1){
System.out.println(new String(b));
read = inputStream.read(b);
}
}
private static void appendToHDFSFile(FileSystem fs, String filePath, String content) throws IllegalArgumentException, IOException {
FSDataOutputStream append = fs.append(new Path(filePath));
append.write(content.getBytes("UTF-8"));
append.flush();
append.close();
}
private static void writeToHDFSFile(FileSystem fs, String filePath, String content) throws IllegalArgumentException, IOException {
FSDataOutputStream outputStream = fs.create(new Path(filePath));
outputStream.write(content.getBytes("UTF-8"));
outputStream.flush();
outputStream.close();
}
private static void createNewFile(FileSystem fs, String string) throws IllegalArgumentException, IOException {
fs.createNewFile(new Path(string));
}
private static void innerCopyAndMoveFile(FileSystem fs, Configuration conf,String src, String dest) throws IOException {
Path srcPath = new Path(src);
Path destPath = new Path(dest);
FileUtil.copy(srcPath.getFileSystem(conf), srcPath, destPath.getFileSystem(conf), destPath,true, conf);
}
private static void renameFile(FileSystem fs, String src, String dest) throws IOException {
Path srcPath = new Path(src);
Path destPath = new Path(dest);
fs.rename(srcPath, destPath);
}
private static void downLoadFileFromHDFS(FileSystem fs, String src, String dest) throws IOException {
Path srcPath = new Path(src);
Path destPath = new Path(dest);
fs.copyToLocalFile(true,srcPath, destPath);
}
private static void uploadFileToHDFS(FileSystem fs, String src, String dest) throws IOException {
Path srcPath = new Path(src);
Path destPath = new Path(dest);
fs.copyFromLocalFile(true,srcPath, destPath);
}
private static void createDir(FileSystem fs, String string) throws IllegalArgumentException, IOException {
Path path = new Path(string);
if(fs.exists(path)){
fs.delete(path, true);
}
fs.mkdirs(path);
}
private static List listFileSystem(FileSystem fs, String path) throws FileNotFoundException, IOException {
Path ppath = new Path(path);
FileStatus[] listStatus = fs.listStatus(ppath);
for (FileStatus fileStatus : listStatus) {
System.out.println(fileStatus.getPath());
}
return null;
}
}