1 查找

列出某个目录下的文件名称,hdfs命令如下所示:
hdfs dfs –ls/usr/app
java代码片段:

         public void list(String srcPath) {Configuration conf = new Configuration();LOG.info("[Defaultfs] :" +conf.get("fs.default.name"));
//                conf.set("hadoop.job.ugi","app,app");   //It is not necessary for the default user.FileSystem fs;try {fs= FileSystem.get(conf);RemoteIterator<LocatedFileStatus>rmIterator = fs.listLocatedStatus(new Path(srcPath));while (rmIterator.hasNext()) {Path path = rmIterator.next().getPath();if(fs.isDirectory(path)){LOG.info("-----------DirectoryName: "+path.getName());}else if(fs.isFile(path)){LOG.info("-----------FileName: "+path.getName());}}}catch (IOException e) {LOG.error("list fileSysetm object stream.:" , e);new RuntimeException(e);}}

输出结果:
2014-03-11 22:38:15,329 INFO  (com.hdfs.client.SyncDFS:48) ------------File Name: README.txt
2014-03-11 22:38:15,331 INFO  (com.hdfs.client.SyncDFS:45) ------------Directory Name: blog_blogpost
2014-03-11 22:38:15,333 INFO  (com.hdfs.client.SyncDFS:45) ------------Directory Name: test
读取文件中的内容,hdfs命令如下:
hdfs dfs –cat /input
java 代码:

        public void readFile(String file){Configurationconf = new Configuration();FileSystemfs;try {fs= FileSystem.get(conf);Pathpath = new Path(file);if(!fs.exists(path)){LOG.warn("file'"+ file+"' doesn't exist!");return ;}FSDataInputStreamin = fs.open(path);Stringfilename = file.substring(file.lastIndexOf('/') + 1, file.length());OutputStreamout = new BufferedOutputStream(new FileOutputStream(new File(filename)));byte[] b = new byte[1024];int numBytes = 0;while ((numBytes = in.read(b)) > 0) {out.write(b,0, numBytes);}in.close();out.close();fs.close();}catch (IOException e) {LOG.error("ifExists fs Exception caught! :" , e);new RuntimeException(e);}}

获取文件的修改时间,java代码:

  /*** Gets the information about the file modifiedtime.* @param source* @throws IOException*/public void getModificationTime(String source) throws IOException{Configurationconf = new Configuration();FileSystemfs = FileSystem.get(conf);PathsrcPath = new Path(source);// Check if the file alreadyexistsif (!(fs.exists(srcPath))) {System.out.println("No such destination " + srcPath);return;}// Get the filename out of thefile pathStringfilename = source.substring(source.lastIndexOf('/') + 1, source.length());FileStatusfileStatus = fs.getFileStatus(srcPath);long modificationTime =fileStatus.getModificationTime();LOG.info("modified datetime: " + System.out.format("File %s; Modification time : %0.2f%n",filename,modificationTime));}

获取文件块定位信息,java代码:

/*** Gets the file block location info* @param source* @throws IOException*/public void getBlockLocations(String source) throws IOException{Configurationconf = new Configuration();FileSystemfs = FileSystem.get(conf);PathsrcPath = new Path(source);// Check if the file alreadyexistsif (!(ifExists(source))) {System.out.println("No such destination " + srcPath);return;}// Get the filename out of thefile pathStringfilename = source.substring(source.lastIndexOf('/') + 1, source.length());FileStatusfileStatus = fs.getFileStatus(srcPath);BlockLocation[]blkLocations = fs.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());int blkCount = blkLocations.length;System.out.println("File :" + filename + "stored at:");for (int i=0; i < blkCount; i++) {String[]hosts = blkLocations[i].getHosts();LOG.info("host ip:" +System.out.format("Host %d: %s %n", i, hosts));}}

获取Hadoop集群中data node的DNS主机名,java代码:

public void getHostnames () throwsIOException{Configurationconfig = new Configuration();FileSystemfs = FileSystem.get(config);DistributedFileSystemhdfs = (DistributedFileSystem) fs;DatanodeInfo[]dataNodeStats = hdfs.getDataNodeStats();String[]names = new String[dataNodeStats.length];for (int i = 0; i < dataNodeStats.length; i++) {names[i]= dataNodeStats[i].getHostName();LOG.info("datenode hostname:"+(dataNodeStats[i].getHostName()));}}

2 创建

创建一个目录,指定具体的文件路径。hdfs命令如下:

hdfs dfs –mkdir/usr/app/tmp

java代码:

  public void mkdir(String dir){Configurationconf = new Configuration();FileSystemfs = null;try {fs= FileSystem.get(conf);Pathpath = new Path(dir);if(!fs.exists(path)){fs.mkdirs(path);LOG.debug("create directory '"+dir+"' successfully!");}else{LOG.debug("directory '"+dir+"' exits!");}}catch (IOException e) {LOG.error("FileSystem get configuration with anerror");e.printStackTrace();}finally{if(fs!= null){try {fs.close();}catch (IOException e) {LOG.error("close fs object stream. :" , e);new RuntimeException(e);}}}}

将本地文件上传到hdfs上去,java代码如下:

public void copyFromLocal (String source, String dest) {Configurationconf = new Configuration();FileSystemfs;try {fs= FileSystem.get(conf);PathsrcPath = new Path(source);PathdstPath = new Path(dest);// Check if the file alreadyexistsif (!(fs.exists(dstPath))) {LOG.warn("dstPathpath doesn't exist" );LOG.error("No such destination " + dstPath);return;}// Get the filename out of thefile pathStringfilename = source.substring(source.lastIndexOf('/') + 1, source.length());try{//if the file exists in thedestination path, it will throw exception.
//                                   fs.copyFromLocalFile(srcPath,dstPath);//remove and overwrite files withthe method//copyFromLocalFile(booleandelSrc, boolean overwrite, Path src, Path dst)fs.copyFromLocalFile(false, true, srcPath, dstPath);LOG.info("File " + filename + "copied to " + dest);}catch(Exception e){LOG.error("copyFromLocalFile exception caught!:" , e);new RuntimeException(e);}finally{fs.close();}}catch (IOException e1) {LOG.error("copyFromLocal IOException objectstream. :" ,e1);new RuntimeException(e1);}}

添加一个文件到指定的目录下,java代码如下:

   public void addFile(String source, String dest)  {// Conf object will readthe HDFS configuration parametersConfigurationconf = new Configuration();FileSystemfs;try {fs= FileSystem.get(conf);// Get the filename out of thefile pathStringfilename = source.substring(source.lastIndexOf('/') + 1, source.length());// Create the destination pathincluding the filename.if (dest.charAt(dest.length() - 1) != '/') {dest= dest + "/" + filename;}else {dest= dest + filename;}// Check if the file alreadyexistsPathpath = new Path(dest);if (fs.exists(path)) {LOG.error("File " + dest + " already exists");return;}// Create a new file and writedata to it.FSDataOutputStreamout = fs.create(path);InputStreamin = new BufferedInputStream(new FileInputStream(new File(source)));byte[] b = new byte[1024];int numBytes = 0;//In this way read and write datato destination file.while ((numBytes = in.read(b)) > 0) {out.write(b,0, numBytes);}in.close();out.close();fs.close();}catch (IOException e) {LOG.error("addFile Exception caught! :" , e);new RuntimeException(e);}}

3 修改

重新命名hdfs中的文件名称,java代码如下:

     public void renameFile (String fromthis, String tothis){Configurationconf = new Configuration();FileSystemfs;try {fs= FileSystem.get(conf);PathfromPath = new Path(fromthis);PathtoPath = new Path(tothis);if (!(fs.exists(fromPath))) {LOG.info("No such destination " + fromPath);return;}if (fs.exists(toPath)) {LOG.info("Already exists! " + toPath);return;}try{boolean isRenamed = fs.rename(fromPath,toPath);     //renames file name indeed.if(isRenamed){LOG.info("Renamed from " + fromthis + " to " + tothis);}}catch(Exception e){LOG.error("renameFile Exception caught! :" , e);new RuntimeException(e);}finally{fs.close();}}catch (IOException e1) {LOG.error("fs Exception caught! :" , e1);new RuntimeException(e1);}}

 

4 删除

在hdfs上,删除指定的一个文件。Java代码:

public void deleteFile(String file)  {Configurationconf = new Configuration();FileSystemfs;try {fs= FileSystem.get(conf);Pathpath = new Path(file);if (!fs.exists(path)) {LOG.info("File " + file + " does not exists");return;}/** recursively delete the file(s) if it is adirectory.* If you want to mark the path that will bedeleted as* a result of closing the FileSystem.*  deleteOnExit(Path f)*/fs.delete(new Path(file), true);fs.close();}catch (IOException e) {LOG.error("deleteFile Exception caught! :" , e);new RuntimeException(e);}}

Appendix 完整代码

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
importorg.apache.hadoop.hdfs.DistributedFileSystem;
import org.apache.hadoop.hdfs.protocol.DatanodeInfo;public class SyncDFS {private static final Log LOG = LogFactory.getLog(SyncDFS.class);/*** Reads the directory name(s) and file name(s)from the specified parameter "srcPath"* @param srcPath*/public void list(String srcPath) {Configuration conf = new Configuration();LOG.info("[Defaultfs] :" +conf.get("fs.default.name"));
//                conf.set("hadoop.job.ugi","app,app");   //It is not necessary for the default user.FileSystem fs;try {fs= FileSystem.get(conf);RemoteIterator<LocatedFileStatus>rmIterator = fs.listLocatedStatus(new Path(srcPath));while (rmIterator.hasNext()) {Path path = rmIterator.next().getPath();if(fs.isDirectory(path)){LOG.info("-----------DirectoryName: "+path.getName());}else if(fs.isFile(path)){LOG.info("-----------FileName: "+path.getName());}}}catch (IOException e) {LOG.error("list fileSysetm object stream.:" , e);new RuntimeException(e);}}/*** Makes the specified directory if it doesn'texist.* @param dir*/public void mkdir(String dir){Configurationconf = new Configuration();FileSystemfs = null;try {fs= FileSystem.get(conf);Pathpath = new Path(dir);if(!fs.exists(path)){fs.mkdirs(path);LOG.debug("create directory '"+dir+"' successfully!");}else{LOG.debug("directory '"+dir+"' exits!");}}catch (IOException e) {LOG.error("FileSystem get configuration with anerror");e.printStackTrace();}finally{if(fs!= null){try {fs.close();}catch (IOException e) {LOG.error("close fs object stream. :" , e);new RuntimeException(e);}}}}/*** Reads the file content in console.* @param file*/public void readFile(String file){Configurationconf = new Configuration();FileSystemfs;try {fs= FileSystem.get(conf);Pathpath = new Path(file);if(!fs.exists(path)){LOG.warn("file'"+ file+"' doesn't exist!");return ;}FSDataInputStreamin = fs.open(path);Stringfilename = file.substring(file.lastIndexOf('/') + 1, file.length());OutputStreamout = new BufferedOutputStream(new FileOutputStream(new File(filename)));byte[] b = new byte[1024];int numBytes = 0;while ((numBytes = in.read(b)) > 0) {out.write(b,0, numBytes);}in.close();out.close();fs.close();}catch (IOException e) {LOG.error("ifExists fs Exception caught! :" , e);new RuntimeException(e);}}public boolean ifExists(String source){if(source == null || source.length() ==0){return false;}Configurationconf = new Configuration();FileSystemfs = null;try {fs= FileSystem.get(conf);LOG.debug("judge file '"+source +  "'");return fs.exists(new Path(source));}catch (IOException e) {LOG.error("ifExists fs Exception caught! :" , e);new RuntimeException(e);return false;}finally{if(fs != null){try {fs.close();}catch (IOException e) {LOG.error("fs.close Exception caught! :" , e);new RuntimeException(e);}}}}/*** Recursively copies the source pathdirectories or files to the destination path of DFS.* It is the same functionality as thefollowing comand:*      hadoopfs -copyFromLocal <local fs><hadoop fs>* @param source* @param dest*/public void copyFromLocal (String source, String dest) {Configurationconf = new Configuration();FileSystemfs;try {fs= FileSystem.get(conf);PathsrcPath = new Path(source);PathdstPath = new Path(dest);// Check if the file alreadyexistsif (!(fs.exists(dstPath))) {LOG.warn("dstPathpath doesn't exist" );LOG.error("No such destination " + dstPath);return;}// Get the filename out of thefile pathStringfilename = source.substring(source.lastIndexOf('/') + 1, source.length());try{//if the file exists in thedestination path, it will throw exception.
//                                   fs.copyFromLocalFile(srcPath,dstPath);//remove and overwrite files withthe method//copyFromLocalFile(booleandelSrc, boolean overwrite, Path src, Path dst)fs.copyFromLocalFile(false, true, srcPath, dstPath);LOG.info("File " + filename + "copied to " + dest);}catch(Exception e){LOG.error("copyFromLocalFile exception caught!:" , e);new RuntimeException(e);}finally{fs.close();}}catch (IOException e1) {LOG.error("copyFromLocal IOException objectstream. :" ,e1);new RuntimeException(e1);}}public void renameFile (String fromthis, String tothis){Configurationconf = new Configuration();FileSystemfs;try {fs= FileSystem.get(conf);PathfromPath = new Path(fromthis);PathtoPath = new Path(tothis);if (!(fs.exists(fromPath))) {LOG.info("No such destination " + fromPath);return;}if (fs.exists(toPath)) {LOG.info("Already exists! " + toPath);return;}try{boolean isRenamed = fs.rename(fromPath,toPath);     //renames file name indeed.if(isRenamed){LOG.info("Renamed from " + fromthis + " to " + tothis);}}catch(Exception e){LOG.error("renameFile Exception caught! :" , e);new RuntimeException(e);}finally{fs.close();}}catch (IOException e1) {LOG.error("fs Exception caught! :" , e1);new RuntimeException(e1);}}/*** Uploads or adds a file to HDFS* @param source* @param dest*/public void addFile(String source, String dest)  {// Conf object will readthe HDFS configuration parametersConfigurationconf = new Configuration();FileSystemfs;try {fs= FileSystem.get(conf);// Get the filename out of thefile pathStringfilename = source.substring(source.lastIndexOf('/') + 1, source.length());// Create the destination pathincluding the filename.if (dest.charAt(dest.length() - 1) != '/') {dest= dest + "/" + filename;}else {dest= dest + filename;}// Check if the file alreadyexistsPathpath = new Path(dest);if (fs.exists(path)) {LOG.error("File " + dest + " already exists");return;}// Create a new file and writedata to it.FSDataOutputStreamout = fs.create(path);InputStreamin = new BufferedInputStream(new FileInputStream(new File(source)));byte[] b = new byte[1024];int numBytes = 0;//In this way read and write datato destination file.while ((numBytes = in.read(b)) > 0) {out.write(b,0, numBytes);}in.close();out.close();fs.close();}catch (IOException e) {LOG.error("addFile Exception caught! :" , e);new RuntimeException(e);}}/***Deletes the files if it is a directory.* @param file*/public void deleteFile(String file)  {Configurationconf = new Configuration();FileSystemfs;try {fs= FileSystem.get(conf);Pathpath = new Path(file);if (!fs.exists(path)) {LOG.info("File " + file + " does not exists");return;}/** recursively delete the file(s) if it is adirectory.* If you want to mark the path that will bedeleted as* a result of closing the FileSystem.*  deleteOnExit(Path f)*/fs.delete(new Path(file), true);fs.close();}catch (IOException e) {LOG.error("deleteFile Exception caught! :" , e);new RuntimeException(e);}}/*** Gets the information about the file modifiedtime.* @param source* @throws IOException*/public void getModificationTime(String source) throws IOException{Configurationconf = new Configuration();FileSystemfs = FileSystem.get(conf);PathsrcPath = new Path(source);// Check if the file alreadyexistsif (!(fs.exists(srcPath))) {System.out.println("No such destination " + srcPath);return;}// Get the filename out of thefile pathStringfilename = source.substring(source.lastIndexOf('/') + 1, source.length());FileStatusfileStatus = fs.getFileStatus(srcPath);long modificationTime =fileStatus.getModificationTime();LOG.info("modified datetime: " + System.out.format("File %s; Modification time : %0.2f%n",filename,modificationTime));}/*** Gets the file block location info* @param source* @throws IOException*/public void getBlockLocations(String source) throws IOException{Configurationconf = new Configuration();FileSystemfs = FileSystem.get(conf);PathsrcPath = new Path(source);// Check if the file alreadyexistsif (!(ifExists(source))) {System.out.println("No such destination " + srcPath);return;}// Get the filename out of thefile pathStringfilename = source.substring(source.lastIndexOf('/') + 1, source.length());FileStatusfileStatus = fs.getFileStatus(srcPath);BlockLocation[]blkLocations = fs.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());int blkCount = blkLocations.length;System.out.println("File :" + filename + "stored at:");for (int i=0; i < blkCount; i++) {String[]hosts = blkLocations[i].getHosts();LOG.info("host ip:" +System.out.format("Host %d: %s %n", i, hosts));}}public void getHostnames () throws IOException{Configurationconfig = new Configuration();FileSystemfs = FileSystem.get(config);DistributedFileSystemhdfs = (DistributedFileSystem) fs;DatanodeInfo[]dataNodeStats = hdfs.getDataNodeStats();String[]names = new String[dataNodeStats.length];for (int i = 0; i < dataNodeStats.length; i++) {names[i]= dataNodeStats[i].getHostName();LOG.info("datenode hostname:"+(dataNodeStats[i].getHostName()));}}/*** @param args*/public static void main(String[] args) {SyncDFSdfs = new SyncDFS();dfs.list("/user/app");dfs.mkdir("/user/app");//                dfs.readFile("/user/app/README.txt");LOG.info("--------------" +dfs.ifExists("/user/warehouse/hbase.db/u_data/u.data")); //falseLOG.info("--------------" + dfs.ifExists("/user/app/README.txt")); //true//copied the local file(s) to thedfs.
//                dfs.copyFromLocal("/opt/test","/user/app");//delete the file(s) from the dfs
//                dfs.deleteFile("/user/app/test");//rename diretory in dfs
//                dfs.renameFile("/user/app/test","/user/app/log");//rename file in dfs
//                dfs.renameFile("/user/app/log/derby.log","/user/app/log/derby_info.log");}}

实现对HDFS增删改查CRUD等操作相关推荐

  1. java实现对HDFS增删改查(CRUD)等操作

    实现对HDFS增删改查CRUD等操作 1 查找 列出某个目录下的文件名称,hdfs命令如下所示: hdfs dfs –ls/usr/app java代码片段: [plain] view plain c ...

  2. 实现对mysql增删改查_Java语言实现对MySql数据库中数据的增删改查操作的代码

    简单说操作的步骤: 1.连接数据库 2.将SQL语句发送到数据库 3.执行SQL语句 这里举个例子: 在一个数据库中有个students表,表中有学号(Id),姓名(Name),性别(Sex),地址( ...

  3. Android前端通过Http协议与J2EE后端数据交互。工具eclipse、MySQL、Tomcat。通过JoSn获取数据。Android端实现对MySQL增删改查功能。

    Android:目录 HttpThread.java package com.example.saads; import java.io.BufferedReader; import java.io. ...

  4. php xml 增删改查,PHP实现对xml进行简单的增删改查(CRUD)操作示例

    本文实例讲述了PHP实现对xml进行简单的增删改查(CRUD)操作.分享给大家供大家参考,具体如下: 假如有下面xml文件: 55.8 56 40 339 如何使用php对它进行CRUD?其实像这种简 ...

  5. 一步步实现:JPA的基本增删改查CRUD(jpa基于hibernate)

    1.创建一个JPA工程 首先,创建一个JPA工程(若不知道JPA创建或出现at least one user library must be selected等错误,请参考http://blog.cs ...

  6. Spring Data JDBC自动生成的增删改查CRUD分页、排序SQL语句非常简洁没有多余的SQL

    通过在application.properties文件中设置记录SQL日志 logging.level.org.springframework.jdbc.core.JdbcTemplate=DEBUG ...

  7. mysql如何修改学生表_MySQL 详细单表增删改查crud语句

    MySQL 增删改查语句 1.创建练习表 这里练习表没有满足三范式 第一范式(又称 1NF):保证每列的原子性 数据表中的每一列(字段),必须是不可拆分的最小单元,也就是确保每一列的原子性.满足第一范 ...

  8. python最强实训程序(增删改查)机房收费管理系统-基于tkinter的图形化界面(附详细代码)

    python最强实训程序(增删改查)机房收费管理系统-基于tkinter的图形化界面(附详细代码) 最近学校实训,用两天时间做了一个python小程序*机房收费管理系统*,一款基于tkinter使用p ...

  9. MyBatisPlus03_MyBatisPlus的增删改查CRUD

    MyBatisPlus03_MyBatisPlus的增删改查CRUD 查询 无条件查询 List<User> list = mapper.selectList(null); eq查询 Qu ...

最新文章

  1. dev c++与VC assist的杂记
  2. sails框架发送邮件
  3. python开发微信小程序-Python 开发者的微信小程序开发实践
  4. TF使用例子-LSTM实现序列标注
  5. 【详解】消息队列和线程关系
  6. 漫画:唐玄奘教你横扫 AVL 树面试题无敌手!
  7. html选择器 并列,CSS 中的选择器 (二)- 组合选择器
  8. iClip mac如何自定义声音?iClip剪切板管理软件更改声音的方法
  9. Centos7搭建pptp一键安装脚本
  10. 2022哈工程计算机考研经验贴
  11. android 酷我音乐接口,酷我音乐 各种付费歌曲,音质包括ape、flac无损音乐api接口...
  12. Apk资源文件混淆[微信开源方法]
  13. 泡泡龙游戏开发系列教程(一)
  14. 3DTools TrackballDecorator实现3D漫游
  15. 跨境电商新手如何建立正确的运营思路
  16. 硬件参数 调整 麦克风MIC灵敏度 原理
  17. 第 20 章 Document Tools
  18. 数据结构上机实验6.29
  19. 直方图均衡化、自适应直方图均衡化
  20. Codeforces 1065 简要题解

热门文章

  1. 【Android 逆向】Android 逆向通用工具开发 ( Android 逆向通用工具组成部分 | 各模块间的关联 )
  2. 【Android 插件化】Hook 插件化框架 ( Hook Activity 启动流程 | Hook 点分析 )
  3. 【嵌入式开发】gcc 学习笔记(一) - 编译C程序 及 编译过程
  4. java BASE64流 输出图片。
  5. [转载] 数据库分析手记 —— InnoDB锁机制分析
  6. 模式识别 - 处理多演示样例学习(MIL)特征(matlab)
  7. linux下的共享库(动态库)和静态库
  8. HDU-2086 A1 = ?
  9. lz0-007 读书笔记09
  10. glutSwapBuffers函数用法