首页 行业资讯 宠物日常 宠物养护 宠物健康 宠物故事

怎么使用java代码直接将从外部拿到的数据存入hdfs

发布网友

我来回答

3个回答

懂视网

一下是使用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("	");</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到数据库或者缓存

标签:soft   [1]   tac   inpu   catch   efault   reids   int   read   

热心网友

存入HDFS有好几种数据格式,我这里给你列出一种格式的存储,sequence的

public class SeqWrite {

private static final String[] data = { "a,b,c,d,e,f,g", "h,i,j,k,l,m,n", "o,p,q,r,s,t", "u,v,w,x,y,z", "0,1,2,3,4", "5,6,7,8,9" };

public static void main(String[] args) throws IOException, Exception {

Configuration configuration = new Configuration();
//这里是你主机的地址
configuration.set("fs.defaultFS", "192.168.51.140");
//这个是存储的路径
Path path = new Path("/tmp/test1.seq");
Option option = SequenceFile.Writer.file(path);
Option optKey = SequenceFile.Writer.keyClass(IntWritable.class);
Option optValue = SequenceFile.Writer.valueClass(Text.class);
SequenceFile.Writer writer = null;
IntWritable key = new IntWritable(10);
Text value = new Text();

writer = SequenceFile.createWriter(configuration, option, optKey, optValue);

for (int i = 0; i < data.length; i++) {
key.set(i);
value.set(data[i]);
writer.append(key, value);
writer.hsync();
Thread.sleep(10000L);
}

IOUtils.closeStream(writer);
}
}

热心网友

创建对象 注入属性 在添加不就可以了

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com