Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2134588
  • 博文数量: 276
  • 博客积分: 5998
  • 博客等级: 大校
  • 技术积分: 5175
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-24 14:43
文章分类

全部博文(276)

文章存档

2014年(25)

2013年(11)

2012年(69)

2011年(167)

2010年(4)

分类: HADOOP

2014-03-24 16:15:57


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




点击(此处)折叠或打开

  1. package test;
  2. import java.io.*;
  3. import java.util.*;
  4. import java.net.*;

  5. import org.apache.hadoop.fs.*;
  6. import org.apache.hadoop.conf.*;
  7. import org.apache.hadoop.io.*;
  8. import org.apache.hadoop.mapred.*;
  9. import org.apache.hadoop.util.*;
  10.  
  11. public class Write{
  12.         
  13.         public static void main (String [] args) throws Exception{
  14.                 try{
  15.                         Path pt=new Path("hdfs://10.14.2.201:9000/user/hadoop/words.txt");
  16.                         FileSystem fs = FileSystem.get(new Configuration());
  17.                         BufferedWriter br=new BufferedWriter(new OutputStreamWriter(fs.create(pt,true)));
  18.                         //BufferedWriter br=new BufferedWriter(new OutputStreamWriter(fs.append(pt)));
  19.                         // TO append data to a file, use fs.append(Path f)
  20.                        
  21.                         
  22.                         String line;
  23.                         //line="lalala";
  24.                         
  25.                         String[] lines = {"aaa","bbb","ccc","ddd","eee","fff","ggg","hhh","iii","jjj","kkk","lll","nnn"};
  26.                         Random random=new Random();
  27.                        
  28.                         for (int i = 0;i < 100;i++){
  29.                             //Thread.sleep(20000);
  30.                             
  31.                             System.out.println(lines[random.nextInt(13)]);
  32.                             System.out.println(i);
  33.                             br.write(lines[random.nextInt(13)]+"\n");
  34.                             
  35.                             
  36.                         }
  37.                       
  38.                        //System.out.println(line);
  39.                        //br.write(line);
  40.                        br.close();
  41.                 }catch(Exception e){
  42.                         System.out.println("File not found");
  43.                         e.printStackTrace();
  44.                 }
  45.         }
  46. }


阅读(3184) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~