Java reads the HDFs file system

Source: Internet
Author: User

Recently there is a requirement to calculate the user portrait.

The system has about 800W of user volume, calculate some data for each user.

The amount of data is relatively large, the use of hive or no pressure, but the written Oracle, in giving the data to the front, it is more uncomfortable.

And then a different solution:

1.hive calculation, write HDFs

2.API read out, write to HBase (HDFs and hbase version mismatch, no way to use Sqoop Direct)

And then the problem comes.

You need to write an API to read the files on HDFs.

Main class:Readhdfs

 Public classReadhdfs { Public Static voidMain (String[]args) {LongStartlong =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 (); }        LongEndlong =System.currenttimemillis (); Hdfsreadlog.writelog ("Cost" + (Endlong-startlong)/1000 + "seconds"); Hdfsreadlog.writelog ("Cost" + (Endlong-startlong)/1000/60 + "Minute"); }    Private Static voidGetFile (String FilePath)throwsIOException {FileSystem fs=Filesystem.get (Uri.create (FilePath), hdfsconf.getconf ()); Path Path=NewPath (FilePath); if(fs.exists (path) &&fs.isdirectory (path)) {filestatus[] stats=fs.liststatus (path);            Fsdatainputstream is;            Filestatus Stat; byte[] buffer; intindex; StringBuilder Laststr=NewStringBuilder ();  for(Filestatus file:stats) {Try{Hdfsreadlog.writelog ("Start read:" +File.getpath ()); is=Fs.open (File.getpath ()); Stat=fs.getfilestatus (path); intsum =is.available (); if(sum = = 0) {Hdfsreadlog.writelog ("No data:" +File.getpath ()); Continue; } hdfsreadlog.writelog ("There has:" + sum + "bytes" ); Buffer=New byte[sum];
Note that if the file is too large, the memory may be insufficient. When the machine is measured, read a file of more than 100 m, resulting in insufficient memory. Is.readfully (
0, buffer); String result=bytes.tostring (buffer); Write to HBasewritehbase.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"); } }}

Configuration class: Hdfsconfie (No need to hurry, URL and path are well, you can read without configuration)

 Public classhdfsconf { Public StaticConfiguration conf =NULL;  Public StaticConfiguration getconf () {if(conf = =NULL) {conf=NewConfiguration (); String Path= Constant.getsysenv ("hadoop_home") + "/etc/hadoop/"; Hdfsreadlog.writelog ("Get Hadoop Home:" + constant.getsysenv ("Hadoop_home")); //HDFs confConf.addresource (path+ "Core-site.xml"); Conf.addresource (Path+ "Hdfs-site.xml"); Conf.addresource (Path+ "Mapred-site.xml"); Conf.addresource (Path+ "Yarn-site.xml"); }        returnconf; }}

Some constants:

Url:hdfs:ip:prot

Path of the Path:hdfs

Note: Considering the read table, there may be more than one file that does the loop.

Read the next article, write data to HBase

Java reads the HDFs file system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.