码迷,mamicode.com
首页 > 编程语言 > 详细

用java从hadoop中读数据

时间:2015-09-21 16:06:36      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

如果用eclipse 连接hadoop测试 一定要把core-site.xml  hdfs-site.xml 放到和包test同目录下 不然会报错程序会报File not found错误,并且加载相对应版本的的hadoop-core-xx.jar包

package test;
import java.io.*;
import java.util.*;
import java.net.*;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.util.*;

public class Cat{
        public static void main (String [] args) throws Exception{
                try{
                        Path pt=new Path("hdfs://10.14.2.201:9000/user/hadoop/words.txt");
                        //Path pt=new Path("hdfs://npvm11.np.wc1.yellowpages.com:9000/user/john/abc.txt");
                        FileSystem fs = FileSystem.get(new Configuration());
                        BufferedReader br=new BufferedReader(new InputStreamReader(fs.open(pt)));
                        String line;
                        line=br.readLine();
                        long sum = 0L;
                        while (line != null){
                                sum ++;
                                System.out.println(line+"sum: "+sum);
                                line=br.readLine();
                        }
                }catch(Exception e){
                    e.printStackTrace();
                }
        }
}

用java从hadoop中读数据

标签:

原文地址:http://my.oschina.net/u/2408078/blog/509106

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