3.5 Java操作hdfs,Maven配置

/**
* <p>内容描述:操作hadoop</p>
* @author lvjie
* @date 2017年7月7日 上午11:53:50
*/
public class UseHadoop {
//读取集群文件夹下的文件
public void login(FileSystem fs) throws IOException{
Path path =new Path("/log/20170329");
FileStatus[] files=fs.listStatus(path);
for(FileStatus file :files){
System.out.println(file.isDirectory());
System.out.println(file.getPath().getName());
System.out.println(file.getModificationTime());
System.out.println(file.getLen());
}
}
/**
* 创建文件
* @throws Exception
*/
public void mkFile(FileSystem fs)throws Exception{
//在根目录下创建zs文件夹
Path path =new Path("/zs");
fs.mkdirs(path);
}
/**
* 下载集群指定文件到本地
* @throws Exception
*/
public void open(FileSystem fs) throws Exception{
//下载集群/user/root/input/kmeans.txt文件到D:/1111.txt
Path path =new Path("/user/root/input/kmeans.txt");
FSDataInputStream in = fs.open(path);
FileUtils.copyInputStreamToFile(in, new File("D:/1111.txt"));
}
/**
* 上传文件到集群
* @throws Exception
*/
public void upload(FileSystem fs)throws Exception{
//上传D:/1111.txt到集群/user/root/output/1111.txt
Path path =new Path("/user/root/output/1111.txt");
FSDataOutputStream out = fs.create(path);
FileUtils.copyFile(new File("D:/1111.txt"), out);
}
/**
* 删除文件
* @throws IOException
*/
public void delFile(FileSystem fs) throws IOException{
//删除集群zs文件
Path dstPath = new Path("/zs");
if(fs.exists(dstPath)){
fs.delete(dstPath, true) ;
}else{
return ;
}
}
public static void main(String[] args) {
UseHadoop use = new UseHadoop();
try {
FileSystem fs =null;
Configuration config =new Configuration();
config.set("fs.defaultFS", "hdfs://node1:9000");
fs =FileSystem.get(config);
//use.login(fs);
use.mkFile(fs);
fs.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
========================================================================================================
<dependency>   
     <groupId> org.apache.hadoop </groupId>   
     <artifactId> hadoop-common </artifactId>   
     <version> 2.5.1 </version>   
</dependency>   
<dependency>   
     <groupId> org.apache.hadoop </groupId>   
     <artifactId> hadoop-hdfs </artifactId>   
     <version> 2.5.1 </version>   
</dependency>   
<dependency>   
     <groupId> org.apache.hadoop </groupId>   
     <artifactId> hadoop-client </artifactId>   
     <version> 2.5.1 </version>   
</dependency>
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值