FileInputStream为通用文件系统 [英] FileInputStream for a generic file System

查看:125
本文介绍了FileInputStream为通用文件系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含像Vector这样的java序列化对象的文件。我通过Hadoop分布式文件系统(HDFS)存储了这个文件。现在我打算在map任务之一中读取这个文件(使用方法readObject)。我猜想

  FileInputStream in = new FileInputStream(hdfs / path / to / file); 

不会工作,因为文件存储在HDFS上。所以我想使用org.apache.hadoop.fs.FileSystem类。但不幸的是,它没有任何方法返回FileInputStream。它只是一个返回FSDataInputStream的方法,但是我想要一个可以从文件读取像vector一样的序列化java对象的输入流,而不仅仅是FSDataInputStream会做的原始数据类型。

请帮忙!

解决方案

FileInputStream 不能直接读取序列化对象。您需要将其封装到 ObjectInputStream 中。您可以使用 FSDataInputStream ,只需将其包装到 ObjectInputStream
$ b

换句话说,如果你有 fileSystem 类型 org.apache.hadoop.fs.FileSystem ,只需使用:

  ObjectInputStream in = new ObjectInputStream(fileSystem.open(path)); 


I have a file that contains java serialized objects like "Vector". I have stored this file over Hadoop Distributed File System(HDFS). Now I intend to read this file (using method readObject) in one of the map task. I suppose

FileInputStream in = new FileInputStream("hdfs/path/to/file");

wont' work as the file is stored over HDFS. So I thought of using org.apache.hadoop.fs.FileSystem class. But Unfortunately it does not have any method that returns FileInputStream. All it has is a method that returns FSDataInputStream but I want a inputstream that can read serialized java objects like vector from a file rather than just primitive data types that FSDataInputStream would do.

Please help!

解决方案

FileInputStream doesn't give you facitily to read serialized objects directly. You need to wrap it into ObjectInputStream. You can do the same with FSDataInputStream, just wrap it into ObjectInputStream and then you can read your objects from it.

In other words, if you have fileSystem of type org.apache.hadoop.fs.FileSystem, just use:

ObjectInputStream in = new ObjectInputStream(fileSystem.open(path));

这篇关于FileInputStream为通用文件系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆