hadoop的读写操作

3 篇文章 0 订阅
1 篇文章 0 订阅

这里写图片描述

在txt文件中写点需要上传的数据,conf是source包,lib是jar包

package com.wangbo.day0830;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

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;

/**
 * hdfs操作类
 * @author Administrator
 *
 */
public class HdfsTest {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        //创建hdfs的配置
        Configuration conf = new Configuration();

        //加载配置文件
        conf.addResource("conf/core-site.xml");
        conf.addResource("conf/hdfs-site.xml");

        //获取HDFS的句柄
        FileSystem fs = FileSystem.get(conf);

//      //向HDFS写入数据
//      writeHDFS(fs);

        //向HDFS读数据
        readHDFS(fs);
    }

    private static void readHDFS(FileSystem fs) throws IllegalArgumentException, IOException {
        //获取文件输入流
        FSDataInputStream in = fs.open(new Path("syhz/hellohadoop.txt"));

        byte[] buff = new byte[1024];

        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File("d:/hdfs.txt")));

//      写入文件 字节流
        int length = in.read(buff);
        while(-1 != length)
        {

//          System.out.println(new String(buff, 0, length));
            out.write(buff, 0, length);
            length = in.read(buff);
        }
        in.close();
        out.close();



        System.out.println("读取结束!");
    }

    public static void writeHDFS(FileSystem fs) throws IOException
    {
        //在HDFS上新建一个虚拟目录
        Path path = new Path("syhz/hellohadoop.txt");

        //输出流
        FSDataOutputStream out = fs.create(path);

        //读取数据写入数据 字节流
//      BufferedInputStream bufferIn = new BufferedInputStream(new FileInputStream(new File("text.txt")));

//      byte[] buff = new byte[1024];

//      int length = bufferIn.read(buff);
//      
//      while(-1 != length)
//      {
//          //写入
//          out.write(buff,0,length);
//          out.flush();
//          length = bufferIn.read(buff);
//      }
//      out.close();
//      bufferIn.close();


        //读取数据写入数据 字符流
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("text.txt")),"gbk"));
        String line = null;
        while((line = reader.readLine())!=null){
            out.write((line+System.getProperty("line.separator")).getBytes("utf-8"));
            out.flush();
        }
        out.close();
        reader.close();
    }


}

这里写图片描述

这里写图片描述

就是酱紫

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值