Artikel ini terutama mempelajari metode akses API Java HDFS. Kode spesifik ditampilkan di bawah ini dan memiliki komentar terperinci.
Kecepatannya sedikit cepat. Saat Anda punya waktu, Anda dapat membungkus ini
impor java.io.ioException; impor java.net.uri; impor java.net.urisyntaxException; impor org.apache.hadoop.conf.configuration; import org.apache.hadoop.fs.blocklocation; import org.apache.hadoop.filestatus; org.apache.hadoop.fs.filesystem; impor org.apache.hadoop.fs.fileutil; import org.apache.hadoop.fs.path; impor org.apache.hadoop.hdfs.distributeDfileSystem; Impor org.apache.hadoop.hdfs.protocinstem; org.apache.apache.hadoop.hdfs.protocinse;
/** * Dapatkan sistem file hdfs * @return * @throws ioException * @throws UrisyntaxException */public static fileSystem getFileSystem () melempar ioException, kembalinya UrisyntaxException {// Baca/Baca Konfigurasi Konfigurasi. Sistem File // FileSystem fs = FileSystem.get (conf); // Alamat sistem file yang ditentukan URI URI = URRI BARU ("HDFS: // HY: 9000"); // Kembalikan sistem file yang ditentukan // jika Anda menguji secara lokal, Anda perlu menggunakan metode ini untuk mendapatkan sistem file file fs = ugret (ugret (ugret (u); @Throws Exception */public static void mkdir () melempar Exception {// Dapatkan file file fileSystem fs = getFileSyStem (); // buat direktori fs.mkdirs (jejak baru ("hdfs: // hy: 9000/hy/weibo")); // rilis sumber daya fs. * @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ static void rmdir () melempar Exception {// Dapatkan sistem file fileSystem fs = getFileSyStem (); // hapus file atau direktori file fs.delete (get @hdfs: // hy: 9000/hy/weibo "), true); // rilis sumber daya fs.close ();}; ListAllFile () melempar Exception {// Dapatkan sistem file file sistem fs = getFileSystem (); // Cantumkan konten direktori filestatus [] status = fs.liststatus (Path baru ("hdfs: // hy: 9000/hy/")); // Dapatkan semua jalur file di jalur direktori [] (lOkeed [] (setiap lip [] (LOPS =/HY/"); // Dapatkan semua jalur file di jalur Direktori [] (Everypor. File untuk (path path: listedpaths) {System.out.println (path);} // Lepaskan sumber daya fs.close ();}/** * Unggah file ke hdfs * @Throws Exception */public static copyToHdfs () melempar pengecualian {// get file file fileux fs = public fs = gets () gets () lempar () lemparan {// dapatkan file file fileux fs = public fs = gets () gets; srcpath = jalur baru ("/home/hadoop/temp.jar"); // Jika Anda perlu menguji di windows, Anda perlu mengubahnya ke jalur di bawah windows, misalnya, e: //temp.jarpath srcpath = jalur baru ("e: //temp.jar"); // jalur tujuan dstpath = "e: //temp.jar"); Path("hdfs://hy:9000/hy/weibo");//Implement file upload fs.copyFromLocalFile(srcPath, dstPath);//Release the resource fs.close();}/** * Download the file from HDFS* @throws Exception */public static void getFile() throws Exception{//Get file system FileSystem fs = getFileSystem (); // sumber jalur file path srcpath = jalur baru ("hdfs: // hy: 9000/hy/weibo/temp.jar"); // jalur default, default adalah linux // jika diuji // devliver, Anda perlu mengubah ke jalur di bawah windows, seperti c: // user/andy/deckt/deskt Anda perlu ke jalur di bawah windows, seperti c: // user/andy/destop/deskt Anda perlu ke jalur di bawah windows, seperti c: // user/andy/deskt/deskop fs.copyToLocalFile(srcPath, dstPath);//Release the resource fs.close();}/** * Get information about HDFS cluster points* @throws Exception */public static void getHDFSNodes() throws Exception{//Get file system FileSystem fs = getFileSystem();//Get distributed file system DistributedFileSystem hdfs = (distributedfileSystem) fs; // dapatkan semua node datanodeInfo [] datanodesTats = hdfs.getDatanodesTats (); // loop traversal untuk (int i = 0; i <datasodestats.length; i ++) {System.out.println ("datasodestats.Length; i ++) {System.out.println (" DATANODESTATS.Length; i ++) {System.out.println ("DATANODESTATS.Length; i ++) {System.out.println (" DATANODESTAT. " datanodesTats [i] .getHostName ());} // Lepaskan sumber daya fs.close ();}/*** Temukan lokasi file dalam cluster hdfs* @throws Exception*/public static void getFilelocal () melempar Exception {// Get file sistem file static fs = getfilelocal () lempar () (Get File File FileStem fs fs = getfiley Path ("hdfs: // hy: 9000/hy/weibo/temp.jar"); // Dapatkan direktori file filestatus filestatus = fs.getfilestatus (path); // Dapatkan blok file blok blocklocation [] blocklocations = fs.getFileBlockLocations (filestatus, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, file. 0;Di atas adalah seluruh konten artikel ini tentang metode akses HDFS Java API. Saya harap ini akan membantu semua orang. Teman yang tertarik dapat terus merujuk ke topik terkait lainnya di situs ini. Jika ada kekurangan, silakan tinggalkan pesan untuk menunjukkannya. Terima kasih teman atas dukungan Anda untuk situs ini!