zoukankan      html  css  js  c++  java
  • Hadoop.2.x_简单的测试文件读取与上传

    代码如下, 后备参考:

    package com.bigdata.hadoop.hdfs;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.FSDataInputStream;
    import org.apache.hadoop.fs.FSDataOutputStream;
    import org.apache.hadoop.fs.FileSystem;
    import org.apache.hadoop.fs.Path;
    import org.apache.hadoop.io.IOUtils;
    import org.junit.Test;
    
    public class HdfsTest {
    	
    	public static FileSystem getFileSystem() throws IOException{
    Configuration configuration = new Configuration(); //Some configurations used to replace the configuration of resource files //configuration.set("fs.defaultFS", "hdfs://linux-66-64.liuwl.com:8020"); //... return FileSystem.get(configuration); } @Test public static void read() throws IOException{ String fileName = "/user/liuwl/tmp/core-site.xml"; FileSystem fileSystem = getFileSystem(); Path path = new Path(fileName); FSDataInputStream instream = fileSystem.open(path); try{ IOUtils.copyBytes(instream, System.out, 4896,false); }catch(Exception e){ e.getStackTrace(); }finally{ IOUtils.closeStream(instream); } } @Test public void write() throws IOException{ String fileName = "/user/liuwl/tmp/input/wc.input"; FileSystem fileSystem = getFileSystem(); Path writePath = new Path(fileName); FSDataOutputStream outStream = fileSystem.create(writePath); FileInputStream inStream = new FileInputStream(new File("/home/liuwl/opt/data/wc.input")); try{ IOUtils.copyBytes(inStream, outStream, 4896,false); }catch(Exception e){ e.getStackTrace(); }finally{ IOUtils.closeStream(inStream); IOUtils.closeStream(outStream); } } }

      

  • 相关阅读:
    python爬虫开发与项目实践-学习笔记(一)
    python之TypeError
    学习笔记-python
    python学习之Unable to locate element
    Chrome浏览器之 webdriver(Chrome version must be >= 63.0.3239.0)
    POJ 1830 开关问题 高斯消元
    HDU 4135 Co-prime 容斥原理
    HDU 1796 How many integers can you find 容斥原理
    卡特兰数,组合数,斯特林数,逆元模板
    HDU 6134 Killer Names 数学 斯特林数
  • 原文地址:https://www.cnblogs.com/eRrsr/p/5948638.html
Copyright © 2011-2022 走看看