Java API reads a single HDFS File
Single file on HDFS:
-bash-3.2$ hadoop fs -ls /user/pms/ouyangyewei/data/input/combineorder/repeat_rec_categoryFound 1 items-rw-r--r-- 2 deploy supergroup 520 2014-08-14 17:03 /user/pms/ouyangyewei/data/input/combineorder/repeat_rec_category/repeatRecCategory.txt
File Content:
-bash-3.2$ hadoop fs -cat /user/pms/ouyangyewei/data/input/combineorder/repeat_rec_category/repeatRecCategory.txt | more810496098554729719175320971895971902971922958261972047972050
Java API: how to read a single HDFS file using FileSystem
/*** Obtain repeated recommendation categories, separated by commas (,) * @ param filePath * @ param conf * @ return */public String getRepeatRecCategoryStr (String filePath) {final String DELIMITER = "\ t"; final String INNER_DELIMITER = ","; String categoryFilterStrs = new String (); BufferedReader br = null; try {FileSystem fs = FileSystem. get (new Configuration (); FSDataInputStream inputStream = fs. open (new Path (filePath); br = new BufferedReader (new InputStreamReader (inputStream); String line = null; while (null! = (Line = br. readLine () {String [] strs = line. split (DELIMITER); categoryFilterStrs + = (strs [0] + INNER_DELIMITER) ;}} catch (IOException e) {e. printStackTrace ();} finally {if (null! = Br) {try {br. close ();} catch (IOException e) {e. printStackTrace () ;}} return categoryFilterStrs ;}