繁体   English   中英

在Hadoop中创建输入流对象以读取文件

[英]Creating an input stream object in Hadoop for reading a file

我的Hadoop文件系统中有一个文件,我需要创建一个InputStream对象以将其作为输入参数传递给另一个API作为ApiMethod(inputstreamObject)

我正在使用《权威指南》中提到的以下方法来创建输入流对象,但不起作用。

class test {

        static {    
        URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());  } 

        InputStream in = null; 
        try {  
        in = new URL("hdfs://host/path").openStream();  
        IOUtils.copyBytes(in, System.out, 4096, false);
        Object = new ApiMethod(in);
        } finally {  
        IOUtils.closeStream(in); } 

}

请帮忙。

如果您只想从hadoop读取文件,请尝试这种方法。 从hadoop读取文件并将其写入本地路径或在屏幕上打印。

FileSystem fileSystem = FileSystem.get(conf);

Path path = new Path("/path/to/file");

FSDataInputStream in = fileSystem.open(path);
OutputStream out = new BufferedOutputStream(new FileOutputStream(
    new File(fileOnLocal)));

byte[] b = new byte[1024];
int numBytes = 0;
while ((numBytes = in.read(b)) > 0) {
    out.write(b, 0, numBytes);
}

in.close();
out.close();
fileSystem.close();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM