public class MyReadAndWrite {
static FileSystem fs;
static {
try {
fs=FileSystem.get(new URI("hdfs://192.168.56.122:9000"),new Configuration());
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
从hdfs上下载文件
public void read(){
try {
InputStream is = fs.open(new Path("/mydemo/hmm/user103.log"));
OutputStream os = new FileOutputStream("d://user103.log");
IOUtils.copyBytes(is,os,4096,true);
} catch (IOException e) {
e.printStackTrace();
}
}
往hdfs上上传文件
public void write(){
try {
OutputStream os = fs.create(new Path("/mydemo/hmm/user103.log"));
InputStream is = new FileInputStream("d://user103.log");
IOUtils.copyBytes(is,os,4096,true);
} catch (IOException e) {
e.printStackTrace();
}
}
往hdfs上上传文件
public static void main(String[] args) {
MyReadAndWrite mrw = new MyReadAndWrite();
mrw.write();
}