【大数据实验】HDFS的应用:数字排序

目录

实验内容

实验步骤 

实验源码 

实验结果

实验报告


实验内容

熟悉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();
        }
    }
}

实验结果

实验报告

《大数据导论》HDFS的应用.docx-Hadoop文档类资源-CSDN下载

  • 1
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

二琳爱吃肉

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

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

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

打赏作者

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

抵扣说明:

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

余额充值