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;
}
}
}
- 一句python,一句R︱模块导入与查看、数据读写出入、数据查看函数、数据类型、遍历文件
先学了R,最近刚刚上手python,所以想着将python和R结合起来互相对比来更好理解python.最好就是一句python,对应写一句R. pandas中有类似R中的read.table的功能,而 ...
- python os.walk()遍历文件夹
转自 http://alanland.iteye.com/blog/612459 via @alanland 今天第一次进行 文件遍历,自己递归写的时候还调试了好久,(主要因为分隔符号的问题),后来发 ...
- python中遍历文件的3个方法
转自: http://www.jb51.net/article/54640.htm 用python进行文件遍历有多种方法,这里列举并说明一下. os.path.walk() 这是一个传统的用法. wa ...
- linux c遍历文件夹 和文件查找的方法
linux c遍历文件夹的方法比较简单,使用c来实现 #include <iostream> #include <stdio.h> #include <sys/types ...
- 遍历文件 创建XML对象 方法 python解析XML文件 提取坐标计存入文件
XML文件??? xml即可扩展标记语言,它可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言. 里面的标签都是可以随心所欲的按照他的命名规则来定义的,文件名为roi.xm ...
- python,遍历文件的方法
在做验证码识别时,识别时需要和库里的图片对比,找到最接近的那个图片,然后就行到了用与图片一致的字符命名,获取文件的名称,去将图片的名称读出来作为验证码.以下是我通过网上的资料总结的三种文件遍历的方式, ...
- HDFS 读取、写入、遍历文件夹获取文件全路径、append
版权声明:本文为博主原创文章,未经博主同意不得转载.安金龙 的博客. https://blog.csdn.net/smile0198/article/details/37573081 1.从HDFS中 ...
- PHP使用glob方法遍历文件夹下所有文件
PHP使用glob方法遍历文件夹下所有文件 遍历文件夹下所有文件,一般可以使用opendir 与 readdir 方法来遍历.<pre><?php$path = dirname(__ ...
- HDFS设计思路,HDFS使用,查看集群状态,HDFS,HDFS上传文件,HDFS下载文件,yarn web管理界面信息查看,运行一个mapreduce程序,mapreduce的demo
26 集群使用初步 HDFS的设计思路 l 设计思想 分而治之:将大文件.大批量文件,分布式存放在大量服务器上,以便于采取分而治之的方式对海量数据进行运算分析: l 在大数据系统中作用: 为各类分布式 ...
随机推荐
- Go语言之旅:基本类型
原文地址:https://learn-linux.readthedocs.io 欢迎关注我们的公众号:小菜学编程 (coding-fan) Go 内置了以下基本类型: 布尔 bool 字符串 stri ...
- ansible批量修改用户密码
实现批量修改目标主机多个用户密码: --- - hosts: testchanange passwd gather_facts: false tasks: - name: change you pas ...
- mina 通讯框架
Apache Mina Server 是一个网络通信应用框架,也就是说,它主要是对基于TCP/IP.UDP/IP协议栈的通信框架(当然,也可以提供JAVA 对象的序列化服务.虚拟机管道通信服务等),M ...
- eclipse安装activiti插件
参考: https://blog.csdn.net/augustaurora/article/details/59618737 https://blog.csdn.net/qq_33547950/ar ...
- element UI 饿了么 UI 分页 按钮不显示的问题
https://blog.csdn.net/sinat_37255207/article/details/88914235 一个很坑的 深坑 element UI 的 按钮 不显示的深坑 <e ...
- SQL循环插入批量数据
declare @i intdeclare @qid int set @i=1set @qid=100 while @i<50000begininsert into Order(orderid, ...
- GoogleMock初探(0)
在进行测试过程中,待测的类或者方法经常会依赖其他类或方法的实现.如果此时这些依赖还没有实现,则需要打桩.另外测试讲求独立,测试之间的互相依赖会导致测试最终混乱不堪. GoogleMock提供一套方法来 ...
- python使用findall正则匹配出所有符合条件的字符串
# -*- coding:utf-8 -*- import re mystr="qqq key:www.baidu.com<br>key:www.tengxun.com<b ...
- 20155209 实验三 敏捷开发与XP实践
20155209 实验三 敏捷开发与XP实践 实验内容 1. XP基础 2. XP核心实践 3. 相关工具 提交点一: 在IDEA中使用工具(Code->Reformate Code)把下面代码 ...
- 虚拟机安装与Linux命令的学习 ——20155215宣言
一.虚拟机的安装 虚拟机的安装对我来说真可谓是一波三折.打开老师发布的安装教程,简单地浏览了一下,主要步骤都有图文解说.我本来以为这个安装按部就班即可,可哪知道,问题一个接着一个出现. 问题1 在我下 ...