我的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); }
}
请帮忙。
答案 0 :(得分:4)
如果您只想阅读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();