码迷,mamicode.com
首页 > 其他好文 > 详细

HDFS读写程序小测试

时间:2016-05-13 10:12:45      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

  WriteHdfs:

package aa;

import java.io.*;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;

public class WriteHdfs {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        try {
            FileSystem fs = FileSystem.get(conf);
            Path f = new Path("ttt.txt");
            System.out.println(fs.isFile(f));
            System.out.println(f.toString());
            FSDataOutputStream os = fs.create(f, true);
            os.writeChars("test");
            os.flush();
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

  ReadHdfs:

package aa;

import java.io.InputStream;

import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils;
 
public class ReadHdfs {
    public static void main(String[] args) throws Exception {
        String uri = "ttt.txt";
        Configuration conf = new Configuration();
    FileSystem fs = FileSystem. get(URI.create (uri), conf);
    InputStream in = null;
    try {
         in = fs.open( new Path(uri));
         IOUtils.copyBytes(in, System.out, 4096, false);
        } finally {
         IOUtils.closeStream(in);
        }
    }
}

  先写后读输出:

log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
t e s t

 

HDFS读写程序小测试

标签:

原文地址:http://www.cnblogs.com/garfieldcgf/p/5485645.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!