jva操作hsfs:

public static void download(FileSystem fs,String hadooppath,String name)
{
try {
File file = new File("D:\\+name");

String newpath=null;
if (!file.exists()) {
newpath="hadoop_"+name;
System.out.println("本地文件存在,另存文件名:"+newpath);
InputStream input = null;
OutputStream output = null;
input = fs.open(new Path(hadooppath));
output = new FileOutputStream("D:\\"+newpath);
IOUtils.copyBytes(input, output, 4096, true);
}else{
fs.copyToLocalFile(false, new Path("/user/hadoop/text/sean.txt"), new Path("E:/"), true);
}
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}

public static void printtext(FileSystem fs, String hadooppath) {
Path remotePath = new Path(hadooppath);
FSDataInputStream in = null;
try {
in = fs.open(remotePath);
BufferedReader d = new BufferedReader(new InputStreamReader(in));
String line = null;
while ((line = d.readLine()) != null) {
String[] strarray = line.split(" ");
for (int i = 0; i < strarray.length; i++) {
System.out.print(strarray[i]);
System.out.print(" ");

}

System.out.println(" ");
}
} catch (IOException e) {
e.printStackTrace();
}
}
posted on 2021-10-08 22:31  sean1246  阅读(46)  评论(0编辑  收藏  举报