博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hadoop 读取文件API报错
阅读量:6914 次
发布时间:2019-06-27

本文共 1754 字,大约阅读时间需要 5 分钟。

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集群上去运行,一切正常:

 

 

 

 

转载地址:http://khncl.baihongyu.com/

你可能感兴趣的文章
028 Partitioner:数据分区器
查看>>
移位实现正负数原码输出
查看>>
阿里云端口失效导致tomcat无法对外提供服务
查看>>
如何在Android Studio中指定NDK位置?
查看>>
更改Android应用程序的图标
查看>>
连载:面向对象葵花宝典:思想、技巧与实践(35) - NOP原则
查看>>
Redis(五)-- Java API
查看>>
Android中使用OnClickListener接口实现button点击的低级失误
查看>>
python核心编程——python对象
查看>>
指数函数及其性质教学设计
查看>>
关于内存中栈和堆的区别(非数据结构中的堆和栈,区别)【转】
查看>>
嵌套查询
查看>>
python-Levenshtein几个计算字串相似度的函数解析
查看>>
shipyard, swarm看到你,我才睡觉:)
查看>>
hdu 4409 Family Name List(LCA&有坑点)
查看>>
Linux内核之于红黑树and AVL树
查看>>
JAVA线程池ScheduledExecutorService周期性地执行任务 与单个Thread周期性执行任务的异常处理...
查看>>
Python 面向对象
查看>>
JAXB xml与javaBean的转换
查看>>
ResultSet 的Type属性 TYPE_FORWARD_ONLY, TYPE_SCROLL_I
查看>>