package linlintest;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils; public class HdfsFileSystem { public static void main(String[] args) throws Exception {
//String uri="hdfs://LL-167:8020/"; //hdfs 地址
// String remote="hdfs://LL-167:8020/lin/in1/1.txt"; //hdfs上的路径
String uri="hdfs://192.168.0.151:8020/"; //hdfs 地址
String local="C:/Users/Administrator/Desktop/a.txt"; //本地路径
String remote="hdfs://192.168.0.151:8020/Workspace/houlinlin";
Configuration conf = new Configuration(); //cat(conf ,uri,"hdfs://LL-167:8020/lin/in1/1.txt");
//download(conf ,uri,remote,local);
// delete(conf ,uri,"hdfs://192.168.0.173:8020/Workspace/houlinlin");
// markDir(conf ,uri,"hdfs://192.168.0.151:8020/Workspace/houlinlin/file/apply/2014/8a8380c7459dd8b90145a1fafb500235"); // checkDir(uri,"d:/file");
// getFile(conf ,uri,"","hdfs://192.168.0.151:8020/Workspace/houlinlin/a.txt");
copyFile(conf,uri,"C:/Users/Administrator/Desktop/8a8380c745d05d370145d06719aa3c89.txt","hdfs://192.168.0.151:8020/Workspace/houlinlin/file/apply/2014/8a8380c7459dd8b90145a1fafb500235");
// ls(conf ,"hdfs://fulonghadoop","hdfs://fulonghadoop/");
}
/**
* 上传文件
* @param conf
* @param local
* @param remote
* @throws IOException
*/
public static void copyFile(Configuration conf , String uri , String local, String remote) throws IOException {
FileSystem fs = FileSystem.get(URI.create(uri), conf);
fs.copyFromLocalFile(new Path(local), new Path(remote));
System.out.println("copy from: " + local + " to " + remote);
fs.close();
} /**
* 获取hdfs上文件流
* @param conf
* @param uri
* @param local
* @param remote
* @throws IOException
*/
public static void getFileStream(Configuration conf , String uri , String local, String remote) throws IOException{
FileSystem fs = FileSystem.get(URI.create(uri), conf);
Path path= new Path(remote);
FSDataInputStream in = fs.open(path);//获取文件流
FileOutputStream fos = new FileOutputStream("C:/Users/Administrator/Desktop/b.txt");//输出流
int ch = 0;
while((ch=in.read()) != -1){
fos.write(ch);
}
System.out.println("-----");
in.close();
fos.close();
} /**
* 创建文件夹
* @param conf
* @param uri
* @param remoteFile
* @throws IOException
*/
public static void markDir(Configuration conf , String uri , String remoteFile ) throws IOException{
FileSystem fs = FileSystem.get(URI.create(uri), conf);
Path path = new Path(remoteFile); fs.mkdirs(path);
System.out.println("创建文件夹"+remoteFile); }
/**
* 查看文件
* @param conf
* @param uri
* @param remoteFile
* @throws IOException
*/
public static void cat(Configuration conf , String uri ,String remoteFile) throws IOException {
Path path = new Path(remoteFile);
FileSystem fs = FileSystem.get(URI.create(uri), conf);
FSDataInputStream fsdis = null;
System.out.println("cat: " + remoteFile);
try {
fsdis = fs.open(path);
IOUtils.copyBytes(fsdis, System.out, 4096, false);
} finally {
IOUtils.closeStream(fsdis);
fs.close();
}
}
/**
* 下载 hdfs上的文件
* @param conf
* @param uri
* @param remote
* @param local
* @throws IOException
*/
public static void download(Configuration conf , String uri ,String remote, String local) throws IOException {
Path path = new Path(remote);
FileSystem fs = FileSystem.get(URI.create(uri), conf);
fs.copyToLocalFile(path, new Path(local));
System.out.println("download: from" + remote + " to " + local);
fs.close();
}
/**
* 删除文件或者文件夹
* @param conf
* @param uri
* @param filePath
* @throws IOException
*/
public static void delete(Configuration conf , String uri,String filePath) throws IOException {
Path path = new Path(filePath);
FileSystem fs = FileSystem.get(URI.create(uri), conf);
fs.deleteOnExit(path);
System.out.println("Delete: " + filePath);
fs.close();
}
/**
* 查看目录下面的文件
* @param conf
* @param uri
* @param folder
* @throws IOException
*/
public static void ls(Configuration conf , String uri , String folder) throws IOException {
Path path = new Path(folder);
FileSystem fs = FileSystem.get(URI.create(uri), conf);
FileStatus[] list = fs.listStatus(path);
System.out.println("ls: " + folder);
System.out.println("==========================================================");
for (FileStatus f : list) {
System.out.printf("name: %s, folder: %s, size: %d\n", f.getPath(),f.isDirectory() , f.getLen());
}
System.out
.println("==========================================================");
fs.close();
} /**
*
* @param parentName 绝对路径地址
* @throws Exception
*/
public static void checkDir(String uri,String parentName) throws Exception{
//D:\file
Configuration conf = new Configuration();
File file = new File(parentName);
boolean flag = true;
while (flag) {
//查出parentName下的所有文件
File[] fileNames = file.listFiles(new FileFilter());
if(fileNames != null) {
for (int i = 0; i < fileNames.length; i++) {
File f = fileNames[i];
//System.out.println("parent directory:"+f.getParent()+",file name:"+f.getName());
System.out.println("parent directory:"+f.getParent().replace("\\", "/").substring(2)+",file name:"+f.getName());
String remoteFolrd= "hdfs://192.168.0.173:8020/Workspace/houlinlin"+f.getParent().replace("\\", "/").substring(2);
markDir(conf ,uri,remoteFolrd);
copyFile(conf ,uri,f.getParent()+"\\"+f.getName(),remoteFolrd);
}
}
//查出parentName下的所有目录
File[] directories = file.listFiles(new DirectortyFilter());
if(directories != null) {
for (int i = 0; i < directories.length; i++) {
File dir = directories[i];
//绝对路径
String path = dir.getAbsolutePath();
//递归
checkDir(uri,path);
}
}
flag = false;
}
} }

Hadoop HDFS文件系统通过java FileSystem 实现上传下载等的更多相关文章

  1. JAVA 实现FTP上传下载(sun.net.ftp.FtpClient)

    package com.why.ftp; import java.io.DataInputStream; import java.io.File; import java.io.FileInputSt ...

  2. CentOS下安装配置NFS并通过Java进行文件上传下载

    1:安装NFS (1)安装 yum install nfs-utils rpcbind (2)启动rpcbind服务 systemctl restart rpcbind.service 查看服务状态 ...

  3. java实现文件上传下载

    喜欢的朋友可以关注下,粉丝也缺. 今天发现已经有很久没有给大家分享一篇技术文章了,于是想了一下给大家分享一篇java实现文件上传下载功能的文章,不喜欢的希望大家勿喷. 想必大家都知道文件的上传前端页面 ...

  4. 【转】Java IOUtils方式上传下载文件 on HDFS

    [From]https://www.cnblogs.com/areyouready/p/9795442.html package com.css.hdfs04; import java.io.File ...

  5. java web 文件上传下载

    文件上传下载案例: 首先是此案例工程的目录结构:

  6. FasfDFS整合Java实现文件上传下载

    文章目录     一 : 添加配置文件     二 : 加载配置文件         1. 测试加载配置文件         2. 输出配置文件     三:功能实现         1.初始化连接信 ...

  7. fastDFS与java整合文件上传下载

    准备 下载fastdfs-client-java源码 源码地址 密码:s3sw 修改pom.xml 第一个plugins是必需要的,是maven用来编译的插件,第二个是maven打源码包的,可以不要. ...

  8. java web service 上传下载文件

    1.新建动态web工程youmeFileServer,新建包com,里面新建类FileProgress package com; import java.io.FileInputStream; imp ...

  9. FasfDFS整合Java实现文件上传下载功能实例详解

    https://www.jb51.net/article/120675.htm 在上篇文章给大家介绍了FastDFS安装和配置整合Nginx-1.13.3的方法,大家可以点击查看下. 今天使用Java ...

随机推荐

  1. Visual Studio 2012 [ADO.NET 实体数据模型]丢失没有的解决方法

    首先打开控制面板,看是否已经安装EF,如果已经安装,先卸载,然后,首先打开安装包,找到/packages/EFTools目录下的EFTools.msi,将它们复制自己计算机的某一目录下,例如:C:\t ...

  2. 如何不通过系统升级来安装window10正式版?(特别针对Xp用户)

    今天是个特殊的日子7/29,相信大家都等了很久了吧,win10正式版终于上线,一些不懂电脑的人只会通过360和腾讯管家等来升级到win10(XP用户除外),而对于大多数像我这样对系统比较热衷的人,我相 ...

  3. Careercup - Google面试题 - 5680330589601792

    2014-05-08 23:18 题目链接 原题: If you have data coming in rapid succession what is the best way of dealin ...

  4. MyEclipse构建WebService案例

    Hi,大家好! 今天主要和大家分享,如何搭建一个Web服务,做Android开发,不可避免会涉及到客户端开发,我们怎么样来实现一个服务端,怎么样来实现一个客户端,并相互传递数据.就算调用别人的服务时, ...

  5. Django RequestContext用法

    模版中的变量由context中的值来替换,如果在多个页面模版中含有相同的变量,比如:每个页面都需要{{user}},笨办法就是在每个页面的请求视图中都把user放到context中.   from d ...

  6. Oracle NULL 和空值

      如果你工作中用到了Oracle,你必须要留意NULL和空值的处理与SQL Server上的不同.现在让我们看些例子. 建立这张数据库表并插入记录 CREATE TABLE TestNull(Col ...

  7. MySQL查看表占用空间大小(转)

    MySQL查看表占用空间大小(转) //先进去MySQL自带管理库:information_schema //自己的数据库:dbwww58com_kuchecarlib //自己的表:t_carmod ...

  8. 解决JS文件页面加载时的阻塞

    关于页面加载时的时间消费,许多书中都做出了介绍,也提出了很多种方法.本文章就详细介绍XHR注入. 概述:JS分拆的方法 1.XHR注入:就是用ajax异步请求同域包含脚本的文件,然后将返回的字符串转化 ...

  9. hadoop,spark,linux上常用命令

    记下常用命令,慢慢补充 1.hadoop 查看hdfs上的目录: hadoop fs -ls /给hdfs上目录授予权限:   hadoop fs -chmod 777 /tmp/hive 在hdfs ...

  10. php curl 分离header和body信息

    php中可以通过curl来模拟http请求,同时可以获取http response header和body,当然也设置参数可以只获取其中的某一个.当设置同时获取response header和body ...