四、記錄1----獲取hdfs上FileSystem的方法 記錄2:正則匹配路徑:linux、hdfs

    /**
     * 獲取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();
    }
相關文章
相關標籤/搜索