排坑!java操作hadoop中的hdfs文件系统

用maven管理项目加入以下依赖:

pom.xml:

<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.7.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client -->


<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.7.3</version>
</dependency>


代码:


package cn.hadoop.hdfs;


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;


import org.apache.commons.io.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;



public class HdfsUtil {

FileSystem fs = null;




public void init() throws Exception{

//读取classpath下的xxx-site.xml 配置文件,并解析其内容,封装到conf对象中
Configuration conf = new Configuration();

//也可以在代码中对conf中的配置信息进行手动设置,会覆盖掉配置文件中的读取的值
conf.set("fs.defaultFS", "hdfs://192.168.231.66:9000/");

//根据配置信息,去获取一个具体文件系统的客户端操作实例对象
fs = FileSystem.get(new URI("hdfs://192.168.231.66:9000/"),conf,"root");


}



/**
* 上传文件 (copyToLocal  是最简单的)

* @throws Exception
*/
@Test
public void upload() throws Exception {


Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://192.168.231.66:9000/");

FileSystem fs = FileSystem.get(conf);

Path dst = new Path("hdfs://192.168.231.66:9000/test/test_hdfs.txt");

FSDataOutputStream os = fs.create(dst);

FileInputStream is = new FileInputStream("c:/test_hdfs.txt");

IOUtils.copy(is, os);



}


/**
* 上传文件,封装好的写法
* @throws Exception
* @throws IOException
*/
@Test
public void upload2() throws Exception, IOException{

fs.copyFromLocalFile(new Path("c:/testHdfs.txt"), new Path("hdfs://192.168.231.66:9000/test/test_hdfs.txt"));

}


/**
* 下载文件
* @throws Exception 
* @throws IllegalArgumentException 
*/
@Test
public void download() throws Exception {

 fs.copyToLocalFile(new Path("hdfs://192.168.231.66:9000/test/testHdfs.txt"), new                            Path("c:/testHdfs.txt"));


}


/**
* 查看文件信息
* @throws IOException 
* @throws IllegalArgumentException 
* @throws FileNotFoundException 

*/
@Test
public void listFiles() throws FileNotFoundException, IllegalArgumentException, IOException {


// listFiles列出的是文件信息,而且提供递归遍历
RemoteIterator<LocatedFileStatus> files = fs.listFiles(new Path("/"), true);

while(files.hasNext()){

LocatedFileStatus file = files.next();
Path filePath = file.getPath();
String fileName = filePath.getName();
System.out.println(fileName);

}

System.out.println("---------------------------------");

//listStatus 可以列出文件和文件夹的信息,但是不提供自带的递归遍历
FileStatus[] listStatus = fs.listStatus(new Path("/"));
for(FileStatus status: listStatus){

String name = status.getPath().getName();
System.out.println(name + (status.isDirectory()?" is dir":" is file"));

}

}


/**
* 创建文件夹
* @throws Exception 
* @throws IllegalArgumentException 
*/
@Test
public void mkdir() throws IllegalArgumentException, Exception {


fs.mkdirs(new Path("/testHdfs"));


}


/**
* 删除文件或文件夹
* @throws IOException 
* @throws IllegalArgumentException 
*/
@Test
public void rm() throws IllegalArgumentException, IOException {


fs.delete(new Path("/test.txt"), true);

}



public static void main(String[] args) throws Exception {


Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://192.168.231.66:9000/");

FileSystem fs = FileSystem.get(conf);

FSDataInputStream is = fs.open(new Path("/hello_hdfs.txt"));

FileOutputStream os = new FileOutputStream("c:/hello_hdfs");

IOUtils.copy(is, os);
}
}


错误:

1.没有加配置文件,或者配置文件错误



2.namenode  通讯错误,这个事相当坑爹的错误。很多人都是链接虚拟机的HDFS很少会碰到这个错误,小编阿里云,腾讯云,百度云测试,腾讯云的机器出现了这个错误, 纠结了我快一个星期!真的想在吐槽一下小马哥(FUCK!)。



一般你的datanode死掉,或者namenode挂了,或者你datanode之间的通讯出现问题,都会报这个错误。小编能够对hdfs集群进行上传,删除,创建目录,也能追加买就是不能下载,一下载就出现这个错误。说不能获取块(could not obtain block),谷歌,必应,百度按照各路大神去修改hdfs的配置文件,依然解决不了这个问题。我就开始怀疑环境问题,最后实在没有办法,在我的阿里云服务器上试了一下,所有配置,以及code都是完全相同的,阿里云时候OK的,腾讯云失败。最后发现了这样的区别:

阿里云:



腾讯云:


我的腾讯服务器是有外网IP的。


两个系统版本号都是一样的,配置也相同,别的没有找到不同。判定就是网卡配置引起的(重装系统OK,但是我之前没有修改过网卡配置哦),如果我的判定有问题,我希望你告诉我,非常感谢!

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值