915每日博客

今天对学过的hadoop知识进行使用,主要是在java连接hadoop,在IDEA创建maven项目,导入相关依赖:

 然后编写一些方法实现连接Hadoop:

private  static FileSystem fs;
private static void init() throws URISyntaxException, IOException, InterruptedException{
Configuration configuration = new Configuration();
configuration.set("dfs.replication","1");
// FileSystem fs = FileSystem.get(new
URI uri=new URI("hdfs://hadoop102:8020");
String user="wang";
fs = FileSystem.get(uri, configuration,user);
}
private static void close() throws IOException {
// 3 关闭资源
fs.close();
}
比如说新建一个文件夹,就可以使用以下的方法:
public void mkdir() throws IOException, URISyntaxException, InterruptedException {
init();
fs.mkdirs(new Path("/deleted/"));
close();
}
这时候在网页打开hadoop即可看到在目录下新建了一个对应名称的文件夹,就代表可以从客户端连接到hadoop

原文地址:https://www.cnblogs.com/ruangongwangxiansheng/p/14161585.html