代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
Hadoop HDFS文件系统通过java FileSystem 实现上传下
作者:
/ 发布于
2017/5/3
/
590
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; } } }
试试其它关键字
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
贡献的其它代码
Label
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3