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); } } }

      

  • 相关阅读:
    03-Spring默认标签解析
    想要写出好味道的代码,你需要养成这些好习惯!
    IDEA 缺少Springboot启动图标 如何添加
    echarts的canvas大小
    JS控制div上下滚动内容
    2020新的一年开始了
    2019年第一个工作日!
    关于.net项目前后端分离框架(一)
    MongoDB学习一:安装及简单使用
    spring默认标签与自定义标签学习
  • 原文地址:https://www.cnblogs.com/eRrsr/p/5948638.html
Copyright © 2011-2022 走看看