أمثلة على عمليات تشغيل ملف HDFS ، بما في ذلك تحميل الملفات إلى HDF ، وتنزيل الملفات من HDFs ، وحذف الملفات على HDFs.
نسخة الكود كما يلي:
استيراد org.apache.hadoop.conf.configuration ؛
استيراد org.apache.hadoop.fs.*؛
استيراد java.io.file ؛
استيراد java.io.ioException ؛
الطبقة العامة hadoopfile {
التكوين الخاص conf = null ؛
hadoopfile العامة () {
conf = التكوين الجديد () ؛
Conf.addresource (مسار جديد ("/hadoop/etc/hadoop/core-site.xml")) ؛
}
hadoopfile العامة (التكوين conf) {
this.conf = conf ؛
}
SendFile المنطقية العامة (مسار السلسلة ، سلسلة محلية) {
ملف الملف = ملف جديد (localfile) ؛
if (! file.isfile ()) {
System.out.println (file.getName ()) ؛
العودة كاذبة
}
يحاول {
FileSystem localfs = fileystem.getLocal (conf) ؛
نظام fileystem hadoopfs = fileystem.get (conf) ؛
المسار hadpath = مسار جديد (المسار) ؛
FSDataOutputStream fsout = hadoopfs.create (مسار جديد (path+"/" file.getName ())) ؛
FSDatainputStream FSIN = localfs.open (مسار جديد (محلي)) ؛
Byte [] buf = new byte [1024] ؛
int readBytes = 0 ؛
بينما ((readBytes = fsin.read (buf))> 0) {
fsout.write (buf ، 0 ، readBytes) ؛
}
fsin.close () ؛
fsout.close () ؛
filestatus [] hadfiles = hadoopfs.liststatus (hadpath) ؛
لـ (filestatus fs: hadfiles) {
system.out.println (fs.toString ()) ؛
}
العودة صحيح.
} catch (ioException e) {
E.PrintStackTrace () ؛
}
العودة كاذبة
}
Delfile المنطقية العامة (سلسلة hadfile) {
يحاول {
نظام fileystem hadoopfs = fileystem.get (conf) ؛
المسار hadpath = مسار جديد (hadfile) ؛
path p = hadpath.getParent () ؛
boolean rtnval = hadoopfs.delete (hadpath ، true) ؛
filestatus [] hadfiles = hadoopfs.liststatus (p) ؛
لـ (filestatus fs: hadfiles) {
system.out.println (fs.toString ()) ؛
}
إرجاع rtnval ؛
} catch (ioException e) {
E.PrintStackTrace () ؛
}
العودة كاذبة
}
تنزيل Boolean Public (String Hadfile ، String LocalPath) {
يحاول {
FileSystem localfs = fileystem.getLocal (conf) ؛
نظام fileystem hadoopfs = fileystem.get (conf) ؛
المسار hadpath = مسار جديد (hadfile) ؛
FSDataOutputStream fsout = localfs.create (مسار جديد (LocalPath+"/"+hadPath.getName ())) ؛
fsdatainputstream fsin = hadoopfs.open (hadpath) ؛
Byte [] buf = new byte [1024] ؛
int readBytes = 0 ؛
بينما ((readBytes = fsin.read (buf))> 0) {
fsout.write (buf ، 0 ، readBytes) ؛
}
fsin.close () ؛
fsout.close () ؛
العودة صحيح.
} catch (ioException e) {
E.PrintStackTrace () ؛
}
العودة كاذبة
}
}