简体   繁体   中英

How to read a serialized object from a file in HDFS in Hadoop

I have serialized an object of type LibSVM into a file named j48.model. This file has been transferred into the HDFS file system.

Now, in a hadoop mapreduce code, how can I deserialize this object and read it back into an instance of class LibSVM? I have included the .jar file relevant to LIBSVM already into the ma reduce project as an external jar file.

What JAVA methods help me read the contents of the file j48.model into a LibSVM object?

When you open a HDFS URL, it returns you an InputStream.

FileSystem fs = FileSystem.get(new Configuration());
InputStream in = fs.open(new Path("your uri"));

You can wrap this input stream in an ObjectInputStream:

ObjectInputStream objReader = new ObjectInputStream(in);

and read your object from it:

LibSVM lib = (LibSVM)objReader.readObject();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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