HDFS For Java
来源:互联网 发布:hdfs java创建目录 编辑:程序博客网 时间:2024/06/11 00:15
package cn.hwadee.wuyang.hdfs.utils;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.OutputStream;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 ConfigurationUtil { private static String url = "hdfs://192.168.193.201:9000"; private static String user = "wuyang"; private static FileSystem fs; /** * 获取文件系统 * @return */ public static FileSystem getFileSystem(){ Configuration conf = new Configuration(); try { return FileSystem.get(URI.create(url), conf, user); } catch (Exception e) { return null; } } /** * 创建文件夹 * @param path * @throws IOException */ public static Boolean createFileDir(Path path) throws IOException{ Configuration conf = new Configuration(); try { fs = FileSystem.get(URI.create(url), conf, user); if(!fs.exists(path)){ fs.mkdirs(path); System.out.println(path.getName()+"文件夹创建成功!!!"); return true; } return false; } catch (Exception e) { e.getMessage(); return false; }finally { fs.close(); } } /** * 删除文件 * @param path * @throws IOException */ public static Boolean deleteFile(Path path) throws IOException{ Configuration conf = new Configuration(); try { fs = FileSystem.get(URI.create(url), conf, user); if(fs.exists(path)){ fs.delete(path, true); System.out.println(path.getName()+"文件删除成功!!!"); return true; } return false; } catch (Exception e) { e.getMessage(); return false; }finally { fs.close(); } } /** * 重命名 * @param name1 * @param name2 * @throws IOException */ public static Boolean fileRename(Path name1,Path name2) throws IOException { Configuration conf = new Configuration(); try { fs = FileSystem.get(URI.create(url), conf, user); if (fs.exists(name1)) { fs.rename(name1, name2); System.out.println(name2.getName()+"替换"+name1.getName()+"成功!!!"); return true; } return false; } catch (Exception e) { e.getMessage(); return false; }finally { fs.close(); } } /** * 创建文件 * @param path * @return */ public static Boolean createFile(Path path){ Configuration conf = new Configuration(); try { fs = FileSystem.get(URI.create(url), conf, user); if(!fs.exists(path)){ fs.create(path); System.out.println(path.getName()+"文件创建成功!!!"); return true; }else{ System.out.println(path.getName()+"文件已经存在!!!"); return false; } }catch (Exception e) { e.getMessage(); return false; }finally { try { fs.close(); } catch (IOException e) { e.getMessage(); } } } /** * 获取path下文件 * @param path * @return */ public static FileStatus[] listFile(Path path){ Configuration conf = new Configuration(); try { fs = FileSystem.get(URI.create(url), conf, user); if(fs.exists(path)){ FileStatus[] list = fs.listStatus(path); System.out.println("文件长度:"+list.length); return list; } return null; } catch (Exception e) { e.getMessage(); return null; } } /** * 从hdfs上下载文件 * @param hdfspath * @param locationpath * @return */ public static Boolean downLoadFileFromHDFS(Path hdfspath,Path locationpath){ Configuration conf = new Configuration(); try { fs = FileSystem.get(URI.create(url), conf, user); if(fs.exists(hdfspath)){ fs.copyToLocalFile(hdfspath, locationpath); System.out.println("download: from" + hdfspath.getName() + " to " + locationpath.getName()); return true; } return false; } catch (Exception e) { e.getMessage(); return false; }finally { try { fs.close(); } catch (IOException e) { e.getMessage(); } } } /** * 拷贝文件到HDFS * @param local * @param remote * @throws IOException */ public static Boolean copyFileToHDFS(String local, String remote) throws IOException { Configuration conf = new Configuration(); try { fs = FileSystem.get(URI.create(url), conf, user); fs.copyFromLocalFile(new Path(local), new Path(remote)); System.out.println("copy from: " + local + " to " + remote); return true; } catch (Exception e) { e.getMessage(); return false; }finally { try { fs.close(); } catch (IOException e) { e.getMessage(); } } } /** * 查看文件中的内容 * @param remoteFile * @return * @throws IOException * @throws InterruptedException */ public static String catFile(String remoteFile) throws IOException, InterruptedException { Path path = new Path(remoteFile); Configuration conf = new Configuration(); fs = FileSystem.get(URI.create(url), conf, user); FSDataInputStream fsdis = null; OutputStream baos = new ByteArrayOutputStream(); String str = null; try { fsdis = fs.open(path); IOUtils.copyBytes(fsdis, baos, 4096, false); str = baos.toString(); } finally { IOUtils.closeStream(fsdis); fs.close(); } return str; }}
阅读全文
2 0
- HDFS For Java
- HDFS For hdfs-site.xml
- HDFS java 操作 HDFS
- java.io.IOException: No FileSystem for scheme: hdfs
- java.io.IOException: No FileSystem for scheme: hdfs异常解决
- java.io.IOException: No FileSystem for scheme: hdfs
- java.io.IOException: No FileSystem for scheme: hdfs
- Java.io.IOException: No FileSystem for scheme: hdfs
- java.io.IOException: No FileSystem for scheme: hdfs
- java.io.IOException: No FileSystem for scheme: hdfs异常解决
- java.io.IOException: No FileSystem for scheme: hdfs
- java.io.IOException: No FileSystem for scheme: hdfs
- java.io.IOException: No FileSystem for scheme: hdfs
- hdfs java读写hdfs demo
- No FileSystem for scheme: hdfs
- No FileSystem for scheme: hdfs
- HDFS For core-site.xml
- No FileSystem for scheme: hdfs
- 利用神经网络识别手写数字
- C语言打印Log
- SQL Server 跨数据库查询
- iOS类别扩展无法识别添加方法
- 谈谈iOS中的网络
- HDFS For Java
- Vad for Detection
- JDK1.8的安装及配置
- FTPClient ftp.storeFile上传文件时服务器无响应的原因
- ARP知识,如何通过 ARP判断IP冲突
- [微信小程序]控制台弹出 warning: Now you can provide attr "wx:key" for a "wx:for" to improve performance
- VS2012+CMake编译OpenCV3.0笔记
- JavaScript中函数参数的值传递和引用传递
- DataV接入ECharts图表库 可视化利器强强联手