java操作HDFS:

 

public void set(FileSystem fs)
{
// 1.新建目录
Path path = new Path("/user/hadoop/hello");
try {
if (!fs.exists(path)){
fs.mkdirs(path);
}else{
System.out.println("路径已存在");
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void logup(FileSystem fs,String path,String name)
{
try {
if(fs.exists(new Path("/user/hadoop/text/"+name))){
System.out.println(name + " 文件存在");
int Temp=0;
System.out.println("请选择追加或覆盖:1、追加 2、覆盖");
Path path1 = new Path("/user/hadoop/text/"+name);
FSDataInputStream getIt = fs.open(path1);
BufferedReader dA = new BufferedReader(new FileReader(path));
BufferedReader dB = new BufferedReader(new InputStreamReader(getIt));
String contentA = dA.readLine(); //读取文件一行
String contentB = dB.readLine();
String mem = new String();
mem = contentB+contentA;
if(Temp==2) mem=contentA;
FSDataOutputStream os = fs.create(path1);
os.writeUTF(mem);
os.close();
dA.close(); //关闭文件A
dB.close(); //关闭文件B

}else{
System.out.println(name + " 文件不存在");
fs.copyFromLocalFile(new Path("D:\\sean.txt"),
new Path("/user/hadoop/text/sean.txt"));
}

fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}

 

posted on 2021-10-06 15:42  sean1246  阅读(30)  评论(0编辑  收藏  举报