zoukankan      html  css  js  c++  java
  • 集群使用初步

    5 集群使用初步

    5.1 HDFS使用

    1、查看集群状态

    命令:   hdfs  dfsadmin  –report

    可以看出,集群共有3datanode可用

    也可打开web控制台查看HDFS集群信息,在浏览器打开http://hdp-node-01:50070/

    2、上传文件到HDFS

    ² 查看HDFS中的目录信息

    命令   hadoop  fs  –ls  /

    ² 上传文件

    命令:   hadoop  fs  -put  ./ scala-2.10.6.tgz  to  /

    ² HDFS下载文件

    命令:  hadoop  fs  -get  /yarn-site.xml

    5.2 MAPREDUCE使用

    mapreducehadoop中的分布式运算编程框架,只要按照其编程规范,只需要编写少量的业务逻辑代码即可实现一个强大的海量数据并发处理程序

    5.2.1 Demo开发——wordcount

    1、需求

    从大量(比如T级别)文本文件中,统计出每一个单词出现的总次数

    2mapreduce实现思路

    Map阶段:

    a) HDFS的源数据文件中逐行读取数据

    b) 将每一行数据切分出单词

    c) 为每一个单词构造一个键值对(单词,1)

    d) 将键值对发送给reduce

    Reduce阶段

    a) 接收map阶段输出的单词键值对

    b) 将相同单词的键值对汇聚成一组

    c) 对每一组,遍历组中的所有“值”,累加求和,即得到每一个单词的总次数

    d) (单词,总次数)输出到HDFS的文件中

    1、 具体编码实现

    (1)定义一个mapper

    //首先要定义四个泛型的类型

    //keyin:  LongWritable    valuein: Text

    //keyout: Text            valueout:IntWritable

    public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable>{

    //map方法的生命周期:  框架每传一行数据就被调用一次

    //key :  这一行的起始点在文件中的偏移量

    //value: 这一行的内容

    @Override

    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

    //拿到一行数据转换为string

    String line = value.toString();

    //将这一行切分出各个单词

    String[] words = line.split(" ");

    //遍历数组,输出<单词,1>

    for(String word:words){

    context.write(new Text(word), new IntWritable(1));

    }

    }

    }

    (2)定义一个reducer

    //生命周期:框架每传递进来一个kv 组,reduce方法被调用一次

    @Override

    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {

    //定义一个计数器

    int count = 0;

    //遍历这一组kv的所有v,累加到count

    for(IntWritable value:values){

    count += value.get();

    }

    context.write(key, new IntWritable(count));

    }

    }

    (3)定义一个主类,用来描述job并提交job

    public class WordCountRunner {

    //把业务逻辑相关的信息(哪个是mapper,哪个是reducer,要处理的数据在哪里,输出的结果放哪里。。。。。。)描述成一个job对象

    //把这个描述好的job提交给集群去运行

    public static void main(String[] args) throws Exception {

    Configuration conf = new Configuration();

    Job wcjob = Job.getInstance(conf);

    //指定我这个job所在的jar

    // wcjob.setJar("/home/hadoop/wordcount.jar");

    wcjob.setJarByClass(WordCountRunner.class);

    wcjob.setMapperClass(WordCountMapper.class);

    wcjob.setReducerClass(WordCountReducer.class);

    //设置我们的业务逻辑Mapper类的输出keyvalue的数据类型

    wcjob.setMapOutputKeyClass(Text.class);

    wcjob.setMapOutputValueClass(IntWritable.class);

    //设置我们的业务逻辑Reducer类的输出keyvalue的数据类型

    wcjob.setOutputKeyClass(Text.class);

    wcjob.setOutputValueClass(IntWritable.class);

    //指定要处理的数据所在的位置

    FileInputFormat.setInputPaths(wcjob, "hdfs://hdp-server01:9000/wordcount/data/big.txt");

    //指定处理完成之后的结果所保存的位置

    FileOutputFormat.setOutputPath(wcjob, new Path("hdfs://hdp-server01:9000/wordcount/output/"));

    //yarn集群提交这个job

    boolean res = wcjob.waitForCompletion(true);

    System.exit(res?0:1);

    }

    5.2.2 程序打包运行

    1. 将程序打包

    2. 准备输入数据

    vi  /home/hadoop/test.txt

    Hello tom

    Hello jim

    Hello ketty

    Hello world

    Ketty tom

    hdfs上创建输入数据文件夹

    hadoop   fs  mkdir  -p  /wordcount/input

    words.txt上传到hdfs

    hadoop  fs  –put  /home/hadoop/words.txt  /wordcount/input

    3. 将程序jar包上传到集群的任意一台服务器上

    4. 使用命令启动执行wordcount程序jar

    $ hadoop jar wordcount.jar cn.itcast.bigdata.mrsimple.WordCountDriver /wordcount/input /wordcount/out

    5. 查看执行结果

    $ hadoop fs –cat /wordcount/out/part-r-00000

  • 相关阅读:
    0129 System类 Math类 Arrays类 大数据运算
    0127 基本类型包装类
    'telnet' 不是内部或外部命令,也不是可运行的程序 解决方案
    删除时报org.springframework.dao.DataIntegrityViolationException
    mapper自动识别驼峰配置 spring MVC
    spring Security如何debug源码
    公司tomcat项目启动
    java.util.ConcurrentModificationException: null 异常解决
    @Transactional 学习
    mangoDB初探
  • 原文地址:https://www.cnblogs.com/burningmyself/p/7040108.html
Copyright © 2011-2022 走看看