您的位置:首页 > 大数据 > Hadoop

连接hdfs异常protocol message end-group tag did not match expected tag

2020-07-14 04:35 1351 查看

在本地调试连接hdfs进行写文件操作,连接异常。
java.io.IOException: Failed on local exception: com.google.protobuf.InvalidProtocolBufferException: Protocol message end-group tag did not match expected tag.; Host Details : local host is: “Gore-PC/10.108.66.211”; destination host is: “10.108.66.81”:9000;

Caused by: com.google.protobuf.InvalidProtocolBufferException: Protocol message end-group tag did not match expected tag.

public static void main(String[] args) throws IOException {
System.setProperty("hadoop.home.dir", "d:/hadoop-common-2.2.0-bin/");
// 将本地文件上传到hdfs。
String target = "hdfs://10.94.1.104:9000/home/history/sample.csv";
FileInputStream fis = new FileInputStream(new File("D:\\home\\table.csv"));// 读取本地文件
Configuration config = new Configuration();
FileSystem fs = FileSystem.get(URI.create(target), config);
OutputStream os = fs.create(new Path(target));
// copy
IOUtils.copyBytes(fis, os, 4096, true);
System.out.println("拷贝完成...");
}

问题解决:
1、需要连接HDFS namenode主机,如果连接其他datanode的话,会报Connection refused
2、需使用8020端口,如果使用9000端口,会报Protocol message end-group tag did not match expected tag
3、在定义一个FileSystem变量的时候分布式和单机版的方法是不一样的,单机版使用的是FileSystem类的静态函数
FileSystem hdfs = FileSystem.get(conf)
分布式下需要使用Path来获得
Path dstDir = new Path(hdfsPath);
FileSystem hdfs = dstDir.getFileSystem(conf);
否则会报
Wrong FS: hdfs://hdfs://10.108.66.81:8020/…, expected: file:///
4、需要定义执行HDFS的用户,否则会出现Permission denied
System.setProperty(“HADOOP_USER_NAME”, “hdfs”);

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐