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