Hadoop Basics Tutorial-4th Chapter HDFs Java API (4.5 Java API Introduction) __java

Source: Internet
Author: User
Tags parent directory file permissions hdfs dfs
4th Chapter HDFs java API 4.5 Java API Introduction

In section 4.4 We already know the HDFs Java API configuration, filesystem, path, and other classes, this section will detail the HDFs Java API, a section to demonstrate more applications. 4.5.1 Java API website

Hadoop 2.7.3 Java API official address
Http://hadoop.apache.org/docs/r2.7.3/api/index.html

As shown in the illustration above, the Java API page is divided into three parts, the upper-left corner is the package (Packages) window, the lower-left corner is all the classes (all classes are) Windows, the right side is the details window.

Here it is recommended to use the "left-bottom index, then view the right details " method, like looking up an English dictionary. and the upper left corner of the package window, use less.

The lower left corner window lists all of the Java interfaces and classes, and you can drag the slider to find the interface or class you want. As shown in the figure, such as finding the filesystem, click the class, and the right window will display the details of the class, including the property method, and so on.
4.5.2 Configuration Class

Method Description
void set (string name, String value) Set the property, name is the property name, value is the property value
void AddResource (String name) Add a configuration Resource

Like what

1. Create Configurator 
Configuration conf = new Configuration ();
Conf.set ("Fs.default.name", "hdfs://192.168.80.131:9000");
Conf.set ("Mapred.jop.tracker", "192.168.80.131:9001");
Configuration conf = new Configuration ();  
Conf.addresource ("Core-default.xml");  
4.5.3 URL and path

URI: Uniform Resource marker (uniform Resource Identifier)
URL: Uniform Resource Locator (Uniform Resource location)
Both the URI and the URL are the location of the resource, that is, the location of the resource, just like the latitude and longitude can indicate where you are in the world. A URI is a broader definition, and a URL is a subset of the URI, meaning that the URL is part of the URI.

The URL is in the java.net package
The path class is often used in conjunction with the URL class, where the path class is located under the Org.apache.hadoop.fs package, naming files or directories in the file system. The path string uses the slash as the directory separator. If you start with a slash, the path string is absolute.

Method Description
Path (String pathstring) A constructor allows you to construct a string into a path
4.5.4 FileSystem class

Hadoop is written by the Java language, where the Hadoop 2.7 series is written by JDK1.7, and we can invoke all the interactive interfaces of HDFs through the Java API. The most commonly used class is the FileSystem class, which contains the implementation of HDFS DFS-related operations.

As shown above, you can see the declaration of the FileSystem class,

Public abstract class filesystem
extends configured
implements Closeable

So the FileSystem class is in the Org.apache.hadoop.fs package, is an abstract class, and then the parent class is configured, which implements the Closeable interface. The Closeable interface is a data source or destination that can be closed. You need to implement the Close method to release resources saved by the object (such as opening a file). The parent class configured has two methods:
void setconf (Configuration conf): Set Configuration Configuration getconf (): Get Configuration

In addition to the above three methods, the FileSystem class commonly used methods are the following table (omitting public)

Method Description
static filesystem Get (Configuration conf) static method, get filesystem instance
Static filesystem get (URI Uri, Configuration conf) static method, get filesystem instance
Static filesystem get (URI Uri, Configuration conf, String user) static method, get filesystem instance, multiple user parameters
Fsdatainputstream open (Path f) Open a file input stream at the path location
void Copyfromlocalfile (path src, path DST) Copy local files to the file system
void Copytolocalfile (path src, path DST) Copy the src file on the file system to local DST
Boolean exists (Path f) Check whether a file or directory exists
Boolean mkdirs (Path f) New all directories (including parent directory), F is the complete directory path
Abstract Boolean mkdirs (Path F, fspermission permission) Create the specified f file on the file system, including the parent directory.
Fsoutputstream Create (Path f) Creates a file for the specified path object, returning an output stream for writing data
Boolean Delete (Path F, Boolean recursive) Permanently deletes the specified file or directory, and if f is an empty directory or file, then the recursive value is ignored. When only recursive=true, a non-empty directory and its contents are deleted.

The Create () method has multiple overloaded versions, allowing us to specify whether to force overwriting of existing files, number of file backups, write file buffer size, file block size, and file permissions.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.