小編給大家分享一下SequenceFile如何實(shí)現(xiàn)讀寫,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
成都創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司,專注做網(wǎng)站、網(wǎng)站設(shè)計(jì)、網(wǎng)站營(yíng)銷推廣,申請(qǐng)域名,網(wǎng)站空間,網(wǎng)站運(yùn)營(yíng)有關(guān)企業(yè)網(wǎng)站制作方案、改版、費(fèi)用等問題,請(qǐng)聯(lián)系成都創(chuàng)新互聯(lián)。
SequenceFile 讀
public static void main(String[] args) throws IOException { Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(conf); Path seqFile = new Path("/user/hive/warehouse/abc/seqfile.seq"); SequenceFile.Reader reader = new SequenceFile.Reader(fs, seqFile, conf); IntWritable key = new IntWritable(); Text value = new Text(); while(reader.next(key, value)){ System.out.println(key); System.out.println(value); } IOUtils.closeStream(reader); }
聲明順序文件的Reader實(shí)例后,調(diào)用next() 方法迭代讀取記錄。最后需要關(guān)閉reader實(shí)例。
如果鍵值對(duì)讀取成功,則返回true;如果已經(jīng)讀到文件末尾,則返回false。
SequenceFile 寫
public static void main(String[] args) throws IOException { Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(conf); Path targetPath = new Path("/user/hive/warehouse/test_url"); final Option optPath = SequenceFile.Writer.file(targetPath); final Option optKeyClass = SequenceFile.Writer.keyClass(Text.class); final Option optValueClass = SequenceFile.Writer.valueClass(BytesWritable.class); final SequenceFile.Writer writer = SequenceFile.createWriter(conf, optPath, optKeyClass, optValueClass); final Collection<File> listFiles = FileUtils.listFiles(new File("/data1/url/"), new String[]{"log"}, false); Text key = null; BytesWritable value = null; for (File file : listFiles) { key = new Text(file.getPath()); value = new BytesWritable(FileUtils.readFileToByteArray(file)); writer.append(key, value); } IOUtils.closeStream(writer); }
通過CreateWriter() 靜態(tài)方法創(chuàng)建SequenceFile 對(duì)象,并返回SequenceFile.Writer 實(shí)例。
擁有SequenceFile.Writer 實(shí)例后,就可以通過 append() 方法在文件末尾追加數(shù)據(jù)。
最后關(guān)閉實(shí)例。
看完了這篇文章,相信你對(duì)“SequenceFile如何實(shí)現(xiàn)讀寫”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
本文名稱:SequenceFile如何實(shí)現(xiàn)讀寫
當(dāng)前路徑:http://www.rwnh.cn/article2/peohoc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、微信小程序、網(wǎng)站改版、網(wǎng)站策劃、手機(jī)網(wǎng)站建設(shè)、電子商務(wù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)