zoukankan      html  css  js  c++  java
  • MapReduce插件的使用

    一、下载插件hadoop-eclipse-plugin-2.7.3,将插件放在eclipse的plugins目录下。

    链接地址:https://pan.baidu.com/s/1nuCoe0L#list/path=%2F

    二、Windows编译后的文件放在hadoop的bin目录下

    三、添加hadoop的环境变量

    HADOOP_HOME=D:hadoop-2.7.7
    Path=%HADOOP_HOME%in;%HADOOP_HOME%sbin; 

    右键我的电脑—>属性—>高级系统设置—>环境变量

    四、配置eclipse

    Window—>Preferences

    将电脑上hadoop的路径配置上去

    切换到Map/Reduce视图

    在控制台Map/Reduce Locations 下面右键—>New Hadoop location

    在HDFS上开放权限

    vim /opt/software/hadoop-2.7.7/etc/hadoop/hdfs-site.xml
    
    <property>
          <name>dfs.permission</name> 
          <value>false<alue>
    </property>

    完成后可以在eclipse上操作hdfs

    四、配置完成,用项目测试可用性。

    某村共有300户居民 因疫情原因隔离在家 现在要求代购下面的商品
    随机构造一些商品 数量随机
    1.洗漱用品 脸盆、杯子、牙刷和牙膏、毛巾、肥皂(洗衣服的)以及皂盒、洗发水和护发素、沐浴液...
    2.床上用品 比如枕头、枕套、枕巾、被子、被套、棉被、毯子、床垫、凉席等。
    3.家用电器 比如电磁炉、电饭煲、吹风机、电水壶、豆浆机、台灯等。
    4.厨房用品 比如锅、碗、瓢、盆、灶、所有的厨具,柴、米、油、盐、酱、醋

    1、新建BestBillPractice类生成每户具名的代购商品清单

    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    import java.io.UnsupportedEncodingException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    
    /**
     * 300户 每户都会有一个清单文件
     * 商品是随机  数量也是随机
     * 洗漱用品 脸盆、杯子、牙刷和牙膏、毛巾、肥皂(洗衣服的)以及皂盒、洗发水和护发素、沐浴液   [1-5之间]
     * 床上用品 比如枕头、枕套、枕巾、被子、被套、棉被、毯子、床垫、凉席   [0 1之间]
     * 家用电器 比如电磁炉、电饭煲、吹风机、电水壶、豆浆机、台灯等   [1-3之间]
     * 厨房用品 比如锅、碗、瓢、盆、灶   [1-2 之间]
     * 柴、米、油、盐、酱、醋 [1-6之间]  
     * 要生成300个文件 命名规则  1-300来表示 
     * @author Administrator
     *
     */
    public class BuildBill
    {
        private static Random random=new Random(); //要还是不要
        private static List<String> washList=new ArrayList<>();
        private static List<String> bedList=new ArrayList<>();
        private static List<String> homeList=new ArrayList<>();
        private static List<String> kitchenList=new ArrayList<>();
        private static List<String> useList=new ArrayList<>();
        
        static{
            washList.add("脸盆");
            washList.add("杯子");
            washList.add("牙刷");
            washList.add("牙膏");
            washList.add("毛巾");
            washList.add("肥皂");
            washList.add("皂盒");
            washList.add("洗发水");
            washList.add("护发素");
            washList.add("沐浴液");
            ///////////////////////////////
            bedList.add("枕头");
            bedList.add("枕套");
            bedList.add("枕巾");
            bedList.add("被子");
            bedList.add("被套");
            bedList.add("棉被");
            bedList.add("毯子");
            bedList.add("床垫");
            bedList.add("凉席");
            //////////////////////////////
            homeList.add("电磁炉");
            homeList.add("电饭煲");
            homeList.add("吹风机");
            homeList.add("电水壶");
            homeList.add("豆浆机");
            homeList.add("电磁炉");
            homeList.add("台灯");
            //////////////////////////
            kitchenList.add("");
            kitchenList.add("");
            kitchenList.add("");
            kitchenList.add("");
            kitchenList.add("");
            ////////////////////////
            useList.add("");
            useList.add("");
            useList.add("");
            useList.add("");
            useList.add("");
        }
        
        
        
        //确定是否需要 1/2 
            private static boolean iswant()
            {
                 int num=random.nextInt(1000);
                 if(num%2==0)
                 {
                     return true;
                 }
                 else
                 {
                     return false;
                 }
            }
            
            /**
             * 表示我要几个,返回一个小于sum的随机数
             * @param sum
             * @return
             */
            private static int wantNum(int sum)
            {
                return random.nextInt(sum);
            }
            
            public static void main(String[] args) {
                for(int i=1;i<=300;i++) //循环三百次,生成三百个清单
                {
                
                try
                {
                    //字节流
                    FileOutputStream out=new FileOutputStream(new File("D:\bill\"+i+".txt"));
                    //转换流  可以将字节流转换字符流  设定编码格式 
                    //字符流
                    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out,"UTF-8"));
                    
                    
                     boolean iswant1=iswant();//先确定是否需要
                        if(iswant1)
                        {
                            //我要几个 不能超过该类商品的总数目
                            int wantNum = wantNum(washList.size()+1);
                            //3
                            for(int j=0;j<wantNum;j++)
                            {
                            String product=washList.get(random.nextInt(washList.size()));//从列表里随机获取一个商品
                            writer.write(product+"	"+(random.nextInt(5)+1));
                            writer.newLine();
                            }
                       }
                        
                        boolean iswant2=iswant();
                        if(iswant2)
                        {
                            //我要几个 不能超过该类商品的总数目
                            int wantNum = wantNum(bedList.size()+1);
                            //3
                            for(int j=0;j<wantNum;j++)
                            {
                            String product=bedList.get(random.nextInt(bedList.size()));
                            writer.write(product+"	"+(random.nextInt(1)+1));
                            writer.newLine();
                            }
                       }
                        
                        boolean iswant3=iswant();
                        if(iswant3)
                        {
                            //我要几个 不能超过该类商品的总数目
                            int wantNum = wantNum(homeList.size()+1);
                            //3
                            for(int j=0;j<wantNum;j++)
                            {
                            String product=homeList.get(random.nextInt(homeList.size()));
                            writer.write(product+"	"+(random.nextInt(3)+1));
                            writer.newLine();
                            }
                       }
                        boolean iswant4=iswant();
                        if(iswant4)
                        {
                            //我要几个 不能超过该类商品的总数目
                            int wantNum = wantNum(kitchenList.size()+1);
                            //3
                            for(int j=0;j<wantNum;j++)
                            {
                            String product=kitchenList.get(random.nextInt(kitchenList.size()));
                            writer.write(product+"	"+(random.nextInt(2)+1));
                            writer.newLine();
                            }
                       }
                        
                        boolean iswant5=iswant();
                        if(iswant5)
                        {
                            //我要几个 不能超过该类商品的总数目
                            int wantNum = wantNum(useList.size()+1);
                            //3
                            for(int j=0;j<wantNum;j++)
                            {
                            String product=useList.get(random.nextInt(useList.size()));
                            writer.write(product+"	"+(random.nextInt(6)+1));
                            writer.newLine();
                            }
                       }
                        writer.flush();
                        writer.close();
                        
                } catch (FileNotFoundException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (UnsupportedEncodingException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                }
            }
    }

    生成了300个清单文档

    2、将300个文档上传到hdfs上的upload目录下(前提现在hdfs上新建upload目录)

    先给upload加权限:hadoop fs -chmod 777 /upload

    右键upload

    然后选取这300个文件上传

    3、新建Map/Reduce项目

    new—>other—>Map/Reduce Project

    在创建Mapper类,Reducer类和Driver类时可以直接new—>other—>你想新建的类

    BillMapper类:

    package com.blb.core;
    
    import java.io.IOException;
    
    import org.apache.hadoop.io.IntWritable;
    import org.apache.hadoop.io.LongWritable;
    import org.apache.hadoop.io.Text;
    import org.apache.hadoop.mapreduce.Mapper;
    
    public class BillMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
    
        public void map(LongWritable ikey, Text ivalue, Context context) throws IOException, InterruptedException {
            String line = ivalue.toString();
            String[] words=line.split("	");
            context.write(new Text(words[0]),new IntWritable(Integer.parseInt(words[1])));
        }
    
    }

    BillReducer类:

    package com.blb.core;
    
    import java.io.IOException;
    
    import org.apache.hadoop.io.IntWritable;
    import org.apache.hadoop.io.Text;
    import org.apache.hadoop.mapreduce.Reducer;
    
    public class BillReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
    
        public void reduce(Text _key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            // process values
            int sum=0;
            for (IntWritable val : values) {
                int i = val.get();
                sum+=i;
            }
            context.write(_key,new IntWritable(sum));
        }
    
    }

    BillDriver类:

    package com.blb.core;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.Path;
    import org.apache.hadoop.io.IntWritable;
    import org.apache.hadoop.io.Text;
    import org.apache.hadoop.mapreduce.Job;
    import org.apache.hadoop.mapreduce.Mapper;
    import org.apache.hadoop.mapreduce.Reducer;
    import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
    import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
    
    public class BillDriver {
    
        public static void main(String[] args) throws Exception {
            Configuration conf = new Configuration();
            conf.set("fs.defaultFS", "hdfs://192.168.0.32:9000");
            Job job = Job.getInstance(conf, "BillDriver");
            job.setJarByClass(BillDriver.class);
            // TODO: specify a mapper
            job.setMapperClass(BillMapper.class);
            // TODO: specify a reducer
            job.setReducerClass(BillReducer.class);
    
            // TODO: specify output types
            job.setOutputKeyClass(Text.class);
            job.setOutputValueClass(IntWritable.class);
    
            // TODO: specify input and output DIRECTORIES (not files)
            FileInputFormat.setInputPaths(job, new Path("/upload"));
            FileOutputFormat.setOutputPath(job, new Path("/out1"));
    
            if (!job.waitForCompletion(true))
                return;
        }
    
    }

    写完之后可以在eclipse上直接运行BillDriver类的main方法,效果与在linux上操作相同(linux上操作参见https://www.cnblogs.com/yangy1/p/12409738.html

    Run As—>Run on Hadoop

    可能出现权限异常 

    hadoop  fs -chmod 777 /

  • 相关阅读:
    创建Graphics对象与Pen对象
    GDI+图形图像处理技术——GDIPlus绘图基础
    WPF的组成架构
    文件监控只FileSystemWatcher控件
    文件夹选择之FolderBrowserDialog控件
    SaveFileDialog控件
    文件选择之OpenFileDialog控件
    编码与解码
    写一个翻译小工具
    【转】字符集与字符编码简介
  • 原文地址:https://www.cnblogs.com/yangy1/p/12420047.html
Copyright © 2011-2022 走看看