Java 讀取HDFS檔案系統

來源:互聯網
上載者:User

標籤:url   http   ret   有一個   tin   apr   計算   create   href   

最近有個需求,計算使用者畫像。

系統大概有800W的使用者量,算每個使用者的一些資料。

資料量比較大,算用hive還是毫無壓力的,但是寫的oracle,在給出資料給前端,就比較難受了。

然後換了種解決方案:

  1.hive計算,寫的HDFS

  2.API讀出來,寫到hbase(hdfs和hbase的版本不匹配,沒辦法用sqoop 直接導)

然後問題就來了。

需要寫個API,讀HDFS上的檔案。

主要類:ReadHDFS 

public class ReadHDFS {    public static void main(String[]args){        long startLong = System.currentTimeMillis();        HDFSReadLog.writeLog("start read file");        String path;        if (args.length > 1) {//            path = args[0];            Constant.init(args[0],args[1]);        }        HDFSReadLog.writeLog(Constant.PATH);        try {            getFile(Constant.URI + Constant.PATH);        } catch (IOException e) {            e.printStackTrace();        }        long endLong = System.currentTimeMillis();        HDFSReadLog.writeLog("cost " + (endLong -startLong)/1000 + " seconds");        HDFSReadLog.writeLog("cost " + (endLong -startLong)/1000/60 + " minute");    }    private static void getFile(String filePath) throws IOException {        FileSystem fs = FileSystem.get(URI.create(filePath), HDFSConf.getConf());        Path path = new Path(filePath);        if (fs.exists(path) && fs.isDirectory(path)) {            FileStatus[] stats = fs.listStatus(path);            FSDataInputStream is;            FileStatus stat;            byte[] buffer;            int index;            StringBuilder lastStr = new StringBuilder();            for(FileStatus file : stats){                try{                    HDFSReadLog.writeLog("start read : " + file.getPath());                    is = fs.open(file.getPath());                    stat = fs.getFileStatus(path);                    int sum  = is.available();                    if(sum == 0){                        HDFSReadLog.writeLog("have no data : " + file.getPath() );                        continue;                    }                    HDFSReadLog.writeLog("there have  : " + sum + " bytes" );                    buffer = new byte[sum];
            // 注意一點,如果檔案太大了,可能會記憶體不夠用。在本機測得時候,讀一個100多M的檔案,導致記憶體不夠。 is.readFully(
0,buffer); String result = Bytes.toString(buffer); // 寫到 hbase WriteHBase.writeHbase(result); is.close(); HDFSReadLog.writeLog("read : " + file.getPath() + " end"); }catch (IOException e){ e.printStackTrace(); HDFSReadLog.writeLog("read " + file.getPath() +" error"); HDFSReadLog.writeLog(e.getMessage()); } } HDFSReadLog.writeLog("Read End"); fs.close(); }else { HDFSReadLog.writeLog(path + " is not exists"); } }}

配置類:HDFSConfie(趕緊沒什麼用,url和path配好了,不需要配置就可以讀)

public class HDFSConf {    public static Configuration conf = null;    public static Configuration getConf(){        if (conf == null){            conf = new Configuration();            String path  = Constant.getSysEnv("HADOOP_HOME")+"/etc/hadoop/";            HDFSReadLog.writeLog("Get hadoop home : " + Constant.getSysEnv("HADOOP_HOME"));            // hdfs conf            conf.addResource(path+"core-site.xml");            conf.addResource(path+"hdfs-site.xml");            conf.addResource(path+"mapred-site.xml");            conf.addResource(path+"yarn-site.xml");        }        return conf;    }}

 

一些常量:

 url : hdfs:ip:prot

 path : HDFS的路徑

註: 考慮到讀的表,可能不止有一個檔案,做了迴圈。

 

看下篇,往hbase寫資料

 

Java 讀取HDFS檔案系統

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.