1.上传本地文件到HDFS

//上传本地文件到HDFS
public class CopyFile {
public static void main(String[] args) {
try {
Configuration conf = new Configuration();
String str_src = "/usr/local/myjar/mongo/地图数据/Zhengye_Drive_Testing_Data/solu"
+ "/solu_Yanming_DriveTesting_09-04.16-17.16-27_True_TA.json";
String str_dst = "hdfs://node4:9000/user/hadoop/TestFile.json"; Path src = new Path(str_src); //本地地址
Path dst = new Path(str_dst); //hdfs地址 FileSystem hdfs = dst.getFileSystem(conf);
//FileSystem hdfs = FileSystem.get(URI.create(str_dst),conf); //这样也可以
//伪分布式上面两种都可以,如果直接FileSystem.get(conf),可能出现错误 hdfs.copyFromLocalFile(src, dst);
System.out.println("Upload to "+conf.get("fs.default.name")); FileStatus files[] = hdfs.listStatus(dst);
for(FileStatus file:files){
System.out.println(file.getPath());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

可能出现的错误 Wrong FS解决方法:
http://blog.csdn.net/kurama_sai/article/details/8604640
http://blog.itpub.net/22846396/viewspace-1119945

2. 在hdfs中创建文件,并写入一行文字

//创建文件,并向文件中写入一行文字
public class CreateFile {
public static void main(String[] args) {
try {
Configuration conf = new Configuration();
byte[] buff = "This is a test line.".getBytes();
String dsf = "hdfs://node4:9000/user/hadoop/Test";
Path pathdsf = new Path(dsf);
FileSystem hdfs = pathdsf.getFileSystem(conf);
FSDataOutputStream outputStream = hdfs.create(pathdsf);
outputStream.write(buff,0,buff.length);
System.out.println("Finish write!");
} catch (IOException e) {
e.printStackTrace();
}
}
}

3.删除文件

Configuration conf = new Configuration();
Path path_del = new Path("hdfs://node4:9000/user/hadoop/Test2");
FileSystem hdfs = path_del.getFileSystem(conf);
boolean isDeleted = hdfs.delete(path_del,false);
//hdfs.delete(path_del,true); //递归删除,如果path_del是一个文件夹,将文件夹以及下面的子文件全删除
System.out.println("delete? " +isDeleted);

4.重命名文件

Configuration conf = new Configuration();
Path path_fr = new Path("hdfs://node4:9000/user/hadoop/Test");
Path path_to = new Path("hdfs://node4:9000/user/hadoop/Test2");
FileSystem hdfs = path_fr.getFileSystem(conf);
boolean isRename = hdfs.rename(path_fr, path_to); //对文件进行重命名
System.out.println("is rename? "+isRename);

5.查看文件以及文件系统的各项信息

Configuration conf = new Configuration();
Path findf = new Path("hdfs://node4:9000/user/hadoop/hadoop.txt");
FileSystem hdfs = findf.getFileSystem(conf); //查看某个HDFS文件是否存在
boolean isExists = hdfs.exists(findf); //查看文件或文件夹是否存在
System.out.println("exists? " + isExists); //查看HDFS文件的属性
FileStatus filestatus = hdfs.getFileStatus(findf);
long modificationTime = filestatus.getModificationTime(); //最后修改时间
System.out.println("Modification time is: "+modificationTime);
long blocksize = filestatus.getBlockSize(); //块大小
System.out.println("Block size is: "+blocksize); //查看某个文件在HDFS集群的位置
BlockLocation[] blkLocations = hdfs.getFileBlockLocations(filestatus, 0, filestatus.getLen());
int blockLen = blkLocations.length;
for(int i = 0 ; i < blockLen ; i++){
String[] hosts = blkLocations[i].getHosts();
System.out.println("block "+i+" location: "+hosts[i]);
} //查看hdfs文件系统的的各项信息
System.out.println("scheme: "+hdfs.getScheme());
System.out.println("used: "+hdfs.getUsed());
System.out.println("canonical service name: "+hdfs.getCanonicalServiceName());
System.out.println("default block size: "+hdfs.getDefaultBlockSize(findf));

输出结果:

exists? true
Modification time is: 1430225267896
Block size is: 134217728
block 0 location: node4
scheme: hdfs
used: 0
canonical service name: 192.168.1.160:9000
default block size: 134217728

6.读取HDFS中的文件内容

下面代码的效果就是Test文件的内容输出

String dsf = "hdfs://node4:9000/user/hadoop/Test";
Configuration conf = new Configuration(); Path pathdsf = new Path(dsf); FileSystem fs = FileSystem.get(URI.create(dsf), conf);
//FileSystem fs = pathdsf.getFileSystem(conf); //这样也可以 FSDataInputStream hdfsInStream = fs.open(pathdsf); byte[] ioBuffer = new byte[1024];
int readLen = hdfsInStream.read(ioBuffer);
while (readLen != -1) {
System.out.write(ioBuffer, 0, readLen);
readLen = hdfsInStream.read(ioBuffer);
}
hdfsInStream.close();
fs.close();

7.获取集群上所有节点的名称

Configuration conf = new Configuration();
Path path = new Path("hdfs://node4:9000/user/hadoop");
FileSystem fs = path.getFileSystem(conf);
DistributedFileSystem dfs = (DistributedFileSystem) fs;
DatanodeInfo[] dataNodeStats = dfs.getDataNodeStats(); String[] names = new String[dataNodeStats.length];
for(int i = 0 ; i < dataNodeStats.length ; i++){
names[i] = dataNodeStats[i].getHostName();
System.out.println("no."+i+", name:"+names[i]);
}

输出的就是节点名称
no.0, name:node4
no.1, name:node3

HDFS操作--文件上传/创建/删除/查询文件信息的更多相关文章

  1. HTTP文件上传服务器-支持超大文件HTTP断点续传的实现办法

    最近由于笔者所在的研发集团产品需要,需要支持高性能的大文件http上传,并且要求支持http断点续传.笔者在以前的博客如何实现支持大文件的高性能HTTP文件上传服务器已经介绍了实现大文件上传的一些基本 ...

  2. PHP实现单文件、多文件上传 封装 面向对象实现文件上传

    文件上传配置 客户端配置 1.表单页面 2.表单的发送方式为post 3.添加enctype = "multipart/form-data" <form action=&qu ...

  3. SpringBoot - 实现文件上传2(多文件上传、常用上传参数配置)

    在前文中我介绍了 Spring Boot 项目如何实现单文件上传,而多文件上传逻辑和单文件上传基本一致,下面通过样例进行演示. 多文件上传 1,代码编写 1)首先在 static 目录中创建一个 up ...

  4. php 文件上传后缀名与文件类型对照表(几乎涵盖所有文件)

    网上有很多php文件上传的类,文件上传处理是php的一个特色(至少手册上是将此作为php特点来展示的,个人认为php在数组方面的优异功能更有特 色),学php的人都知道文件上传怎么做,但很多人在编程中 ...

  5. django设置并获取cookie/session,文件上传,ajax接收文件,post/get请求及跨域请求等的方法

    django设置并获取cookie/session,文件上传,ajax接收文件等的方法: views.py文件: from django.shortcuts import render,HttpRes ...

  6. SpringMVC ajax技术无刷新文件上传下载删除示例

    参考 Spring MVC中上传文件实例 SpringMVC结合ajaxfileupload.js实现ajax无刷新文件上传 Spring MVC 文件上传下载 (FileOperateUtil.ja ...

  7. Struts2 文件上传,下载,删除

    本文介绍了: 1.基于表单的文件上传 2.Struts 2 的文件下载 3.Struts2.文件上传 4.使用FileInputStream FileOutputStream文件流来上传 5.使用Fi ...

  8. c# txt 文件上传、写入TXT文件、创建图形验证码

    asp.net mvc 图片上传 html 在使用包含文件上传控件的表单时,必须使用 enctype="multipart/form-data" 属性 <form encty ...

  9. 利用Rsync同步工具上传、删除目标文件

    Rsync是文件备份工具,当然也可以当做传输工具,管理远程服务器的文件 上传 rsync -avzP --progress --port 9106 /path/.../指定文件 root@192.16 ...

随机推荐

  1. Cracking the coding interview--Q2.1

    原文: Write code to remove duplicates from an unsorted linked list.FOLLOW UPHow would you solve this p ...

  2. PCB Layout爬电距离、电气间隙的确定

    爬电距离的确定:首先需要确定绝缘的种类:基本绝缘:一次电路与保护地工作绝缘 ① :一次电路内部:二次电路内部工作绝缘 ② :输入部分(输入继电器之前)内部,二次电路与保护地加强绝缘:一次电路与二次电路 ...

  3. android实现类似于支付宝余额快速闪动的效果

    效果如下:   此图片不会动,但实际上是会快速跳动的.  之前看到有支付宝的效果非常牛逼.就是进去看到余额呼噜噜的直接上蹿下跳到具体数字,效果帅,但不知道怎么实现,最近终于知道了. 思路: 首先经常用 ...

  4. NERDTree这个插件的用法简介

    事情是这样子的,想做做李治军老师班的操作系统实验,但是Linux上的gedit太简陋了(这个简陋程度堪比Windows环境下的"记事本"),被杨世祺大神嘲笑了.我心想既然在linu ...

  5. EA强大的画图工具---设计数据库表格

    http://blog.csdn.net/senior_lee/article/details/30272169?utm_source=tuicool 关于EA这个优秀的软件是从师哥哪里听来的,自己瞎 ...

  6. [LeetCode] 134. Gas Station 解题思路

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  7. [LeetCode] 35. Search Insert Position 解决思路

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  8. 求一个数组中第K小的数

    面试南大夏令营的同学说被问到了这个问题,我的第一反应是建小顶堆,但是据他说用的是快排的方法说是O(n)的时间复杂度, 但是后来经过我的考证,这个算法在最坏的情况下是O(n^2)的,但是使用堆在一般情况 ...

  9. JavaScript新手学习笔记4——我记不住的几个坑:短路逻辑、按值传递、声明提前

    1.短路逻辑 逻辑运算中,如果前一个条件已经可以得出最终结论,则后续所有条件不再执行!这里的逻辑运算指的是逻辑与和逻辑或. 我们要理解逻辑与是两个条件都为真的时候,才为真,如果第一个就是假的,那么后面 ...

  10. zeptoJS:如何像jQuery一样,让滚动变得优雅?

    利用jQuery的animate() 方法,我们很容易实现滚动条的平滑滚动效果: $(function() { $('#top').click( function (e) { $('html, bod ...