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

use*_*576 0 java hadoop bigdata

我的Hadoop文件系统中有一个文件,我需要创建一个InputStream对象,以便将其作为输入参数传递给as 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); } 

}
Run Code Online (Sandbox Code Playgroud)

请帮忙。

sal*_*nbw 5

如果您只想从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();
Run Code Online (Sandbox Code Playgroud)