java hdfs 批量写数据库_java操作hdfs到数据库或者缓存

使用hadoop工具将数据分析出来以后,须要做入库处理或者存到缓存中。不然就没了意义

一下是使用javaAPI操作hdfs存入缓存的代码:

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);//这儿是自己实现的一个路径顾虑器,也可不适用,在【1】处直接推断part-r-等标示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");//做存入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();

}

}

}上面代码须要改动后使用,请勿直接粘贴。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值