hdfs提供了良好的文件上传方式,
首先,我们confoiguraton ,
然后,创建文件系统fs,
再指定路径copy即可
以下是编码测试源码
package com.map2.www;
import java.io.IOException;
import java.io.Serializable;
import java.io.IOException;
import java.util.Iterator;
import java.util.StringTokenizer;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.mapred.TextInputFormat;
import org.apache.hadoop.mapred.TextOutputFormat;
import jdk.nashorn.internal.ir.Block;
import org.apache.hadoop.classification.InterfaceAudience.Public;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.Hdfs;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.fs.Path;
public class map2 {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Configuration conf=new Configuration();
FileSystem fs=FileSystem.get(conf);
Path src=new Path("/home/joke/test");
Path dst=new Path("/home/joke/testout");
fs.copyFromLocalFile(src, dst);
System.out.println("Upload to "+ conf.get("fs.default.name"));
System.out.println("nice");
}
}
相应的,HDFS文件创建关键代码如下:
Configuration conf=new Configuration();
FileSystem fs=FileSystem.get(conf);
byte bs[]="hello everyday".getBytes();
//Path src=new Path("/home/joke/test");
Path dst=new Path("/home/joke/testout/yu.txt");
FSDataOutputStream ops=fs.create(dst);
ops.write(bs,0,bs.length);
//fs.copyFromLocalFile(src, dst);
// System.out.println("Upload to "+ conf.get("fs.default.name"));
//
System.out.println("asdh");
相应的,文件目录创建
fs.mkdir(path,false);