java操作hdfs到数据库或者缓存,javahdfs
java操作hdfs到数据库或者缓存,javahdfs
使用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>上面代码需要修改后使用,请勿直接粘贴。
你好,在网站上帮你找到了一篇类似的文章,你参考一下,复制“tomcat启动时如何读取数据库内容”百度一下。就可以看到,我看了一下,原理就那样。
写一个静态类,里面包含:1.数据信息结构,2.读取数据方法,和3.检测是否读取数据方法,每次需要数据的时候,读取3.检测是否读取数据方法,如果数据没有加载则调用2.读取数据方法,然后从1.数据信息结构获取数据,如果是已经加载数据则直接从1数据信息结构中获取数据就可以了
评论暂时关闭