import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
 
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
 
class HelloWord {
public static void main(String[] args){
 
 
Hdfs();
 
}
 
 
//方法1:
public static void Hdfs(){
try{
Configuration conf = new Configuration();
conf.set("fs.defaultFS","hdfs://10.8.6.126:8020");
FileSystem fs = null;
//fs = FileSystem.get(new URI("hdfs://10.8.6.126:8020"),conf); //这两种方式都可以配置hdfs ip
fs = FileSystem.get(conf);
 
RemoteIterator<LocatedFileStatus> lt = fs.listFiles(new Path("hdfs://10.8.6.126:8020/ada/lyy/App"), true);
while (lt.hasNext()) {
LocatedFileStatus file = lt.next();
if(file.isFile())
{
Path path = file.getPath();
System.out.println("文件:["+path.toString()+"]");
System.out.println("文件名:["+path.getName.toString()+"]"); //只是文件名,没有路径信息
}else{
Path path = file.getPath();
System.out.println("目录:["+path.toString()+"]");
}
}
 
}
catch( IOException e){
System.out.println(e.getStackTrace());
}
}
 
}
 
//方法2:
public static void Hdfs(){
try{
Configuration conf = new Configuration();
conf.set("fs.defaultFS","hdfs://10.8.6.126:8020");
FileSystem fs = null;
//fs = FileSystem.get(new URI("hdfs://10.8.6.126:8020"),conf); //这两种方式都可以配置hdfs ip
fs = FileSystem.get(conf);
 
Path path = new Path("/shell");
//通过fs的listStatus方法获取一个指定path的所有文件信息(status),因此我们需要传入一个hdfs的路径,返回的是一个filStatus数组
FileStatus[] fileStatuses = fs.listStatus(path);
for (FileStatus fileStatus : fileStatuses) {
//判断当前迭代对象是否是目录
boolean isDir = fileStatus.isDirectory();
//获取当前文件的绝对路径
String fullPath = fileStatus.getPath().toString();
System.out.println("isDir:" + isDir + ",Path:" + fullPath);
}
}
 
 
 
同时读取本地和hdfs目录:
 
public class TestHdfs{
 
public static void main(String[] args) {
 
 
//读取配置文件
Configuration conf=new Configuration();
//String path = "hdfs://10.8.6.126:8020/ada/lyy/data/NaiveBayesModel.model";
String path = "file:///home/liyanyan/cluster/NaiveBayesModel.model";
 
 
 
String classfile = "";
 
//读取配置文件
FileStatus[] listFile = null;
FileSystem fs = null;
try {
if(path.startsWith("hdfs:")){
conf.set("fs.defaultFS",path.substring(0,path.indexOf('/', path.indexOf(':') + 3)));
fs = FileSystem.get(conf);
 
}else if(path.startsWith("file:")){
fs=FileSystem.getLocal(conf);
//获取文件目录
}
 
listFile =fs.listStatus(new Path(path+"/metadata"), new RegxRejectPathFilter("^[._]+\\w+.*$"));
 
if(listFile.length != 1){
}else{
classfile = listFile[0].getPath().toString();
}
 
}catch(IOException e){
e.printStackTrace();
}
 
System.out.println("classfile = "+classfile.replaceFirst("file:","file://"));
 
}
 
private static class RegxRejectPathFilter implements PathFilter {
private final String regex;
public RegxRejectPathFilter(String regex) {
this.regex=regex;
}
 
@Override
public boolean accept(Path path) {
// TODO 自动生成的方法存根
boolean flag=path.getName().toString().matches(regex);
return !flag;
}
}
}
 

hdfs遍历文件方法的更多相关文章

  1. 一句python,一句R︱模块导入与查看、数据读写出入、数据查看函数、数据类型、遍历文件

    先学了R,最近刚刚上手python,所以想着将python和R结合起来互相对比来更好理解python.最好就是一句python,对应写一句R. pandas中有类似R中的read.table的功能,而 ...

  2. python os.walk()遍历文件夹

    转自 http://alanland.iteye.com/blog/612459 via @alanland 今天第一次进行 文件遍历,自己递归写的时候还调试了好久,(主要因为分隔符号的问题),后来发 ...

  3. python中遍历文件的3个方法

    转自: http://www.jb51.net/article/54640.htm 用python进行文件遍历有多种方法,这里列举并说明一下. os.path.walk() 这是一个传统的用法. wa ...

  4. linux c遍历文件夹 和文件查找的方法

    linux c遍历文件夹的方法比较简单,使用c来实现 #include <iostream> #include <stdio.h> #include <sys/types ...

  5. 遍历文件 创建XML对象 方法 python解析XML文件 提取坐标计存入文件

    XML文件??? xml即可扩展标记语言,它可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言. 里面的标签都是可以随心所欲的按照他的命名规则来定义的,文件名为roi.xm ...

  6. python,遍历文件的方法

    在做验证码识别时,识别时需要和库里的图片对比,找到最接近的那个图片,然后就行到了用与图片一致的字符命名,获取文件的名称,去将图片的名称读出来作为验证码.以下是我通过网上的资料总结的三种文件遍历的方式, ...

  7. HDFS 读取、写入、遍历文件夹获取文件全路径、append

    版权声明:本文为博主原创文章,未经博主同意不得转载.安金龙 的博客. https://blog.csdn.net/smile0198/article/details/37573081 1.从HDFS中 ...

  8. PHP使用glob方法遍历文件夹下所有文件

    PHP使用glob方法遍历文件夹下所有文件 遍历文件夹下所有文件,一般可以使用opendir 与 readdir 方法来遍历.<pre><?php$path = dirname(__ ...

  9. HDFS设计思路,HDFS使用,查看集群状态,HDFS,HDFS上传文件,HDFS下载文件,yarn web管理界面信息查看,运行一个mapreduce程序,mapreduce的demo

    26 集群使用初步 HDFS的设计思路 l 设计思想 分而治之:将大文件.大批量文件,分布式存放在大量服务器上,以便于采取分而治之的方式对海量数据进行运算分析: l 在大数据系统中作用: 为各类分布式 ...

随机推荐

  1. android TextView里边实现图文混配效果

    做的游戏攻略中的图文载入已经用TextView实现.但看到网易新闻里的内容.点击图片能够调到一个新的Activity ,感觉也像Textview 实现的,但不知道怎么弄,想想能够通过动态载入Textv ...

  2. e2fsprogs

    开源文件系统ext2/ext3/ext4管理工具e2progs包含的工具组件: 1.debugfs: ext2/ext3/ext4文件系统调试工具.debugfs是一个交互式的文件系统调试工具,可以用 ...

  3. Linux 防止rm -rf 误删Shell脚本

    #!/bin/bash #:set ff=unix #:set nobomb #-*- coding:utf-8 -*- ####################################### ...

  4. CSS 学习路线(二)选择器

    选择器 规则结构: 分两个基本部分 选择器(selector)和声明块(declaration block) 组成 声明块:由一个或多个声明组成,每一个声明都是属性-值对 选择器分为:元素选择器,类选 ...

  5. Oracle数据库对表基本的操作--增删查改

    --向student表中加入入学时间属性,其数据类型为日期型alter table student add scome date; --删除student表中的入学时间属性alter table st ...

  6. webStorm安装以及集成git使用!

    一:安装webstorm 百度网盘地址:https://pan.baidu.com/s/1K96mg7WYHc6X3odtk7_f2g 密码:2cgd   二:破解webstorm 1:选择liste ...

  7. Navicat Premium 12 激活

    链接:https://pan.baidu.com/s/1R4WB2JjKd0UYnN00CpUPSA 提取码:e3wy (破解工具及软件安装包) 破解流程:https://www.jianshu.co ...

  8. yyy loves Easter_Egg I(恶心的字符串模拟)

    题目背景 Soha的出题效率着实让人大吃一惊.OI,数学,化学的题目都出好了,物理的题还没有一道.于是,Huntfire,absi2011,redbag对soha进行轮番炸,准备炸到soha出来,不料 ...

  9. 关于alias同时使用多个命令小记。

    为了方便自己快速使用命令,接触了一下alias.在使用多个命令时,一般有两种方式.各有不同 根据需求选择. 直接上代码: 1.alias Name='(date;pwd;cmd1;cmd2) 写法简洁 ...

  10. ajaxSubmit请求返回数据成功,但是不执行success回调函数

    最近项目涉及到附件上传就头痛,一直在用plupload插件在做...ie9偶尔抽风但还是可以的... 然后有个需求,表格每行都有个上传按钮,页面多上传按钮. 一.开始的时候,用plupload做的,多 ...