All Downloads are FREE. Search and download functionalities are using the official Maven repository.

stream.urls.HdfsConnection Maven / Gradle / Ivy

There is a newer version: 0.0.3
Show newest version
/**
 * 
 */
package stream.urls;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import stream.io.SourceURL;

/**
 * @author chris
 *
 */
public class HdfsConnection extends Connection {

	InputStream stream;

	/**
	 * @param url
	 */
	public HdfsConnection(SourceURL url) {
		super(url);
	}

	/**
	 * @see stream.urls.Connection#getSupportedProtocols()
	 */
	@Override
	public String[] getSupportedProtocols() {
		return new String[] { "hdfs" };
	}

	/**
	 * @see stream.urls.Connection#connect()
	 */
	@Override
	public InputStream connect() throws IOException {

		if (stream == null) {
			Configuration config = new Configuration();
			FileSystem fs = FileSystem.get(URI.create(url.toString()), config);
			stream = fs.open(new Path(url.toString()));
		}

		return stream;
	}

	/**
	 * @see stream.urls.Connection#disconnect()
	 */
	@Override
	public void disconnect() throws IOException {
		if (stream != null) {
			stream.close();
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy