Hadoop learning-- getting file system status through API-- day04
Import java.io.ByteArrayOutputStream
Import java.io.InputStream
Import java.net.URL
Import org.apache.hadoop.conf.Configuration
Import org.apache.hadoop.fs.BlockLocation
Import org.apache.hadoop.fs.FSDataInputStream
Import org.apache.hadoop.fs.FileStatus
Import org.apache.hadoop.fs.FileSystem
Import org.apache.hadoop.fs.FsUrlStreamHandlerFactory
Import org.apache.hadoop.fs.Path
Import org.apache.hadoop.io.IOUtils
Import org.junit.Test
/ * *
* Test the API of the hadoop file system
* @ author Administrator
*
, /
Public class readfilestatus {
/ * *
* read HDFS file data through filesystem object API
*
* @ author Administrator
*
* 1. Create a configuration object. Note that if some parameters are involved, you need to write and specify it in the src directory.
* 2. Using the get method of filesystem to get the filesystem object, there are three get methods (key)
* 3. Use the open method of filesystem to open a data input stream, which requires the parameter transfer of a path object (key).
* 4. Using ioutils tool class to realize the output of files
*
*
*
, /
@ Test
Public void read () throws Exception {
/ / create configuration objects, which have a default loading order, first from core-default.xml to the files in the src directory, here
/ / We have given
Configuration conf = new Configuration ()
/ / you can add the specified profile manually
/ / conf.addResource ("my-core-site.xml")
/ / the distributed file system fs is created through the configuration object of conf. By default, it is the local file system if no file is specified.
FileSystem fs = FileSystem.get (conf)
/ / define a string of URL
String file = "hdfs://hadoop01/user/hadoop/data2/demo.txt"
/ / construct a path object from a string of URL, which is actually the address of the file
Path path = new Path (file)
/ / get the status of the files under the specified path
FileStatus st = fs.getFileStatus (path)
System.out.println ("st.getBlockSize () =" + st.getBlockSize ())
System.out.println ("st.getAccessTime () =" + st.getAccessTime ())
System.out.println ("st.getGroup () =" + st.getGroup ())
System.out.println ("st.getLen () =" + st.getLen ())
System.out.println ("st.getModificationTime () =" + st.getModificationTime ())
System.out.println ("st.getOwner () =" + st.getOwner ())
System.out.println ("st.getReplication () =" + st.getReplication ())
System.out.println ("st.getClass () =" + st.getClass ())
System.out.println ("st.getPath () =" + st.getPath ())
System.out.println ("st.getPermission () =" + st.getPermission ())
System.out.println ("-")
/ / System.out.println ("st.getSymlink () =" + st.getSymlink ())
/ / get the collection of block location information for the specified file status
/ / start the entire file from 0 bytes to the full-length bytes of the entire file
BlockLocation [] locations = fs.getFileBlockLocations (st, 0, st.getLen ())
For (BlockLocation block: locations) {
System.out.println (block.getLength ())
System.out.println (block.getOffset ())
System.out.println (block.getCachedHosts ())
System.out.println (block.getHosts ())
System.out.println (block.getNames ())
System.out.println (block.getTopologyPaths ())
}
}
}