/**
* 获取hadoop相关配置信息
* @param hadoopConfPath 目前用户需要提供hadoop的配置文件路径
* @return
*/
public static Configuration getHadoopConfig(String hadoopConfPath){
Configuration conf=new Configuration();
conf.addResource(new Path(hadoopConfPath+"/core-site.xml"));
conf.addResource(new Path(hadoopConfPath+"/hdfs-site.xml"));
return conf;
} /**
* 获取hdfs文件系统连接
* @param hadoopConfPath 目前用户需要提供hadoop的配置文件路径
* @return
*/
public static FileSystem getFileSystem(String hadoopConfPath) {
Configuration conf=new Configuration();
conf.addResource(new Path(hadoopConfPath+"/core-site.xml"));
conf.addResource(new Path(hadoopConfPath+"/hdfs-site.xml"));
FileSystem fs = null;
try {
fs=FileSystem.get(conf);
} catch (IOException e) { LOGGER.error("从path={}路径获取hadoop配置信息错误:{}", hadoopConfPath, e.getMessage()); } return fs;
}

正则匹配路径的方法:

 /**
* 通过正则获取该目录下满足条件的所有目录
* @param luceneFilePathRegular 正则目录,如/user/solrindex/正则表达式
* @return 满足正则表达式的目录集合 list
*/
public static List<String> fetchDirByRegularLinux(String luceneFilePathRegular){
List<String> list=new ArrayList<>();
//分割获取主目录
int len= luceneFilePathRegular.lastIndexOf(EtlConstants.LINUX_ROUTE_SEGMENT)+1;
String mainDir=luceneFilePathRegular.substring(0, len);
String regular=luceneFilePathRegular.substring(len,luceneFilePathRegular.length());
File dir=new File(mainDir);
if(dir.exists() && dir.isDirectory()){
File [] arr= dir.listFiles();
for (File file : arr) {
if (file.exists() && file.isDirectory()) {
String fileName = file.getName();
if (matchStr(fileName, regular)) {
list.add(file.getAbsolutePath()+SolrUtil.INDEX_DIR_SUFFIX);
}
}
}
}
if(list.size()>0){
LOGGER.info("通过正则匹配到的Solr目录有:");
for (String s : list) {
LOGGER.info(s);
}
}else{
LOGGER.error("路径{}下,不存在满足正则:{}条件的目录", dir, regular);
}
return list;
} /**
* 通过正则获取该目录下满足条件的所有目录
* @param luceneFilePathRegular 正则目录,如hdfs:/user/solrindex/正则表达式
* @param nameNodeConfigPath //获取name配置信息目录
* @return 满足正则表达式的目录集合 list
*/
public static List<String> fetchDirByRegularHdfs(String luceneFilePathRegular,String nameNodeConfigPath){
List<String> list=new ArrayList<>();
FileSystem fs=HdfsUtil.getFileSystem(nameNodeConfigPath);
String prefixHdfs=luceneFilePathRegular.split(":")[0];
String hdfsPath=luceneFilePathRegular.split(":")[1];
//分割获取主目录
int len= hdfsPath.lastIndexOf(EtlConstants.LINUX_ROUTE_SEGMENT)+1;
String mainDir=hdfsPath.substring(0, len);
String regular=hdfsPath.substring(len, hdfsPath.length());
try {
FileStatus[] fileStatuses = fs.globStatus(new Path(mainDir+"*"));
for (FileStatus fileStatus : fileStatuses){
if (fileStatus.isDirectory() && matchStr(fileStatus.getPath().getName(), regular)) {
list.add(prefixHdfs+":"+mainDir+fileStatus.getPath().getName()+SolrUtil.INDEX_DIR_SUFFIX);
}
} } catch (IOException e) {
LOGGER.error("获取hdfs目录信息异常,路径:{},异常信息:{}",luceneFilePathRegular,e.getMessage());
e.printStackTrace();
}
if(list.size()>0){
LOGGER.info("通过正则匹配到的Solr目录有:");
for (String s : list) {
LOGGER.info(s);
}
}else{
LOGGER.error("路径{}下,不存在满足正则:{}条件的目录", luceneFilePathRegular, regular);
}
return list;
} /**
* @Method Description:按正则表示是匹配字符串
* @param str
* @param regular
* @return
* @author: libingjie
*/
public static Boolean matchStr(String str, String regular) {
Pattern pattern = Pattern.compile(regular);
Matcher matcher = pattern.matcher(str);
return matcher.matches();
}

4、记录1----获取hdfs上FileSystem的方法 记录2:正则匹配路径:linux、hdfs的更多相关文章

  1. php程序无法记录log情况下可尝试下面方法记录log

    error_reporting(E_ERROR | E_PARSE); function shutdownCallback(){    $arrError = error_get_last(); // ...

  2. python获取文件扩展名的方法(转)

    主要介绍了python获取文件扩展名的方法,涉及Python针对文件路径的相关操作技巧.具体实现方法如下: 1 2 3 4 import os.path def file_extension(path ...

  3. python获取文件扩展名的方法

    主要介绍了python获取文件扩展名的方法,涉及Python针对文件路径的相关操作技巧 import os.path def file_extension(path): ] print file_ex ...

  4. Linux记录-shell获取hdfs used使用

    #!/bin/bash export JAVA_HOME=/app/jdk/jdk1.8.0_92 export HADOOP_HOME=/app/hadoop export HADOOP_CONF_ ...

  5. Eclipse 上传 删除 下载 分析 hdfs 上的文件

    本篇讲解如何通过Eclipse 编写代码去操作分析hdfs 上的文件. 1.在eclipse 下新建Map/Reduce Project项目.如图:  项目建好后,会默认加载一系列相应的jar包. 下 ...

  6. 用流的方式来操作hdfs上的文件

    import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import ...

  7. HBase 在HDFS 上的目录树

         总所周知,HBase 是天生就是架设在 HDFS 上,在这个分布式文件系统中,HBase 是怎么去构建自己的目录树的呢? 这里只介绍系统级别的目录树. 一.0.94-cdh4.2.1版本 系 ...

  8. hadoop的API对HDFS上的文件访问

    这篇文章主要介绍了使用hadoop的API对HDFS上的文件访问,其中包括上传文件到HDFS上.从HDFS上下载文件和删除HDFS上的文件,需要的朋友可以参考下hdfs文件操作操作示例,包括上传文件到 ...

  9. iOS获取UIView上某点的颜色值

    项目需求中遇到获取UIView上某个坐标点的RGB颜色值的需求,现在把自己找到的解决方案简单总结记录一下,遇到了下面的情况: 不可移动的UIView 旋转式的UIView 滑条式的UIView 不可移 ...

随机推荐

  1. 武汉科技大学ACM :1004: C语言程序设计教程(第三版)课后习题6.3

    Problem Description 求Sn=2+22+222+…+22…222(有n个2)的值. 例如:2+22+222+2222+22222(n=5),n由键盘输入. Input n Outpu ...

  2. cas+tomcat+shiro实现单点登录-1-tomcat添加https协议

    目录 1.tomcat添加https安全协议 2.下载cas server端部署到tomcat上 3.CAS服务器深入配置(连接MYSQL) 4.Apache Shiro 集成Cas作为cas cli ...

  3. shell的string operator

    ${varname:-word} 如果varname存在并且不为nil,那么返回varname的值,否则返回word.这个常用来在varname未定义时返回默认值 ${varname:=word} 如 ...

  4. 解决表格里面使用text-overflow后依旧不能隐藏超出的文本

    解决表格里面使用text-overflow后依旧不能隐藏超出的文本 来源: http://blog.csdn.net/colinmuxi/article/details/9069595  (非原创,自 ...

  5. php对象中类的继承性访问类型控制

    类型的访问控制通过使用修饰符允许开发人员对类中成员的访问进行限制.这是PHP5的新特性,也是OOP语言中的重要特性,大多数OOP语言都已支持此特性.PHP5支持如下三种访问修饰符,在类的封装中我们已经 ...

  6. C语言初学 if-else语句判断俩数的最大值

    #include<stdio.h> main() { float a,b; printf("输入俩个任意实数\n"); scanf("%f%f",& ...

  7. SDWebImage使用——一个可管理远程图片加载的类库

    SDWebImage使用——一个可管理远程图片加载的类库     SDWebImage使用——一个可管理远程图片加载的类库 SDWebImage托管在github上.https://github.co ...

  8. Lintcode--004(最小子串覆盖)

    给定一个字符串source和一个目标字符串target,在字符串source中找到包括所有目标字符串字母的子串. 注意事项 如果在source中没有这样的子串,返回"",如果有多个 ...

  9. PYTHON线程知识再研习C---线程互斥锁

    结合例子,就很好理解了. 就是不要让共享变量被各个线程无序执行,导致结果不可预期 threading模块中定义了Lock类,可以方便的处理锁定: #创建锁mutex = threading.Lock( ...

  10. LeetCode_Rotate List

    Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given ...