最近在学习hadoop,在linux上面启动hadoop后准备用java操作hadoop,看了中文的资料都是坑~ 查了英文资料一番折腾终于连上了
String HDFS_PATH = "hdfs://192.168.203.128:9000"; //要连接的hadoop Configuration configuration = new Configuration(); configuration.set("fs.defaultFS", HDFS_PATH); //连接文件系统,FileSystem用来查看文件信息和创建文件 FileSystem fileSystem = FileSystem.get(configuration); //操作文件io,用来读写 configuration.set("fs.hdfs.impl", DistributedFileSystem.class.getName()); //设置处理分布式文件系统 URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory(configuration)); //FsUrlStreamHandlerFactory() 不加参数连接会无法识别的hdfs协议,原因是hadoop在获取处理hdfs协议的控制器时获取了configuration的fs.hdfs.impl值 //获取Hadoop文件 InputStream inputStream = new URL(HDFS_PATH + "/hello.txt").openStream();