赞
踩
目录
熟悉hadoop文件命令,熟悉在Linux编写、编译、运行Java程序的过程,实现文件的读写操作,读出数据并排序。
启动hadoop
cd /usr/local/hadoop
./sbin/start-dfs.sh判断hadoop是否启动成功,成功会有显示
jps查看jar包
cd /usr/local/hadoop/myapp
ls上传文件test1.txt 和test2.txt将其存放在input中,test1.txt test2.txt存放需要排序的无序数字。
./bin/hdfs dfs -put /home/hadoop/test3.txt input查看hadoop目录 查看input文件夹下的内容 看test1.txt test2.txt是否成功上传
./bin/hdfs dfs -ls
./bin/hdfs dfs -ls input删除output文件夹,不删除会报错
./bin/hdfs dfs -rm -r output./运行实验2程序
./bin/hadoop jar ./myapp/shiyan2.jar
显示运行结果
./bin/hdfs dfs -cat test3.txt关闭hadoop
./sbin/stop-dfs.sh
- package shiyan;
-
- import java.io.*;
- import java.nio.charset.StandardCharsets;
- import java.util.*;
-
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.fs.FileSystem;
- import org.apache.hadoop.fs.Path;
- import org.apache.hadoop.fs.FSDataInputStream;
- import org.apache.hadoop.fs.FSDataOutputStream;
-
- import static java.lang.Integer.*;
-
- public class shiyan2 {
- public static void main(String[] args){
- try{
- ArrayList<Integer> a = new ArrayList<Integer>();
- ArrayList<Integer> b = new ArrayList<Integer>();
-
- Configuration conf = new Configuration();
- conf.set("fs.defaultFS","hdfs://localhost:9000");
- conf.set("fs.hdfs.impl","org.apache.hadoop.hdfs.DistributedFileSystem");
- FileSystem fs = FileSystem.get(conf);
- Path file = new Path("test1.txt");
- FSDataInputStream getit = fs.open(file);
- BufferedReader d = new BufferedReader(new InputStreamReader(getit));
-
- Path file1 = new Path("test2.txt");
- FSDataInputStream getit1 = fs.open(file1);
- BufferedReader d1 = new BufferedReader(new InputStreamReader(getit1));
- String str;
- while((str = d.readLine())!= null)
- {
- a.add(Integer.valueOf(str));
- }
- while((str = d1.readLine())!= null)
- {
- a.add(Integer.valueOf(str));
- }
- d.close();
- d1.close();
- a.sort(Comparator.naturalOrder());
- for (int i : a){
- System.out.println(i);
- }
-
-
- String filename = "test3.txt";
- FSDataOutputStream os = fs.create(new Path(filename));
- for(Integer i :a){
- os.write(i.toString().getBytes());
- os.write("\r\n".getBytes());
- }
- os.close();
- fs.close();
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。