この記事では、主にHDFSのJava APIのアクセス方法を研究しています。特定のコードを以下に示し、詳細なコメントがあります。
最近、ペースは少し速くなっています。時間があれば、これをまとめることができます
java.io.ioexception; Import java.net.uri; import java.net.urisyntaxection; import org.apache.hadoop.configuration; import org.apache.hadoop.fs.blocklocation; Import org.apache.hadoop.fs.filestatus; import interstatus; 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.hdfsprotocol.datanodeinfo;
/** * get hdfs file system * @return * @throws ioexception * @throws urisyntaxexception */public staticファイルシステムgetFilesystem()throws ioException {// urisyntaxexception、urisyntaxexception {// read config fileconfiguration conf = new configuration(); fs = filesystem.get(conf); //指定されたファイルシステムアドレスuri uri = new uri( "hdfs:// hy:9000"); //指定されたファイルシステムを返す//この方法をローカルにテストする場合は、ファイルファイルファイルシステムfs = filesystem.get(uri、conf); return fs; mkdir()スロー例外{//ファイルシステムファイルシステムfs = getfilesystem(); //ファイルディレクトリfs.mkdirs( "hdfs:// hy:9000/hy/weibo"); //リリースリソースfs.close();}/**ファイルファイルまたはファイルディレクトリ* @@throws*/public sitic void*/public sitic void*/**例外{//ファイルシステムファイルシステムfs = getFilesystem(); //ファイルまたはファイルディレクトリfs.delete( "hdfs:// hy:9000/hy/weibo")、true) filesystem fs = getFilesystem(); //ディレクトリコンテンツFileStatus [] fileStatus [] status = fs.listStatus(new Path( "hdfs:// hy:9000/hy/")); //ディレクトリパスですべてのファイルパスを取得します[] listedpaths = fileutil.stat2paths(statuss); {system.out.println(path);} //リソースFs.close();}/** *ファイルをhdfsにアップロードする * @throws例外 */public static void copytohdfs()throws exception {// get file objectファイルファイルシステムpath( "/home/hadoop/temp.jar"); // Windowsでテストする必要がある場合は、たとえば、e://temp.jarpath srcpath = new Path( "e://temp.jar") fs.CopyFromLocalFile(SRCPATH、DSTPATH); //リソースをリリースfs.Close();}/*** hdfsからファイルをダウンロード* @throws例外*/public static void getfile()throws {// get file system filefilesystem fs = getfilesemy(); PATH( "hdfs:// hy:9000/hy/weibo/temp.jar"); //デフォルトパス、デフォルトはlinux //ウィンドウの下でテストされた場合、c:// user/andy/andy/desktop/path dstpath = new path( "d://"); fs.Close(}/*** HDFSクラスターポイントに関する情報を取得* @throws例外*/public static void gethdfsnodes()スロー例外{//ファイルシステムファイルシステムfs = getfilesystem(); datanodestats = hdfs.getDatanodestats(); //ループトラバーサル(int i = 0; i <datanodestats.length; i ++){system.out.println( "datanote_" + i + "_name:" + datanodestats [i] .gethostname() HDFSクラスターのファイルの位置 * @throws例外 */public static void getFileLocal()throws Exception {// Get file System Filesystem fs = getFilesystem(); //ファイルパスパス= new Path( "hdfs:// hy:9000/hy/weibo/temp.jar"); fs.getFileStatus(PATH); //ファイルブロックロケーションリストリストブロックロケーション[] BlockLocations = fs.GetFileBlockLoctations(FileStatus、0、FileStatus.getLen()); //ループ出力ブロック情報(int i = 0; i <blocklocations.length; i ++ {string = hosts = hosts = hosts = blocklocations [i] .gethosts(); system.out.println( "block_" + i + "_location:" + hosts [0]);} //リソースfs.close();}上記は、HDFS Java APIのアクセス方法に関するこの記事のコンテンツ全体です。私はそれが誰にでも役立つことを願っています。興味のある友人は、このサイトの他の関連トピックを引き続き参照できます。欠点がある場合は、それを指摘するためにメッセージを残してください。このサイトへのご支援をありがとうございました!