1. Overview
2. File Operations
2.1 upload local files to hadoop fs
2.2 create a file in hadoop fs and write
2.3 Delete files on hadoop fs
2.4 read files
3. Directory operations
3.1 create a directory on hadoop fs
3.2 delete a directory
3.3 read all files in a directory
4. Download the reference material from the code
<1>. Overview
The file operation classes in hadoop are basically in the org. apache. hadoop. fs package. The operations supported by these Apis include opening files, reading and writing files, and deleting files.
In the hadoop class library, the final user-oriented interface class is FileSystem, which is an abstract class and can only be obtained through the get method of the class. The get method has several overloaded versions, which are commonly used:
Static FileSystem get (Configuration conf );
This class encapsulates almost all file operations, such as mkdir and delete. To sum up, the Library Framework of the Operation file can be obtained:
Operator ()
{
Get Configuration object
Obtain the FileSystem object.
File Operations
}
In addition, if you want to run the following program, you need to generate a jar package for the program and run it in the form of hadoop jar. This method is troublesome, another method is to install the hadoop plug-in of eclipse, which can take a lot of time to package.
<1>. File Operations
1.1 upload local files to the file system
/*
* Upload the local file to the hds
* Notice that the path is full like/tmp/test. c
*/
Public static void uploadLocalFile2HDFS (String s, String d)
Throws IOException
{
Configuration config = new Configuration ();
FileSystem hdfs = FileSystem. get (config );
Path src = new Path (s );
Path dst = new Path (d );
Hdfs. copyFromLocalFile (src, dst );
Hdfs. close ();
}
1.2 create a new file and write
/*
* Create a new file in the hdfs.
* Notice that the toCreateFilePath is the full path
* And write the content to the hdfs file.
*/
Public static void createNewHDFSFile (String toCreateFilePath, String content) throws IOException
{
Configuration config = new Configuration ();
FileSystem hdfs = FileSystem. get (config );
FSDataOutputStream OS = hdfs. create (new Path (toCreateFilePath ));
OS. write (content. getBytes ("UTF-8 "));
OS. close ();
Hdfs. close ();
}
1.3 Delete an object
/*
* Delete the hdfs file
* Notice that the dst is the full path name
*/
Public static boolean deleteHDFSFile (String dst) throws IOException
{
Configuration config = new Configuration ();
FileSystem hdfs = FileSystem. get (config );
Path path = new Path (dst );
Boolean isDeleted = hdfs. delete (path );
Hdfs. close ();
Return isDeleted;
}
1.4 read files
/** Read the hdfs file content
* Notice that the dst is the full path name
*/
Public static byte [] readHDFSFile (String dst) throws Exception
{
Configuration conf = new Configuration ();
FileSystem fs = FileSystem. get (conf );
// Check if the file exists
Path path = new Path (dst );
If (fs. exists (path ))
{
FSDataInputStream is = fs. open (path );
// Get the file info to create the buffer
FileStatus stat = fs. getFileStatus (path );
// Create the buffer
Byte [] buffer = new byte [Integer. parseInt (String. valueOf (stat. getLen ()];
Is. readFully (0, buffer );
Is. close ();
Fs. close ();
Return buffer;
}
Else
{
Throw new Exception ("the file is not found .");
}
}
<2>. Directory operations
2.1 create a directory
/** Make a new dir in the hdfs
*
* The dir may like '/tmp/testdir'
*/
Public static void mkdir (String dir) throws IOException
{
Configuration conf = new Configuration ();
FileSystem fs = FileSystem. get (conf );
Fs. mkdirs (new Path (dir ));
Fs. close ();
}
2.2 Delete A directory
/** Delete a dir in the hdfs
*
* Dir may like '/tmp/testdir'
*/
Public static void deleteDir (String dir) throws IOException
{
Configuration conf = new Configuration ();
FileSystem fs = FileSystem. get (conf );
Fs. delete (new Path (dir ));
Fs. close ();
}
2.3 read all files in a directory
Public static void listAll (String dir) throws IOException
{
Configuration conf = new Configuration ();
FileSystem fs = FileSystem. get (conf );
FileStatus [] stats = fs. listStatus (new Path (dir ));
For (int I = 0; I <stats. length; ++ I)
{
If (stats [I]. isFile ())
{
// Regular file
System. out. println (stats [I]. getPath (). toString ());
}
Else if (stats [I]. isDirectory ())
{
// Dir
System. out. println (stats [I]. getPath (). toString ());
}
Else if (stats [I]. isSymlink ())
{
// Is s symlink in linux
System. out. println (stats [I]. getPath (). toString ());
}
}
Fs. close ();
}
<4>. Download reference materials and code
/Files/xuqiang/HadoopFSOperations.rar
Note that if you are operating files on hdfs, you need to add the jar packages of hadoop-core and common-log to classpath and run them locally. If it is a mapreduce program, you need to compress the program into a jar package and upload it to hdfs to run the program.