java操作hdfs到資料庫或者緩衝

來源:互聯網
上載者:User

標籤:style   io   os   ar   使用   java   for   sp   資料   

使用hadoop工具將資料分析出來以後,需要做入庫處理或者存到緩衝中,不然就沒了意義

一下是使用javaAPI操作hdfs存入緩衝的代碼:

<span style="font-family:Microsoft YaHei;font-size:14px;">public class InterestToRedisJob {FileSystem hdfs = null;public InterestToRedisJob(){init();}private void init(){Configuration conf = new Configuration();conf.set("fs.default.name", "hdfs地址");try {hdfs = FileSystem.get(conf);} catch (IOException e) {e.printStackTrace();}}public void ReadFileToReids(String path) throws IOException{Path paths = new Path(path);FileStatus[] files = hdfs.listStatus(paths);</span>
<span style="font-family:Microsoft YaHei;font-size:14px;"><span style="white-space:pre"></span>//這兒是自己實現的一個路徑顧慮器,也可不適用,在【1】處直接判斷part-r-等標示</span>
<span style="font-family:Microsoft YaHei;font-size:14px;">PathFilter filter = new ResultNameFilter("part-r-"); Text line = new Text();RedisClient redis = new RedisClient();for(FileStatus file:files){if(file.isDir() || !filter.accept(file.getPath())){//【1】continue;}else{FSDataInputStream input = null;try{input = hdfs.open(file.getPath());LineReader reader = new LineReader(input);while(reader.readLine(line) > 0){System.out.println(line);String[] arr = line.toString().split("\t");</span>
<span style="font-family:Microsoft YaHei;font-size:14px;"><span style="white-space:pre"></span>//做存入redis處理redis.saveHsetValue(arr[0], "interest", arr[1]);}}catch(Exception e){e.printStackTrace();}finally{if(input != null){input.close();}}}}}public static void main(String[] args) {InterestToRedisJob job = new InterestToRedisJob();try {job.ReadFileToReids("你的path");} catch (IOException e) {e.printStackTrace();}}}</span>
上面代碼需要修改後使用,請勿直接粘貼。

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.