/**
* 获取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. java学习笔记(4):内存管理

    在内存的方面,Java自身带有垃圾运行机制,表面上好像我们可以忽略不计,但是如果我们对其加以理解的话,说不定有时会达到事半功倍的效果. 所以自己花些时间整理一些此类的资料. 一.基本概念 1.栈 对于 ...

  2. struct可以拥有class般的构造函数

    struct A { int a, b; A(int x, int y) :a(x), b(y){} }; int main() { A a(1, 2); cout << a.a < ...

  3. mybatis常用语句

    <trim>标签中,prefix 前缀,suffix后缀, suffixOverrides语句最后如果有指定符号,则去除此符号, prefixOverrides语句最前面如果有指定符号,则 ...

  4. 各种位置和高度计算:.position()、.offset()、.outerHeight()、.scrollTop、.scrollHeight、.clientHeight

    1..position()和.offset() jquery的.position()获取相对于最近的position为relative或absolute的父元素的偏移,返回.position().le ...

  5. php 获取网站根目录的写法

    路径方式,代码如下: define('BASE_PATH',str_replace('\\','/',realpath(dirname(__FILE__).'/'))."/"); ...

  6. JS判断手机端和PC端自动跳转

    <script type="text/javascript">     function browserRedirect() {     var sUserAgent ...

  7. [linux]磁盘挂载

    最近磁盘空间不足了, 所以需要将更多的磁盘空间加进来. 因为目前占空间最多的就是home, 无论是下载还是本地用户的东西都是放在这里的. 将分区格式化为ext4, 然后使用blkid /dev/sda ...

  8. MySQL性能分析脚本

    #!/usr/bin/python #!coding:utf-8 import mysql.connector as connector import json """ ...

  9. 关于取url或者微信中参数的js

    今天遇到这么个情况,因为是第一次弄,所以在这里做了个记录,因为要弄过二维码的功能,要获取里面的参数并传给后台,所以要对二维码里面的地址进行过滤.刚开始是很惆怅的,因为之前没弄过,原以为可以通过spli ...

  10. swift闭包-备

    我给Swift 中的闭包一个定义:闭包是自包含的匿名函数代码块,可以作为表达式.函数参数和函数返回值,闭包表达式的运算结果是一种函数类型. Swift中的闭包类似于Objective-C中的代码块.J ...