List of usage examples for org.apache.hadoop.fs FileSystem open
public FSDataInputStream open(PathHandle fd) throws IOException
From source file:com.github.sakserv.minicluster.HdfsLocalClusterIntegrationTest.java
License:Apache License
@Test public void testDfsClusterStart() throws IOException { // Write a file to HDFS containing the test string FileSystem hdfsFsHandle = dfsCluster.getHdfsFileSystemHandle(); FSDataOutputStream writer = hdfsFsHandle .create(new Path(propertyParser.getProperty(ConfigVars.HDFS_TEST_FILE_KEY))); writer.writeUTF(propertyParser.getProperty(ConfigVars.HDFS_TEST_STRING_KEY)); writer.close();/*from ww w . j av a 2 s .co m*/ // Read the file and compare to test string FSDataInputStream reader = hdfsFsHandle .open(new Path(propertyParser.getProperty(ConfigVars.HDFS_TEST_FILE_KEY))); assertEquals(reader.readUTF(), propertyParser.getProperty(ConfigVars.HDFS_TEST_STRING_KEY)); reader.close(); hdfsFsHandle.close(); }
From source file:com.github.sakserv.minicluster.impl.HdfsLocalClusterIntegrationTest.java
License:Apache License
@Test public void testDfsClusterStart() throws Exception { // Write a file to HDFS containing the test string FileSystem hdfsFsHandle = dfsCluster.getHdfsFileSystemHandle(); FSDataOutputStream writer = hdfsFsHandle .create(new Path(propertyParser.getProperty(ConfigVars.HDFS_TEST_FILE_KEY))); writer.writeUTF(propertyParser.getProperty(ConfigVars.HDFS_TEST_STRING_KEY)); writer.close();/*from w w w .j a v a2 s . c om*/ // Read the file and compare to test string FSDataInputStream reader = hdfsFsHandle .open(new Path(propertyParser.getProperty(ConfigVars.HDFS_TEST_FILE_KEY))); assertEquals(reader.readUTF(), propertyParser.getProperty(ConfigVars.HDFS_TEST_STRING_KEY)); reader.close(); hdfsFsHandle.close(); URL url = new URL(String.format("http://localhost:%s/webhdfs/v1?op=GETHOMEDIRECTORY&user.name=guest", propertyParser.getProperty(ConfigVars.HDFS_NAMENODE_HTTP_PORT_KEY))); URLConnection connection = url.openConnection(); connection.setRequestProperty("Accept-Charset", "UTF-8"); BufferedReader response = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line = response.readLine(); response.close(); assertEquals("{\"Path\":\"/user/guest\"}", line); }
From source file:com.github.sakserv.minicluster.impl.KdcLocalClusterHdfsIntegrationTest.java
License:Apache License
@Test public void testHdfs() throws Exception { FileSystem hdfsFsHandle = hdfsLocalCluster.getHdfsFileSystemHandle(); UserGroupInformation.loginUserFromKeytab(kdcLocalCluster.getKrbPrincipalWithRealm("hdfs"), kdcLocalCluster.getKeytabForPrincipal("hdfs")); assertTrue(UserGroupInformation.isSecurityEnabled()); assertTrue(UserGroupInformation.isLoginKeytabBased()); // Write a file to HDFS containing the test string FSDataOutputStream writer = hdfsFsHandle .create(new Path(propertyParser.getProperty(ConfigVars.HDFS_TEST_FILE_KEY))); writer.writeUTF(propertyParser.getProperty(ConfigVars.HDFS_TEST_STRING_KEY)); writer.close();/*w w w. ja v a 2 s . com*/ // Read the file and compare to test string FSDataInputStream reader = hdfsFsHandle .open(new Path(propertyParser.getProperty(ConfigVars.HDFS_TEST_FILE_KEY))); assertEquals(reader.readUTF(), propertyParser.getProperty(ConfigVars.HDFS_TEST_STRING_KEY)); reader.close(); // Log out UserGroupInformation.getLoginUser().logoutUserFromKeytab(); UserGroupInformation.reset(); try { Configuration conf = new Configuration(); UserGroupInformation.setConfiguration(conf); FileSystem.get(hdfsFsHandle.getUri(), conf) .open(new Path(propertyParser.getProperty(ConfigVars.HDFS_TEST_FILE_KEY))); fail(); } catch (AccessControlException e) { LOG.info("Not authenticated!"); } }
From source file:com.github.sakserv.minicluster.impl.MRLocalClusterIntegrationTest.java
License:Apache License
private String readFileFromHdfs(String filename) throws Exception { FileSystem hdfsFsHandle = dfsCluster.getHdfsFileSystemHandle(); FSDataInputStream reader = hdfsFsHandle.open(new Path(filename)); String output = reader.readUTF(); reader.close();/* w w w .j ava2 s .c o m*/ hdfsFsHandle.close(); return output; }
From source file:com.github.sakserv.storm.KafkaHdfsTopologyTest.java
License:Apache License
/** * Validate that the files in HDFS contain the expected data from Kafka * @throws Exception/*from ww w . ja va 2s .com*/ */ private void validateHdfsResults() throws Exception { LOG.info("HDFS: VALIDATING"); // Get the filesystem handle and a list of files written by the test FileSystem hdfsFsHandle = hdfsLocalCluster.getHdfsFileSystemHandle(); RemoteIterator<LocatedFileStatus> listFiles = hdfsFsHandle.listFiles( new Path(propertyParser.getProperty(ConfigVars.STORM_HDFS_BOLT_OUTPUT_LOCATION_KEY)), true); // Loop through the files and count up the lines int count = 0; while (listFiles.hasNext()) { LocatedFileStatus file = listFiles.next(); LOG.info("HDFS READ: Found File: " + file); BufferedReader br = new BufferedReader(new InputStreamReader(hdfsFsHandle.open(file.getPath()))); String line = br.readLine(); while (line != null) { LOG.info("HDFS READ: Found Line: " + line); line = br.readLine(); count++; } } hdfsFsHandle.close(); // Validate the number of lines matches the number of kafka messages assertEquals(Integer.parseInt(propertyParser.getProperty(ConfigVars.KAFKA_TEST_MESSAGE_COUNT_KEY)), count); }
From source file:com.github.sakserv.storm.KafkaHiveHdfsTopologyTest.java
License:Apache License
public void validateHdfsResults() throws IOException { System.out.println("HDFS: VALIDATING"); FileSystem hdfsFsHandle = hdfsCluster.getHdfsFileSystemHandle(); RemoteIterator<LocatedFileStatus> listFiles = hdfsFsHandle.listFiles(new Path("/tmp/kafka_data"), true); while (listFiles.hasNext()) { LocatedFileStatus file = listFiles.next(); System.out.println("HDFS READ: Found File: " + file); BufferedReader br = new BufferedReader(new InputStreamReader(hdfsFsHandle.open(file.getPath()))); String line = br.readLine(); while (line != null) { System.out.println("HDFS READ: Found Line: " + line); line = br.readLine();//from w w w . j a v a 2 s . c om } } hdfsFsHandle.close(); }
From source file:com.google.mr4c.content.HDFSContentFactory.java
License:Open Source License
public InputStream readContentAsStream(URI uri) throws IOException { Path path = toPath(uri);// www . ja va 2 s . c o m FileSystem fs = FileSystem.get(uri, m_config); return fs.open(path); }
From source file:com.gruter.hadoop.customShell.CustomShell.java
License:Apache License
/** * Fetch all files that match the file pattern <i>srcf</i> and display * their content on stdout. /*from w ww .j a va 2s . c om*/ * @param srcf: a file pattern specifying source files * @exception: IOException * @see org.apache.hadoop.fs.FileSystem.globStatus */ void cat(final String src, boolean verifyChecksum) throws IOException { //cat behavior in Linux // [~/1207]$ ls ?.txt // x.txt z.txt // [~/1207]$ cat x.txt y.txt z.txt // xxx // cat: y.txt: No such file or directory // zzz Path srcPattern = new Path(src); new DelayedExceptionThrowing() { @Override void process(Path p, FileSystem srcFs) throws IOException { FSDataInputStream in = srcFs.open(p); /** * snappy */ if (isSnappy(p.getName())) { printToStdout(getSnappyCodec().createInputStream(in)); } else { printToStdout(in); } /** * end */ } }.globAndProcess(srcPattern, getSrcFileSystem(srcPattern, verifyChecksum)); }
From source file:com.gruter.hadoop.customShell.CustomShell.java
License:Apache License
private InputStream forMagic(Path p, FileSystem srcFs) throws IOException { FSDataInputStream i = srcFs.open(p); switch (i.readShort()) { case 0x1f8b: // RFC 1952 i.seek(0);//from ww w. j ava2s . c om return new GZIPInputStream(i); case 0x5345: // 'S' 'E' if (i.readByte() == 'Q') { i.close(); return new TextRecordInputStream(srcFs.getFileStatus(p)); } break; } i.seek(0); /** * snappy */ if (isSnappy(p.getName())) { return getSnappyCodec().createInputStream(i); } /** * end */ return i; }
From source file:com.hadoop.compression.fourmc.FourMcInputStream.java
License:BSD License
/** * Reads blocks index at tail of file.// www.j ava2 s .c o m * * @param fs filesystem * @param file path to 4mc file * @return block index * @throws IOException */ public static FourMcBlockIndex readIndex(FileSystem fs, Path file) throws IOException { long fileSize = fs.getFileStatus(file).getLen(); if (fileSize < (12 + 20)) { // file too small return new FourMcBlockIndex(); } FSDataInputStream indexIn = fs.open(file); /* 4mc Footer: Footer size: 4 bytes Footer version: 4 byte (1) Block index offset: 4 bytes delta offset for each stored block, the delta between offset between previous file position and next block Footer size: 4 bytes (repeated to be able to read from end of file) MAGIC SIGNATURE: 4 bytes: "4MC\0" Footer checksum: 4 bytes (always in XXHASH32) */ /** * jump to file tail and read-ahead last 4KB of file which should be enough in most cases * Improvement: we could estimate a best case compression factor of 10% and calc forecast * based on filesize and blocksize, to see if better to read-head more. */ int readTailSize = 4 * 1024; if (readTailSize > (fileSize - 12)) readTailSize = (int) (fileSize - 12); indexIn.seek(fileSize - readTailSize); byte[] buf = new byte[readTailSize]; readFully(indexIn, buf, 0, buf.length); int footerSize = getInt(buf, buf.length - 12); int magic = getInt(buf, buf.length - 8); int checksum = getInt(buf, buf.length - 4); if (magic != FourMcCodec.FOURMC_MAGIC) { throw new IOException("Invalid 4mc footer magic"); } if (footerSize >= (fileSize - 12)) { throw new IOException("Invalid 4mc footer checksum"); } // very rare case: read head was not enough! seek back and read it all if (footerSize > readTailSize) { readTailSize = footerSize; indexIn.seek(fileSize - readTailSize); buf = new byte[readTailSize]; readFully(indexIn, buf, 0, buf.length); } indexIn.close(); int startFooterOffset = readTailSize - footerSize; if (getInt(buf, startFooterOffset) != footerSize) { // size again throw new IOException("Invalid 4mc footer size"); } if (getInt(buf, startFooterOffset + 4) != FourMcCodec.FOURMC_VERSION) { // version throw new IOException("Invalid 4mc footer version (" + getInt(buf, startFooterOffset + 4) + ")"); } if (checksum != Lz4Decompressor.xxhash32(buf, startFooterOffset, footerSize - 4, 0)) { throw new IOException("Invalid 4mc footer checksum"); } int totalBlocks = (footerSize - 20) / 4; FourMcBlockIndex index = new FourMcBlockIndex(totalBlocks); long curOffset = 0; for (int i = 0; i < totalBlocks; ++i) { curOffset += getInt(buf, startFooterOffset + 8 + (i * 4)); index.set(i, curOffset); } return index; }