package com.zhiyou.bd23;
import java.io.FileNotFoundException;
import java.io.IOException;
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.hadoop.fs.UnsupportedFileSystemException;
import org.apache.hadoop.fs.permission.FsAction;
import org.apache.hadoop.fs.permission.FsPermission;
import org.apache.hadoop.security.AccessControlException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HdfsUtils {
private static Logger logger = LoggerFactory.getLogger(HdfsUtils.class);
public static Configuration configuration = new Configuration();
public static FileSystem hdfs;
//静态代码块中初始化hdfs对象
static {
try {
hdfs = FileSystem.get(configuration);
} catch (Exception e) {
logger.error("hdfs连接异常");
e.printStackTrace();
}
}
//在hdfs上创建一个人目录
public static void createDir(String path) {
Path p = new Path(path);
try {
boolean result = hdfs.mkdirs(p);
if (result) {
logger.info("创建成功了"+path);
}
} catch (Exception e) {
logger.error("创建目录异常"+path);
logger.debug(e.getMessage());
logger.debug(e.getStackTrace().toString());
}
}
//删除一个目录或文件,删除之前判断参数路径是目录还是文件,删除成功后提示删除目录或文件成功
public static void deleteDir(String path) {
Path p =new Path(path);
try {
if (hdfs.exists(p)) {
if (hdfs.isFile(p)) {
logger.info("删除文件"+path);
}else {
logger.info("删除目录"+path);
}
hdfs.delete(p,true);
}else {
logger.info("路径不存在"+path);
}
} catch (Exception e) {
e.printStackTrace();
}
}
//把hdfs上的文件下载到本地路径
public static void downloadFromHdfs(String hdfsPath,String localPath) {
Path src = new Path(hdfsPath);
Path dst = new Path(localPath);
try {
hdfs.copyToLocalFile(false,src,dst,true);
logger.info("下载成功"+localPath);
} catch (Exception e) {
logger.debug("下载异常");
e.printStackTrace();
}
}
//把文件上传到hdfs上
public static void uploadFromHdfs(String localPath,String hdfsPath) {
Path dst = new Path(localPath);
Path src = new Path(hdfsPath);
try {
hdfs.copyFromLocalFile(dst, src);
logger.info("上传成功"+hdfsPath);
} catch (Exception e) {
logger.debug("上传异常");
e.printStackTrace();
}
}
//读取hdfs上的文件
public static void readFileOnHdfs(String path) {
Path p =new Path(path);
try {
if (hdfs.exists(p)) {
if (hdfs.isFile(p)) {
FSDataInputStream inputStream = hdfs.open(p);
String str = inputStream.readUTF();
logger.info(str);
//inputStream.readInt();
int lineNumber = 1;
logger.info("开始");
while(str!=null) {
logger.info(lineNumber+":"+str);
lineNumber +=1;
try {
str = inputStream.readUTF();
} catch (Exception e) {
logger.info("文件读取结束");
str = null;
}
}
}else {
logger.info(path+"它不是文件");
}
}else {
logger.info("不存在"+path);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
//往hdfs上的一个文件后面追加三行数据内容
public static void appendContentToFile(String path) {
Path P = new Path(path);
try {
FSDataOutputStream outputStream = hdfs.append(P);
outputStream.writeUTF("大数据");
outputStream.writeUTF("java");
outputStream.writeUTF("sh");
outputStream.hflush();
logger.info("追加成功");
} catch (Exception e) {
logger.info("追加失败");
e.printStackTrace();
}
}
//hdfs的连接关闭
public static void close() {
if (hdfs !=null) {
try {
hdfs.close();
logger.info("关闭和hdfs的连接");
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static Configuration con = new Configuration();
public static FileSystem system ;
static {
try {
system= FileSystem.get(con);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
//设置hdfs上文件的权限
public static void setPermission(String path) throws Exception {
Path p =new Path(path);
FsPermission permission= new FsPermission(FsAction.ALL,FsAction.ALL,FsAction.ALL);
if (hdfs.exists(p)) {
hdfs.setPermission(p, permission);
logger.info("权限设置成功");
}else {
logger.info("给定路径不存在"+path);
}
}
public static void getPathStatus(String path){
Path p = new Path(path);
FileStatus fileStatus ;
try {
fileStatus= hdfs.getFileLinkStatus(p);
logger.info(fileStatus.toString());
logger.info("getPermission:"+fileStatus.getPermission());
} catch (Exception e) {
// TODO: handle exception
}
}
public static void listPathStatus(String path) {
Path p = new Path(path);
FileStatus[] fileStatus;
try {
if (hdfs.isDirectory(p)) {
fileStatus = hdfs.listStatus(p);
for (FileStatus fs : fileStatus) {
logger.info(fs.toString());
}
}else {
logger.info("给定的路径不是一个目录"+path);
}
} catch (Exception e) {
// TODO: handle exception
}
}
public static void main(String[] args) throws Exception {
// createDir("/bd23");
deleteDir("/secondarysortoutput");
// downloadFromHdfs("/fromjava.txt", "C:\\Users\\zc\\Desktop\\yyyy.txt");
// uploadFromHdfs("C:\\Users\\zc\\Desktop\\zzz.txt", "/ttt.txt");
// readFileOnHdfs("/zxcvb.txt");
// appendContentToFile("/fromjava.txt");
// setPermission("/fromjava.txt");
// getPathStatus("/fromjava.txt");
// listPathStatus("/fromjava.txt");
close();
}
}