Exemplos de operações de operação de arquivos HDFS, incluindo o upload de arquivos para HDFS, baixando arquivos do HDFS e excluindo arquivos no HDFS.
A cópia do código é a seguinte:
importar org.apache.hadoop.conf.configuration;
importar org.apache.hadoop.fs.*;
importar java.io.file;
importar java.io.ioException;
classe pública hadoopfile {
Configuração privada conf = null;
public hadoopfile () {
conf = new Configuration ();
conf.addresource (novo caminho ("/hadoop/etc/hadoop/core-site.xml"));
}
public HadoopFile (Configuration conf) {
this.conf = conf;
}
public boolean Sendfile (String Path, String LocalFile) {
Arquivo de arquivo = novo arquivo (LocalFile);
if (! file.isfile ()) {
System.out.println (file.getName ());
retornar falso;
}
tentar {
Filesystem localfs = filesystem.getLocal (conf);
Filesystem hadoopfs = filesystem.get (conf);
Caminho hadpath = novo caminho (caminho);
FsdataOutputStream fsout = hadoopfs.create (novo caminho (caminho+"/"+file.getName ()));
FsdatainputStream fsin = localfs.open (novo caminho (LocalFile));
byte [] buf = novo byte [1024];
int readBytes = 0;
while ((readbytes = fsin.read (buf))> 0) {
fsout.write (buf, 0, readbytes);
}
fsin.close ();
fsout.close ();
Filestatus [] hadfiles = hadoopfs.liststatus (hadpath);
para (filestatus fs: hadfiles) {
System.out.println (fs.toString ());
}
retornar true;
} catch (ioexception e) {
E.PrintStackTrace ();
}
retornar falso;
}
public boolean delfile (string hadfile) {
tentar {
Filesystem hadoopfs = filesystem.get (conf);
Caminho hadpath = novo caminho (hadfile);
Caminho p = hadpath.getParent ();
rtnval booleano = hadoopfs.delete (hadpath, verdadeiro);
Filestatus [] hadfiles = hadoopfs.liststatus (p);
para (filestatus fs: hadfiles) {
System.out.println (fs.toString ());
}
retornar rtnval;
} catch (ioexception e) {
E.PrintStackTrace ();
}
retornar falso;
}
public boolean DownloadFile (String hadfile, String localPath) {
tentar {
Filesystem localfs = filesystem.getLocal (conf);
Filesystem hadoopfs = filesystem.get (conf);
Caminho hadpath = novo caminho (hadfile);
FsdataOutputStream fsout = localfs.create (novo caminho (LocalPath+"/"+HadPath.getName ()));
FsdatainputStream fsin = hadoopfs.open (hadpath);
byte [] buf = novo byte [1024];
int readBytes = 0;
while ((readbytes = fsin.read (buf))> 0) {
fsout.write (buf, 0, readbytes);
}
fsin.close ();
fsout.close ();
retornar true;
} catch (ioexception e) {
E.PrintStackTrace ();
}
retornar falso;
}
}