• Hadoop 读取文件API报错


    Exception in thread "main" org.apache.hadoop.hdfs.BlockMissingException: Could not obtain block: BP-1124468226-10.0.2.15-1429879726015:blk_1073742186_1370 file=/user/testdir/yarn-site.xml
        at org.apache.hadoop.hdfs.DFSInputStream.chooseDataNode(DFSInputStream.java:889)
        at org.apache.hadoop.hdfs.DFSInputStream.fetchBlockByteRange(DFSInputStream.java:998)
        at org.apache.hadoop.hdfs.DFSInputStream.read(DFSInputStream.java:1333)
        at org.apache.hadoop.fs.FSInputStream.readFully(FSInputStream.java:78)
        at org.apache.hadoop.fs.FSDataInputStream.readFully(FSDataInputStream.java:116)
        at com.hdfs.file.ReadFile.main(ReadFile.java:24)、

    代码如下,在windows机器上运行,报上面的错误:

    package com.hdfs.file;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.FSDataInputStream;
    import org.apache.hadoop.fs.FileStatus;
    import org.apache.hadoop.fs.FileSystem;
    import org.apache.hadoop.fs.Path;
    
    public class ReadFile {
    
        /**
         * @param args
         * @throws Exception 
         */
        public static void main(String[] args) throws Exception {
            // TODO Auto-generated method stub
            Configuration conf=new Configuration();
            FileSystem fs=FileSystem.get(conf);
            Path path=new Path("hdfs://sandbox.hortonworks.com:8020/user/testdir/yarn-site.xml");
            if(fs.exists(path)){
                FSDataInputStream fsIn=fs.open(path);
                FileStatus status=fs.getFileStatus(path);
                byte[] buffer=new byte[Integer.parseInt(String.valueOf(status.getLen()))];
                fsIn.readFully(0,buffer);
                fsIn.close();
                fs.close();
                System.out.println("读取完成!");
                System.out.println(new String(buffer));
            }else{
                throw new Exception("the file is not found!");
            }
        }
    
    }

     

    将代码打成Jar包,放到hadoop集群上去运行,一切正常:

    image

    image

    image

    image

  • 相关阅读:
    为什么构造函数内部不能调用虚函数
    偏差与方差
    rJava配置
    《商道》读书笔记
    在myeclipse中使用Java语言进行spark Standalone模式应用程序开发
    机器学习算法之旅【转】
    最流行的4个机器学习数据集【转】
    机器学习资源大全【转】
    机器学习算法基础概念学习总结【转】
    机器学习问题的十个实例【转】
  • 原文地址:https://www.cnblogs.com/justinzhang/p/4844341.html
Copyright © 2020-2023  润新知