如果用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 Write{
-
-
public static void main (String [] args) throws Exception{
-
try{
-
Path pt=new Path("hdfs://10.14.2.201:9000/user/hadoop/words.txt");
-
FileSystem fs = FileSystem.get(new Configuration());
-
BufferedWriter br=new BufferedWriter(new OutputStreamWriter(fs.create(pt,true)));
-
//BufferedWriter br=new BufferedWriter(new OutputStreamWriter(fs.append(pt)));
-
// TO append data to a file, use fs.append(Path f)
-
-
-
String line;
-
//line="lalala";
-
-
String[] lines = {"aaa","bbb","ccc","ddd","eee","fff","ggg","hhh","iii","jjj","kkk","lll","nnn"};
-
Random random=new Random();
-
-
for (int i = 0;i < 100;i++){
-
//Thread.sleep(20000);
-
-
System.out.println(lines[random.nextInt(13)]);
-
System.out.println(i);
-
br.write(lines[random.nextInt(13)]+"\n");
-
-
-
}
-
-
//System.out.println(line);
-
//br.write(line);
-
br.close();
-
}catch(Exception e){
-
System.out.println("File not found");
-
e.printStackTrace();
-
}
-
}
-
}
阅读(3680) | 评论(0) | 转发(0) |