Common hadoop hdfs operations java code

Source: Internet
Author: User
Tags readfile

Common hadoop hdfs operations java code

Package hadoopTest; import java. io. IOException; import java. util. logging; import org. apache. hadoop. conf. configuration; import org. apache. hadoop. fs. blockLocation; import org. apache. hadoop. fs. FSDataInputStream; import org. apache. hadoop. fs. FSDataOutputStream; import org. apache. hadoop. fs. fileStatus; import org. apache. hadoop. fs. fileSystem; import org. apache. hadoop. fs. path; import org. apache. hadoop. hdfs. distributedFileSystem; import org. apache. hadoop. hdfs. protocol. datanodeInfo; public class MKdir {/*** @ Description: create a file * @ Author: Cao bigang * @ time: october 15, 2015 11:32:24 * @ throws Exception */private static void mkdir () throws Exception {Configuration conf = new Configuration (); FileSystem fileSystem = FileSystem. get (conf); Path path = new Path ("hadoop_test"); fileSystem. mkdirs (path);}/*** @ Description: delete a file * @ Author: Cao bigang * @ time: october 15, 2015 11:33:05 * @ throws IOException */private static void deletedir () throws IOException {Configuration conf = new Configuration (); FileSystem fileSystem = FileSystem. get (conf); Path path = new Path ("hadoop_test"); fileSystem. delete (path, true);}/*** @ Description: Write File * @ Author: Cao bigang * @ time: october 15, 2015 11:37:46 * @ param args * @ throws IOException * @ throws Exception */private static void writerFile () throws IOException {Configuration conf = new Configuration (); FileSystem fileSystem = FileSystem. get (conf); Path path = new Path ("hadoop_test.txt"); FSDataOutputStream dataOutputStream = fileSystem. create (path); dataOutputStream. writeUTF ("hello hadoop test");}/*** @ throws IOException * @ Description: Read the file * @ Author: Cao bigang * @ time: october 15, 2015 11:42:06 */private static void readFile () throws IOException {Configuration conf = new Configuration (); FileSystem fileSystem = FileSystem. get (conf); Path path = new Path ("hadoop_test.txt"); if (fileSystem. exists (path) {FSDataInputStream dataInputStream = fileSystem. open (path); FileStatus fileStatus = fileSystem. getFileStatus (path); byte B [] = new byte [Integer. parseInt (String. valueOf (fileStatus. getLen ()]; dataInputStream. readFully (B) ;}}/*** @ Description: traverse directory files * @ Author: Cao bigang * @ time: october 15, 2015 1:21:32 * @ throws IOException */private static void listDir () throws IOException {Configuration conf = new Configuration (); FileSystem fileSystem = FileSystem. get (conf); Path path = new Path ("/"); FileStatus [] fileStatus = fileSystem. listStatus (path); print (fileStatus);} private static void print (FileStatus [] fileStatus) {for (int I = 0; I <fileStatus. length; I ++) {FileStatus status = fileStatus [I]; if (status. isDir () {} else {System. out. println (status. getPath (). getName () ;}}/ *** @ throws Exception * @ Description: File Location * @ Author: Cao bigang * @ time: october 15, 2015 1:26:13 */private static void fileLocal () throws Exception {Configuration conf = new Configuration (); FileSystem fileSystem = FileSystem. get (conf); Path path = new Path ("/user/centos"); FileStatus fileStatus = fileSystem. getFileStatus (path); BlockLocation [] blockLocations = fileSystem. getFileBlockLocations (fileStatus, 0, fileStatus. getLen (); for (int I = 0; I <blockLocations. length; I ++) {String hosts [] = blockLocations [I]. getHosts (); for (int j = 0; j 

 

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.