1、创建HDFS配置信息静态代码块
static Configuration conf = new Configuration();
//具体配置信息根据自己的安装环境参数来
static {
//HDFS通信地址
String hdfsUrl="hdfs://localhost:9000";
conf.set("fs.defaultFS", hdfsUrl);
// conf.set("dfs.nameservices", "nameservice1");
// conf.set("dfs.ha.namenodes.nameservice1", "nn1,nn2");
// conf.set("dfs.namenode.rpc-address.nameservice1.nn1", "xxx:8020");
// conf.set("dfs.namenode.rpc-address.nameservice1.nn2", "xxx:8020");
// conf.set("dfs.client.failover.proxy.provider.nameservice1"
// ,"org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider");
// conf.addResource("classpath:/hadoop/core-site.xml");
// conf.addResource("classpath:/hadoop/hdfs-site.xml");
// conf.addResource("classpath:/hadoop/mapred-site.xml");
}
2、创建新文件
/**
* 创建新文件
* @author LiHaoShan
* @param dst HDFS路径
* @param contents 文件内容
* */
public static Map createFile(String dst , byte[] contents) throws IOException{
FileSystem fs = FileSystem.get(conf);
//目标路径
Path dstPath = new Path(dst);
URI uri=dstPath.toUri();
String path=uri.getPath();
//打开一个输出流
FSDataOutputStream outputStream = fs.create(dstPath);
outputStream.write(contents);
Map map= Maps.newHashMap();
map.put("savePath",path);
outputStream.close();
fs.close();