標籤: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到資料庫或者緩衝