hdfs 例子
package hadoop; import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
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; public class HdfsClient { public static void main(String[] args) throws IOException, InterruptedException, URISyntaxException {
FileSystem fs = null;
Configuration conf = new Configuration();
fs = FileSystem.get(new URI("hdfs://192.168.1.231:9000"), conf, "root"); //上传
//fs.copyFromLocalFile( new Path("F:/新建文本文档.txt") , new Path("/") ); //下载
fs.copyToLocalFile( new Path( "/新建文本文档.txt" ), new Path( "F:/aaa" )); // 创建目录
fs.mkdirs(new Path("/a1/b1/c1")); // 删除文件夹 ,如果是非空文件夹,参数2必须给值true
fs.delete(new Path("/aaa"), true); // 重命名文件或文件夹
fs.rename(new Path("/a1"), new Path("/a2")); RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true); while (listFiles.hasNext()) {
LocatedFileStatus fileStatus = listFiles.next(); System.out.println(fileStatus.getPath().getName());
System.out.println(fileStatus.getBlockSize());
System.out.println(fileStatus.getPermission());
System.out.println(fileStatus.getLen());
BlockLocation[] blockLocations = fileStatus.getBlockLocations();
for (BlockLocation bl : blockLocations) {
System.out.println("block-length:" + bl.getLength() + "--" + "block-offset:" + bl.getOffset());
String[] hosts = bl.getHosts();
for (String host : hosts) {
System.out.println(host);
}
}
System.out.println("--------------分割线--------------");
} FileStatus[] listStatus = fs.listStatus(new Path("/"));
String flag = "d-- "; for (FileStatus fstatus : listStatus) {
if (fstatus.isFile())
flag = "f-- ";
System.out.println(flag + fstatus.getPath().getName());
} fs.close();
} }
依赖:
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.8.5</version>
</dependency>
hdfs 例子的更多相关文章
- HDFS handler
http://docs.oracle.com/goldengate/bd1221/gg-bd/GADBD/GUID-85A82B2E-CD51-463A-8674-3D686C3C0EC0.htm#G ...
- HBase的基本架构及其原理介绍
1.概述:最近,有一些工程师问我有关HBase的基本架构的问题,其实这个问题仅仅说架构是非常简单,但是需要理解.在这里,我觉得可以用HDFS的架构作为借鉴.(其实像Hadoop生态系统中的大部分组建的 ...
- 文件查找find命令
find命令总结: 常用选项: -name 查找/etc目录下以conf结尾的文件 find /etc -name '*conf' -iname 查找当前目录下文件名为aa的文件,不区分大小写 fin ...
- shell编程系列7--shell中常用的工具find、locate、which、whereis
shell编程系列7--shell中常用的工具find.locate.which.whereis .文件查找之find命令 语法格式:find [路径] [选项] [操作] 选项 -name 根据文件 ...
- [Flume]使用 Flume 来传递web log 到 hdfs 的例子
[Flume]使用 Flume 来传递web log 到 hdfs 的例子: 在 hdfs 上创建存储 log 的目录: $ hdfs dfs -mkdir -p /test001/weblogsfl ...
- Hadoop MapReduce执行过程详解(带hadoop例子)
https://my.oschina.net/itblog/blog/275294 摘要: 本文通过一个例子,详细介绍Hadoop 的 MapReduce过程. 分析MapReduce执行过程 Map ...
- 何为HDFS?
该文来自百度百科,自我收藏. Hadoop分布式文件系统(HDFS)被设计成适合运行在通用硬件(commodity hardware)上的分布式文件系统.它和现有的分布式文件系统有很多共同点.但同时, ...
- [转]HDFS中JAVA API的使用
HDFS是一个分布式文件系统,既然是文件系统,就可以对其文件进行操作,比如说新建文件.删除文件.读取文件内容等操作.下面记录一下使用JAVA API对HDFS中的文件进行操作的过程. 对分HDFS中的 ...
- 【从零开始学习Hadoop】--2.HDFS分布式文件系统
1. 文件系统从头说2. Hadoop的文件系统3. 如何将文件复制到HDFS3.1 目录和文件结构3.2 FileCopy.java文件的源代码3.3 编译3.4打包3.5 运行3.6 检查结果 1 ...
随机推荐
- mod_fcgid: HTTP request length 136136 (so far) exceeds MaxRequestLen (131072)
原来是fastcgi模式下的设置问题,需要在配置文件.htaccess或者直接在apache的配置文件http.conf 中指明,如下: 查看官方说明有这么一句:Default: FcgidMaxRe ...
- python scrapy 数据处理时间格式转换
def show(self,response): # print(response.url) title = response.xpath('//main/div/div/div/div/h1/tex ...
- Python 时间
import time # 时间戳: 从1970-01-01 00:00:00开始计算. 未来存储的时候用是时间戳 print(time.time()) # 格式化时间 print(time.strf ...
- 2017ICPC北京赛区网络赛 Visiting Peking University(简单思维)
描述 Ming is going to travel for n days and the date of these days can be represented by n integers: 0 ...
- Django + nginx + uswgi 的部署总结
一.引言 自己小组内写了一个网站,需要部署到远程服务器,搜索了好多资料,但是大部分资料都比较繁琐,并且没有一个教程能够直接从头到尾适合,在部署过程中,我是按照很多教程然后综合试验着逐渐部署成功,其中有 ...
- Redis过期策略(转)
1.设置过期时间 expire key time(以秒为单位)--这是最常用的方式 setex(String key, int seconds, String value)--字符串独有的方式 具体的 ...
- Ubuntu Core 网络配置
/********************************************************************************* * Ubuntu Core 网络配 ...
- 【opencv基础】图像的几何变换
参考 1. 图像的几何变换-平移和镜像: 2.图像的几何变换-缩放和旋转: 3. opencv图像旋转实现: 完
- Python之路,第十一篇:Python入门与基础11
python3 函数2 全局变量:一直存在 局部变量:函数执行时存在,执行完毕后销毁: lambda 表达式(又称匿名函数表达式) 作用: 创建一个匿名(无名)函数对象, 同 def 类似但不提供函 ...
- 带列表写入文件出错先 json.dumps
output = json.dumps(output, ensure_ascii=False).encode('utf-8')