This operation is used to delete a file from the directory.
Following interfaces provide functionality to perform delete file operation:
Following code is used to delete file from the directory. org.apache.Hadoop.fs.FileSystem.delete(org.apache.hadoop.fs.Path, boolean) is used to delete a file from the path provided. Second argument signifies whether delete operation will execute recursively or single time. Providing false will delete single file only.
import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.hdfs.DFSConfigKeys; public class DeleteObject { /* * This program deletes the specified file from the HDFS. */ public static void main(String[] args) throws IOException{ Configuration conf = new Configuration(true); //Create a configuration object to define hdfs properties conf.set(DFSConfigKeys.FS_DEFAULT_NAME_KEY, "hdfs://192.168.0.1:9000"); // URL for your namenode conf.set(DFSConfigKeys.DFS_REPLICATION_KEY, "3"); // Replication count for files you write //Initialize DFS FileSystem object with QueryIO configurations FileSystem dfs = FileSystem.get(conf); dfs.delete(new Path("/queryio/demo/file1.txt"), false); // Deleting just the file } }
HTTP DELETE
operation can be used for delete file action. Following sample is explained using curl
command.
Syntax of curl command for delete operation:
curl -i -X DELETE "http://<host>:<port>/webhdfs/v1/<path>?user.name=<username>&op=DELETE"
-i option
: Include the HTTP-header in the output like server-name, date of the document, HTTP-version etc.-X option
: (HTTP) Specifies a custom request method to use when communicating with the HTTP server.<HOST>
: Hostname of the queryio server.<PORT>
: Port on which server is working.<PATH>
: A valid path to file.user.name=<username>
: QueryIO account username for authentication.op=DELETE
: Delete file operation.Sample Request: curl -i -X DELETE "http://192.168.0.1:50070/webhdfs/v1/queryio/demo/file1.txt?user.name=admin&op=DELETE"
DELETE file operation using WEBHDFS api returns a JSON object.
The following is the actual HTTP request:
DELETE /webhdfs/v1/queryio/demo/file1.txt?user.name=admin&op=DELETE HTTP/1.1 User-Agent: curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5 Host: 192.168.0.1:50070 Accept: */*
Following is the HTTP response sent when QueryIO Server successfully deletes a file:
HTTP/1.1 200 OK Expires: Thu, 01-Jan-1970 00:00:00 GMT Set-Cookie: hadoop.auth="u=admin&p=admin&t=simple&e=1356639516361&s=vJS6Dj8EnrpWbiLWeZ7LuNbVq84=";Path=/ Content-Type: application/json Transfer-Encoding: chunked Server: Jetty(6.1.26) {"boolean":true}