Hadoop 基本API 使用 -- 从HDFS文件系统 上读取文件

 

 Hadoop 基本API 使用 从HDFS文件系统 上读取文件

不多说 看代码:

 

如果报错 wutils.exe  之类:

 请注意一定要添加这一行:

 

 System.setProperty("hadoop.home.dir", "D:\\hadoop-2.7.3");

 

 

 

 

import java.io.BufferedReader;  
import java.io.BufferedWriter;  
import java.io.FileInputStream;  
import java.io.FileWriter;  
import java.io.InputStreamReader;  
  
import org.apache.hadoop.conf.Configuration;  
import org.apache.hadoop.fs.FSDataInputStream;  
import org.apache.hadoop.fs.FileSystem;  
import org.apache.hadoop.fs.Path; 

/**
 * 类说明
 * 
 * <pre>
 * Modify Information:
 * Author        Date          Description
 * ============ =========== ============================
 * DELL          2017年3月23日    Create this file
 * </pre>
 * 
 */

public class ReadFromHDFS {  
  
    /** 
     * @param args 
     * @throws Throwable  
     */  
    public static void main(String[] args) throws Throwable {  
        String[]  ioArg = new String[]{"hdfs://192.168.233.128:9000/user/wangxiaoming/bankstatementconfig/bankstatementconfig.txt"};
        String path = ioArg[0];  
//        String confFile = args[1];  
        Configuration conf = new Configuration();   
//        FileInputStream fis = new FileInputStream(confFile);  
//        conf.addResource(fis); 
        conf.set("mapred.jop.tracker", "hdfs://192.168.233.128:9001");
        conf.set("fs.default.name", "hdfs://192.168.233.128:9000");
        System.setProperty("hadoop.home.dir", "D:\\hadoop-2.7.3");
        FileSystem fileSystem = FileSystem.get(conf);  
        FSDataInputStream fs = fileSystem.open(new Path(path));  
        //GBK
        BufferedReader bis = new BufferedReader(new InputStreamReader(fs,"GBK"));     
//        FileWriter fos = new FileWriter(args[2]);  
//        BufferedWriter bw = new BufferedWriter(fos);  
        String temp;  
        int i=0;  
        while ((temp = bis.readLine()) != null) {  
//            bw.write(temp);  
            System.out.println(temp);  
//            if(temp.startsWith("</doc>")) {  
            
//                break;  
//            }  
//            if(temp.indexOf("上海") > -1) {  
//                System.out.println("发现你了 coming");  
//                break;  
//            }  
        }         
//        bw.close();  
        bis.close();  
        fileSystem.close();  
    }  
  
}  

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wangxiaoming

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值