이 기사는 주로 HDFS의 Java API의 액세스 방법을 연구합니다. 특정 코드는 아래에 나와 있으며 자세한 주석이 있습니다.
속도는 최근에 조금 빠릅니다. 시간이 있으면 이것을 마무리 할 수 있습니다
import java.io.ioexception; import java.net.uri; import java.net.urisyntaxexception; import org.apache.hadoop.conf.configuration; import org.apache.hadoop.fs.blocklocation; import org.apache.hadoop.fs.fs.filestatus; import org.apache.hadoop.fs.filesystem; org.apache.hadoop.fs.filesystem; import org.apache.hadoop.fs.fileutil; import org.apache.hadoop.fs.path; import org.apache.hadoop.hdfs.distributedfilesystem; import org.apache.hadoop.hadoop.hdfs.protococ.datanodeinfo;
/** * hdfs 파일 시스템 가져 오기 * @return * @throws ioexception * @throws urisyntaxexception */public static filesystem getfilesystem ()가 ioexception, urisyntaxexception {// urisyntaxexception {// new configuration (new configuration); 파일 시스템 // filesystem fs = filesystem.get (conf); // 지정된 파일 시스템 주소 uri uri = new uri ( "hdfs : // hy : 9000"); // 지정된 파일 시스템을 반환 // 로컬로 테스트하는 경우 파일 시스템을 사용하여 파일 시스템 fs = filesystem.get (uri, conf); @Throws Exception */public static void mkdir ()는 예외를 겪고 {// 파일 시스템 파일 파일 파일 파일 파일을 가져옵니다. // 파일 디렉토리 만들기 fs.mkdirs ( "hdfs : // hy : 9000/hy/weibo"); // resource fs.close ()/ * * delete 파일 또는 파일 파일 또는 파일 또는 파일 또는 파일 파일 또는 파일 또는 파일 파일을 제작합니다. 정적 void rmdir ()는 예외를 겪고 {// 파일 시스템 가져 오기 파일 시스템 파일 시스템 fs = getfilesystem (); // 파일 또는 파일 디렉토리 fs.delete (새로운 경로 ( "hdfs : // hy : 9000/hy/weibo"), true); // public exception*/public exception*/public expler*/public exploy*/@ths. ListAllFile ()은 예외를 발생시킵니다 {// 파일 시스템 파일 시스템 파일 시스템 파일 시스템 fs = getFilesystem (); // 디렉토리 목록 FILESTATUS [] status = fs.listStatus (새 경로 ( "hdfs : // hy : 9000/hy/"); // listory paths = loop gets geted. strates (strates). (Path Path : ListedPaths) {System.out.println (Path);} // resource fs.close ();}/** * 파일을 hdfs * @throws 예외 */public static void copyhdfs ()에 rows {// get system fs = getfilesystem (); 새 경로 ( "/home/hadoop/temp.jar"); // Windows에서 테스트 해야하는 경우 Windows 아래의 경로로 변경해야합니다 (예 : e : //temp.jarpath srcpath = new Path) fs.copyfromlocalfile (srcpath, dstpath); // 리소스 릴리스 fs.close ();}/*** hdfs* @throws 예외*/public static void getfile ()에서 파일을 다운로드하여 예외 {// 파일 시스템 fileSystem fs = getFilesystem (); 소스 Path Path = New PATH ( "hdfs : // y : 9000/hy/weibo/temp.jar"); // 기본 경로, 기본 경로는 Linux // Windows에서 테스트 된 경우 C : // user/ansy/gooktop/path dstpath = new Path ( "d : //")와 같은 Windows 아래의 경로로 변경해야합니다 ( "d : //"); // copytolople (sstolocalfile); resource fs.close ();}/*** HDFS 클러스터 포인트* @Throws Exception*/public static void gethdfsnodes ()에 대한 정보 가져옵니다. 예외 {// 파일 시스템 파일 시스템 fs = getfilesystem (); // 배포 된 파일 분산 파일 hdfs = // getfilesystem) fs; datanodeinfo [] datanodestats = hdfs.getDatanodestats (); // 루프 트래버스 (int i = 0; i <datanodestats.length; i ++) {system.out.println ( "datanote_" + i + "_name :" + datanodestats [i] .gethostname ()); fs.close();}/** * Find the location of a file in the HDFS cluster* @throws Exception */public static void getFileLocal() throws Exception{//Get file system FileSystem fs = getFileSystem();//File path Path path = new Path("hdfs://hy:9000/hy/weibo/temp.jar");//Get file directory filestatus filestatus = fs.getFilestatus (path); // 파일 블록 위치 목록 BlockLocation [] blocklocations = fs.getFileBlockLocations (filestatus, 0, filestatus.getlen ()); // (int i = 0; i <blocklocations.length; length; i ++)에 대한 루프 출력 블록 정보를 가져옵니다. blocklocations [i] .gethosts (); system.out.println ( "block_" + i + "_location :" + hosts [0]);} // 리소스 fs.close ();}를 해제합니다.위는 HDFS Java API의 액세스 방법에 대한이 기사의 전체 내용입니다. 모든 사람에게 도움이되기를 바랍니다. 관심있는 친구는이 사이트의 다른 관련 주제를 계속 참조 할 수 있습니다. 단점이 있으면 메시지를 남겨 두십시오. 이 사이트를 지원해 주신 친구들에게 감사드립니다!