/*** 经过正则获取该目录下知足条件的全部目录
*@paramluceneFilePathRegular 正则目录,如/user/solrindex/正则表达式
*@return知足正则表达式的目录集合 list*/
public static ListfetchDirByRegularLinux(String luceneFilePathRegular){
List 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=newFile(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);
}returnlist;
}/*** 经过正则获取该目录下知足条件的全部目录
*@paramluceneFilePathRegular 正则目录,如hdfs:/user/solrindex/正则表达式
*@paramnameNodeConfigPath //获取name配置信息目录
*@return知足正则表达式的目录集合 list*/
public static ListfetchDirByRegularHdfs(String luceneFilePathRegular,String nameNodeConfigPath){
List 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);
}returnlist;
}/*** @Method Description:按正则表示是匹配字符串
*@paramstr
*@paramregular
*@return*@author: libingjie*/
public staticBoolean matchStr(String str, String regular) {
Pattern pattern=Pattern.compile(regular);
Matcher matcher=pattern.matcher(str);returnmatcher.matches();
}