您的位置:首页 > 运维架构

Hadoop之旅(8)— HDFS API 实战操作

2017-08-30 16:31 483 查看

1、得到系统文件

/**
* Get FileSystem (得到系统文件)
*
* @return
* @throws Exception
*/
public static FileSystem getFileSystem() throws Exception {
Configuration configuration = new Configuration();
configuration.set("fd.defaultFS","hdfs://chenzy-1:9000");
// get filesystem
FileSystem fileSystem = FileSystem.get(configuration);

System.out.println(fileSystem);
return fileSystem;
}


2、创建文件

//指定相对路径relative path   路径:/user/Administrator/文件名
private static void relativePath() throws Exception{
// get filesystem
FileSystem fileSystem = getFileSystem();
boolean b = fileSystem.createNewFile(new Path("relative.txt"));
System.out.println(b);
fileSystem.close();
}




3、追加文件内容

//追加内容
private static void appendContent() throws Exception{
// get filesystem
FileSystem fileSystem = getFileSystem();

Path path = new Path("/chenzy/mapreduce/wordcount/input/test.txt");
FSDataOutputStream append = fileSystem.append(path);
append.write("this is append content ! 追加内容 !".getBytes());
append.close();
fileSystem.close();
}

//添加内容
private static void bufferedContent() throws Exception{
// get filesystem
FileSystem fileSystem = getFileSystem();

Path path = new Path("/chenzy/mapreduce/wordcount/input/test.txt");
FSDataOutputStream append = fileSystem.append(path);
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(append));
bufferedWriter.newLine();
bufferedWriter.write("this is append content ! 内容 !");
bufferedWriter.newLine();
bufferedWriter.write("这是换行的内容!");
bufferedWriter.newLine();

bufferedWriter.close();
fileSystem.close();
}


4、读取文件

//读取文件
private static void openRead() throws Exception{
// get filesystem
FileSystem fileSystem = getFileSystem();
InputStream is = fileSystem.open(new Path("/chenzy/mapreduce/wordcount/input/test.txt"));
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));

String line = null;
while ((line = bufferedReader.readLine()) != null){
System.out.println(line);
}

is.close();
bufferedReader.close();
fileSystem.close();
}




5、从本地拷贝到hdfs

//从本地拷贝到hdfs
private static void copyFromlocal() throws Exception{
// get filesystem
FileSystem fileSystem = getFileSystem();
fileSystem.copyFromLocalFile(new Path("E:\\from\\new.txt"),new Path("/chenzy/mapreduce/wordcount/newMkdirs/test.txt"));
fileSystem.close();
}

//从hdfs拷贝到本地
private static void copyTolocal() throws Exception{
// get filesystem
FileSystem fileSystem = getFileSystem();
fileSystem.copyToLocalFile(new Path("/chenzy/mapreduce/wordcount/newMkdirs/test.txt"),new Path("E:\\from\\new2.txt"));
fileSystem.close();
}


更多的例子:查看GitHub
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: