• 【转】Java从hdfs上读取文件中的某一行


    【From】https://blog.csdn.net/u010989078/article/details/51790166

    package test;
     
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.URI;
     
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.FSDataInputStream;
    import org.apache.hadoop.fs.FileSystem;
    import org.apache.hadoop.fs.Path;
     
    public class ReadHDFS {
     
        public static String getStringByTXT(String txtFilePath, Configuration conf)
        {
     
            StringBuffer buffer = new StringBuffer();
            FSDataInputStream fsr = null;
            BufferedReader bufferedReader = null;
            String lineTxt = null;
            try
            {
                FileSystem fs = FileSystem.get(URI.create(txtFilePath),conf);
                fsr = fs.open(new Path(txtFilePath));
                bufferedReader = new BufferedReader(new InputStreamReader(fsr));        
                while ((lineTxt = bufferedReader.readLine()) != null)
                {
                    if(lineTxt.split("	")[0].trim().equals("00067")){
                        return lineTxt;
                    }
                    
                }
            } catch (Exception e)
            {
                e.printStackTrace();
            } finally
            {
                if (bufferedReader != null)
                {
                    try
                    {
                        bufferedReader.close();
                    } catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                }
            }
     
            return lineTxt;
        }
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Configuration conf = new Configuration();
            String txtFilePath = "hdfs://master:9000/OutPutBus/line/part-r-00000";
            String mbline = getStringByTXT(txtFilePath, conf);
            System.out.println(mbline);
        }
     
    }
    --------------------- 
    作者:cao_yaqi 
    来源:CSDN 
    原文:https://blog.csdn.net/u010989078/article/details/51790166 
    版权声明:本文为博主原创文章,转载请附上博文链接!
  • 相关阅读:
    测试工具iometer, iozone, bonnie++
    python 解析properties文件的工具类
    Linux文件系统性能测试工具fdtree和iozone
    python的WEB容器框架
    iozone的介绍与使用
    Python 的 SOAP 实现
    在powerpoint2007里,如何改变模板里的背景图形
    python的接口和抽象类
    决策树算法总结
    C++ 重载、覆盖和隐藏
  • 原文地址:https://www.cnblogs.com/pekkle/p/11004470.html
Copyright © 2020-2023  润新知