Example usage for org.apache.hadoop.fs HarFileSystem close

List of usage examples for org.apache.hadoop.fs HarFileSystem close

Introduction

In this page you can find the example usage for org.apache.hadoop.fs HarFileSystem close.

Prototype

@Override
    public void close() throws IOException 

Source Link

Usage

From source file:org.cripac.isee.vpe.util.hdfs.HadoopHelper.java

License:Open Source License

/**
 * Retrieve a track from the HDFS./*from   w  w w. j  a va  2 s.  c  o m*/
 *
 * @param storeDir The directory storing the tracklets.
 * @param id       The identifier of the track.
 * @return The track retrieved.
 */
public static Tracklet retrieveTracklet(@Nonnull String storeDir, @Nonnull Tracklet.Identifier id,
        @Nullable Logger logger) {
    if (logger == null) {
        logger = new ConsoleLogger(Level.INFO);
    }
    try {
        // Open the Hadoop Archive of the task the track is generated in.
        HarFileSystem harFileSystem = new HarFileSystem();
        harFileSystem.initialize(new URI(storeDir), new Configuration());

        // Read verbal informations of the track.
        Gson gson = new Gson();
        Tracklet tracklet = gson.fromJson(
                new InputStreamReader(
                        harFileSystem.open(new Path(storeDir + "/" + id.serialNumber + "/info.txt"))),
                Tracklet.class);

        // Read frames concurrently..
        ContiguousSet.create(Range.closedOpen(0, tracklet.locationSequence.length), DiscreteDomain.integers())
                .parallelStream().forEach(idx -> {
                    Tracklet.BoundingBox bbox = tracklet.locationSequence[idx];
                    FSDataInputStream imgInputStream = null;
                    try {
                        imgInputStream = harFileSystem
                                .open(new Path(storeDir + "/" + id.toString() + "/" + idx + ".jpg"));
                        byte[] rawBytes = IOUtils.toByteArray(imgInputStream);
                        imgInputStream.close();
                        opencv_core.Mat img = imdecode(new opencv_core.Mat(rawBytes), CV_8UC3);
                        bbox.patchData = new byte[img.rows() * img.cols() * img.channels()];
                        img.data().get(bbox.patchData);
                        img.release();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                });
        harFileSystem.close();
        return tracklet;
    } catch (Exception e) {
        try {
            logger.error("Error when retrieving tracklets" + " from \"" + storeDir + "/" + id.serialNumber
                    + "/info.txt\".", e);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return new FakePedestrianTracker().track(null)[0];
    }
}