困惑了多少个中午,终于解决了:通过java连接hdfs的问题
♠ ♥ ♦ ♣ 该文章仅为平时的笔记,非商用,如有侵权请联系博主删除 ♣ ♦ ♥ ♠
之前用java连接hdfs时(提交到集群上跑),总会报错:Call From xxxxx to xxxxx failed on connection exception: java.net.ConnectException: Connection refused; For more details see: http://wiki.apache.org/hadoop/ConnectionRefused
在昨晚在看大佬修改项目时终于发现,怎么确认
conf.set(“fs.defaultFS”,“hdfs://localhost:9000”); 中fs.defaultFS的值,该值其实就是nameode的位置!
1、如图所示,在50070页面中,查看Overview下Namespace的值
2、如图所示,在启动namenode时,观察启动位置,即fs.defaultFS的值。
检测代码如下:
package com.blueasdfg;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
/**
* Hello world!
*/
public class App {
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
conf.set("fs.defaultFS","hdfs://localhost:9000");
FileSystem fs = null;
fs = FileSystem.get(conf);
fs.mkdirs(new Path("/test2"));
}
}
仅作为日常笔记使用,有点简单,大家有什么想法也可以交流哟~