hdfs客户端操作-文件上传
接下来,我们向/maven下上传一个文件。 要用到的api是put (或者copyFormLocalFile)。核心代码如下。
public void testCopyFromLocalFile() throws IOException, InterruptedException, URISyntaxException {
// 1 获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:8020"), configuration, "root");
// 2 上传文件
fs.copyFromLocalFile(new Path("d:/sunwukong.txt"), new Path("/maven"));
// 3 关闭资源
fs.close();
}
上传结束之后,回到hdfs的UI界面去检查是否成功。