设为首页 加入收藏

TOP

利用hadoop的API编写java代码
2018-12-03 08:53:32 】 浏览:58
Tags:利用 hadoop API 编写 java 代码

问题:利用hadoop的API编写java代码,在hdfs文件系统的根目录下创建一个文件夹custom,将本地文件file1.txt上传至HDFS的custom中,并在HDFS文件系统中对文件进行删除、复制、移动等操作,并测试是否成功



package kgc;


import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
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 org.apache.zookeeper.common.IOUtils;


public class HdfsHandle {
/**
* 设置hadoop HDFS初始化配置方法
* @throws IOException
*/
public static FileSystem init() throws IOException{
Configuration config=new Configuration();
config.set("fs.defaultFS", "hdfs://192.168.9.154:8020/");
FileSystem fs=FileSystem.get(config);
return fs;
}


/**
* 在HDFS中创建文件
* @throws IOException
*/
public static void mkdir(String dir) throws IOException{
FileSystem fs=init();
Path dst=new Path(dir);
boolean isOk=fs.mkdirs(dst);
if(isOk){
System.out.println("目录创建成功");
}else{
System.out.println("目录创建失败");
}
fs.close();
}

/**
* 在HDFS中创建一个空文件,并写东西
* @throws IOException
*/
public static void createFile(String fileName,byte[] contents) throws IOException{
FileSystem fs=init();
Path dst=new Path(fileName);
FSDataOutputStream fsos=fs.create(dst);
fsos.write(contents);
fsos.close();
fs.close();
}

/**
* 重命名
* @throws IOException
*/
public static void rename(String srcName,String dstName) throws IOException{
FileSystem fs=init();
Path src=new Path(srcName);
Path dst=new Path(dstName);
fs.rename(src, dst);
fs.close();
}

/**
* 上传文件到HDFS
* @param uploadPath 上传文件所在路径
* @param hdfsPath 上传到的HDFS路径
* @throws IOException
*/
public static void uploadFileHdfs(String uploadPath,String hdfsPath) throws IOException{
FileSystem fs=init();
Path src=new Path(uploadPath);
Path dst=new Path(hdfsPath);
fs.copyFromLocalFile(src, dst);
fs.close();
}

/**
* 删除HDFS中的文件
* @param deletePath
* @throws IOException
*/
public static void deleteFileHdfs(String deletePath) throws IOException{
FileSystem fs=init();
Path dePath=new Path(deletePath);
boolean isOk=fs.deleteOnExit(dePath);
if(isOk){
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
}

/**
* 删除文件或目录
* @param dirName
* @throws IOException
*/
public static void deleteDir(String dirName) throws IOException{
FileSystem fs=init();
Path dePath=new Path(dirName);
boolean isOk=fs.delete(dePath);
if(isOk){
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
}

/**
* 将HDFS上文件复制到HDFS上,可以换名字
* @param srcFile
* @param dstFile
* @throws IOException
*/
public static void copyFile(String srcFile,String dstFile) throws IOException{
FileSystem fs=init();
FSDataInputStream fsis=fs.open(new Path(srcFile));;
FSDataOutputStream fsos=fs.create(new Path(dstFile));
byte[] byt=new byte[1024];
int a=0;
while((a=fsis.read(byt))!=-1){
fsos.write(byt,0,a);
}
fsis.close();
fsos.close();
fs.close();
}

/**
* 读取文件内容
* @param dstPath
* @return
* @throws IOException
*/
public static void readFile(String dstPath) throws IOException{
FileSystem fs=init();
Path path=new Path(dstPath);
if(fs.exists(path)){
FSDataInputStream in=fs.open(path);
IOUtils.copyBytes(in, System.out, 1024);
IOUtils.closeStream(in);
fs.close();
}
}

public static void main(String[] args) throws IOException {
mkdir("/test"); //在HDFS中创建文件
// byte[] byt="welcome to kgc".getBytes();
// createFile("hdfs://192.168.9.154:8020/custom/test1.txt",byt); //在HDFS中创建一个空文件,并写东西
// rename("hdfs://192.168.9.154:8020/custom/test1.txt","hdfs://192.168.9.154:8020/custom/test2.txt"); // 重命名
// uploadFileHdfs("/home/hadoop/app/hadoop-2.6.0-cdh5.7.0/file/file1.txt", "hdfs://192.168.9.154:8020/custom"); //上传文件到HDFS
// deleteFileHdfs("hdfs://192.168.9.154:8020/custom/test1.txt"); //删除HDFS中的文件
// deleteDir("hdfs://192.168.9.154:8020/custom"); //删除文件或目录
// copyFile("hdfs://192.168.9.154:8020/custom/test1.txt","hdfs://192.168.9.154:8020/custom/test2.txt"); //将HDFS上文件复制到HDFS上,可以换名字
// readFile("hdfs://192.168.9.154:8020/custom/file1.txt"); //读取文件内容
}

}

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇(hadoop运维 二) 避免hadoop节点.. 下一篇java+hadoop实现文件操作

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目