• 7月17号day9总结


    今天学习过程和小结

    今天学习了如何使用idea操作hdfs

    public class HDFSTest {
        Configuration configuration;
        FileSystem fileSystem;

        String HDFS_PATH="hdfs://192.168.122.141:9000";
        @Before
        public void beform() throws Exception{
            configuration=new Configuration();
            fileSystem=FileSystem.get(new URI(HDFS_PATH),configuration,"root");

        }
        //hdfs上创建目录
        @Test
        public void mkdir() throws IOException{
            boolean result=fileSystem.mkdirs(new Path("/idea"));
                System.out.println(result);

        }

        //hdfs上创建文件
        @Test
        public void createFile() throws IOException{
            FSDataOutputStream fsDataOutputStream =fileSystem.create(new Path("/idea/hello.txt"));
            fsDataOutputStream.write("hello,idea".getBytes());
            fsDataOutputStream.close();
        }

        //查看HDFS上的文件内容/idea/hello.txt
        @Test
        public  void  readFromHDFS() throws  IOException{
            FSDataInputStream fsDataInputStream= fileSystem.open(new Path("/idea/hello.txt"));
            //打印到控制台
            IOUtils.copyBytes(fsDataInputStream,System.out,1024);
            fsDataInputStream.close();
        }

        //文件重命名
        @Test
        public void rename() throws IOException{
            Path oldFilePath=new Path("/idea/hello.txt");
            Path newFilePath=new Path("/idea/h.txt");
            fileSystem.rename(oldFilePath,newFilePath);
        }

        //上传文件到hdfs
        @Test
        public  void  uploadToHDFS() throws IOException{
            Path localPath=new Path("E:/abc.txt");
            Path hdfsPath=new Path("/idea/");
            fileSystem.copyFromLocalFile(localPath,hdfsPath);

        }

        //下载HDFS到本地
        @Test
        public  void downloadToLocal() throws IOException{

            Path hdfsPath=new Path("/idea/h.txt");
            Path localPath=new Path("E:/hello.txt");
            fileSystem.copyToLocalFile(hdfsPath,localPath);
        }

        //查询目录下的所有文件
        @Test
        public  void  list() throws IOException{
            Path distPath=new Path("/");
            FileStatus[] fileStatuses=fileSystem.listStatus(distPath);
            for(FileStatus fileStatus:fileStatuses){
                String path= fileStatus.getPath().toString();
                String  status=fileStatus.isDirectory()?"目录":"文件";
                System.out.println(path);
                System.out.println(status);
            }
        }

        //删除hdfs上的文件
        @Test
        public void  deleteFile() throws IOException{
            Path path=new Path("/idea/h.txt");
            fileSystem.delete(path,true);
        }

        //带进度条文件上传
        @Test
        public void uploadFileByProcessbar() throws IOException{
            InputStream inputStream=new BufferedInputStream(new FileInputStream("E:/feiq/feiq/Recv Files/javaHDFS.wmv"));
            FSDataOutputStream fsDataOutputStream=fileSystem.create(new Path("/idea/javaHDFS.wmv "), new Progressable() {
                @Override
                public void progress() {
                    System.out.print(".");
                }
            });
            IOUtils.copyBytes(inputStream,fsDataOutputStream,4096);
        }
     
        @After
        public void destory(){

        }
    }

    学习了MapReduce

    分布式计算框架MapReduce

    1.MapReduce概述

    MapReduce优点:海量数据离线处理&易开发&易运行

    MapReduce缺点:无法实时流式计算

    1. MapReduce编程模型

    3.案例:TopN问题

         词频统计wordcount

          cat.txt内容如下:

          hadoop  welcome

          hadoop  hdfs mapreduce

          hadoop hdfs

         需求:统计每个单词出现的次数

    MapReduce编程模型之Map阶段和Reduce阶段

    将作业拆分成Map阶段和Reduce阶段

    Map阶段:Map Tasks

    Reduce阶段:Reduce Tasks

    MapReduce编程模型之执行步骤

    1,通过InputFormat讲文件读入并拆分成split

    2,通过RecordReaderssplit中的数据读入,并交给map处理。

    3map处理后的结果按照partitioner进行分区,然后将数据发送到对应的reduce上处理

    4reduce处理完成后,由outputFomat将结果写到文件系统

    核心概念

    Split:交由MapReduce作业来处理的数据块,是MapReduce中最小的计算单元。

    InputFormat:将输入数据进行分片(split):InputSplit[] getSplits(JobConf job)

    TextInputFormat:处理文本格式数据

    OutputFormat:输出

    Combiner

    Partitioner

    3.MapReduce架构

       MapReduce架构之MapReduce1.x

    JobTracker:JT

                作业的管理者,将作业分解成一堆任务:Task(MapTaskReduceTask),将任务分派给TaskTracker运行

                 作业的监控、容错处理(task作业挂了,重启task的机制)

                  在一定的时间间隔内,JT没有收到TT的心跳信息,TT可能挂了,TT上运行的任务会被指派到其它TT上去执行

    TaskTrackerTT

         任务的执行者  干活的

         TT上执行我们的Task(MapTaskReduceTask)

    MapTask

                自己开发的map任务交给Task

                解析每条记录的数据,交个自己的map方法处理

               map的输出结果写到本地磁盘(有些作业仅有map没有reduce====>HDFS

    ReduceTask

            MapTask输出的数据进行读取

           按照数据进行分组传给我们自己编写的reduce方法处理

           输出结果到HDFS

    MapReduce架构之MapReduce2.x

    1. MapReduce

    练习了wordcount的计算。

    又学习了倒排索引。

    遇到的问题汇总

    1. 对于hdfs的文件输入输出还不是很熟悉要多加练习。
    2. WordcountMapReduce方法中的基础计算,要熟悉代码的书写才可以。

    3.掌握了基本的单词个数统计,对于更难一点的key,value.也要能熟悉它的数据流动分析过程才可以。

    学习技能思维导图

  • 相关阅读:
    be accustomed to doing|actual |acute|adapt |
    By virtue of|sustain|post |scrape off |stretch|access to|take into account of|exploit|hasten|blur |idle|bored her to|account for|accused of|cruelty
    Block to|wreck|Range|Reach|span|chase around|amuse|exploit |instructed
    blast -m1
    qdel
    uname|mv|tar -xzvf|
    Hypothesis Tests for One Population Mean When σ Is Unknown|other
    dll显示调用和隐式调用
    使用预编译头提高编译速度
    NMS过程图示
  • 原文地址:https://www.cnblogs.com/kangy123/p/9325174.html
Copyright © 2020-2023  润新知