• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
导航菜单
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • windows
  • 服务器硬件
  • 服务器运维
  • 云计算
  • 虚拟化
  • IIS教程
  • Linux
  • Apache
  • Ftp
  • DNS
  • Nginx
您的位置:首页 > 服务器 >云计算 > MapReduce编程之实现多表关联,mapreduce编程关联

MapReduce编程之实现多表关联,mapreduce编程关联

作者:网友 字体:[增加 减小] 来源:互联网

本文主要包含mapreduce编程实例,mapreduce编程模型,mapreduce编程,mapreduce编程模板,mapreduce编程案例等服务器相关知识,网友希望可以进行参考

MapReduce编程之实现多表关联,mapreduce编程关联


多表关联和单表关联类似,它也是通过对原始数据进行一定的处理,从其中挖掘出关心的信息。如下

输入的是两个文件,一个代表工厂表,包含工厂名列和地址编号列;另一个代表地址表,包含地址名列和地址编号列。

要求从输入数据中找出工厂名和地址名的对应关系,输出工厂名-地址名表

样本如下:

factory:

<span style="font-size:14px;">factoryname addressed
Beijing Red Star 1
Shenzhen Thunder 3
Guangzhou Honda 2
Beijing Rising 1
Guangzhou Development Bank 2
Tencent 3
Back of Beijing 1
</span>

address:

<span style="font-size:14px;">addressID addressname
1 Beijing
2 Guangzhou
3 Shenzhen
4 Xian
</span>


结果:

<span style="font-size:14px;">factoryname     addressname
Beijing Red Star        Beijing
Beijing Rising  Beijing
Bank of Beijing         Beijing
Guangzhou Honda         Guangzhou
Guangzhou Development Bank      Guangzhou
Shenzhen Thunder        Shenzhen
Tencent         Shenzhen
</span>


代码如下:

<span style="font-size:14px;">import java.io.IOException;
import java.util.*;

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;
import org.apache.hadoop.util.GenericOptionsParser;

 
public class MTjoin {

 
    public static int time = 0;

    /*
     * 在map中先区分输入行属于左表还是右表,然后对两列值进行分割,
     * 保存连接列在key值,剩余列和左右表标志在value中,最后输出
     */

    public static class Map extends Mapper<Object, Text, Text, Text> {

        // 实现map函数</span>
<span style="font-size:14px;">        public void map(Object key, Text value, Context context)
                throws IOException, InterruptedException {
            String line = value.toString();// 每行文件
            String relationtype = new String();// 左右表标识 

            // 输入文件首行,不处理

            if (line.contains("factoryname") == true
                    || line.contains("addressed") == true) {
                return;
            }

            // 输入的一行预处理文本

            StringTokenizer itr = new StringTokenizer(line);
            String mapkey = new String();
            String mapvalue = new String();
            int i = 0;
            while (itr.hasMoreTokens()) {

                // 先读取一个单词

                String token = itr.nextToken();
                // 判断该地址ID就把存到"values[0]"
                if (token.charAt(0) >= '0' && token.charAt(0) <= '9') {
                    mapkey = token;
                    if (i > 0) {
                        relationtype = "1";
                    } else {
                        relationtype = "2";
                    }
                    continue;

                }

 

                // 存工厂名

                mapvalue += token + " ";

                i++;

            }

            // 输出左右表

            context.write(new Text(mapkey), new Text(relationtype + "+"+ mapvalue));

        }

    }

 
    /*
     * reduce解析map输出,将value中数据按照左右表分别保存,
   * 然后求出笛卡尔积,并输出。
     */

    public static class Reduce extends Reducer<Text, Text, Text, Text> {

 

        // 实现reduce函数

        public void reduce(Text key, Iterable<Text> values, Context context)

                throws IOException, InterruptedException {

 

            // 输出表头

            if (0 == time) {

                context.write(new Text("factoryname"), new Text("addressname"));
                time++;

            }

 

            int factorynum = 0;

            String[] factory = new String[10];
            int addressnum = 0;
            String[] address = new String[10];

            Iterator ite = values.iterator();

            while (ite.hasNext()) {

                String record = ite.next().toString();

                int len = record.length();

                int i = 2;

                if (0 == len) {

                    continue;

                }

 

                // 取得左右表标识

                char relationtype = record.charAt(0);

 

                // 左表

                if ('1' == relationtype) {

                    factory[factorynum] = record.substring(i);

                    factorynum++;

                }

 

                // 右表

                if ('2' == relationtype) {

                    address[addressnum] = record.substring(i);

                    addressnum++;

                }

            }

 

            // 求笛卡尔积

            if (0 != factorynum && 0 != addressnum) {

                for (int m = 0; m < factorynum; m++) {

                    for (int n = 0; n < addressnum; n++) {

                        // 输出结果

                        context.write(new Text(factory[m]),

                                new Text(address[n]));

                    }

                }

            }

 

        }

    }

 

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

        Configuration conf = new Configuration();

        // 这句话很关键

  //      conf.set("mapred.job.tracker", "192.168.1.2:9001");

 
	//可使用args
//      String[] ioArgs = new String[] { "MTjoin_in", "MTjoin_out" };

        String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();

        if (otherArgs.length != 2) {

            System.err.println("Usage: Multiple Table Join <in> <out>");
            System.exit(2);

        }

        Job job = new Job(conf, "Multiple Table Join");
        job.setJarByClass(MTjoin.class);

        // 设置Map和Reduce处理类

        job.setMapperClass(Map.class);
        job.setReducerClass(Reduce.class);

        // 设置输出类型

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);

 

        // 设置输入和输出目录

        FileInputFormat.addInputPath(job, new Path(otherArgs[0]));

        FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));

        System.exit(job.waitForCompletion(true) ? 0 : 1);

    }

}

</span>
<span style="font-size:14px;">javac -classpath hadoop-core-1.1.2.jar:/opt/hadoop-1.1.2/lib/commons-cli-1.2.jar -d firstProject firstProject/MTJoin.java
</span>
<span style="font-size:14px;">jar -cvf MTJoin.jar -C firstProject/ .     </span>
<span style="font-size:14px;">
</span>

删除已经存在的output

<span style="font-size:14px;">hadoop fs -rmr output
</span>
<span style="font-size:14px;">hadoop fs -mkdir input
</span>
<span style="font-size:14px;">hadoop fs -put factory input
</span>
<span style="font-size:14px;"> hadoop fs -put address input
</span>

运行

<span style="font-size:14px;">hadoop jar  MTJoin.jar MTJoin input output
</span>


查看结果

<sp
  


 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • 高阶MapReduce_3_reducer侧联结原理,mapreduce原理
  • MapReduce编程之实现多表关联,mapreduce编程关联
  • 分析MapReduce与Storm的异同,mapreducestorm
  • MapReduce排序及实例,MapReduce排序实例
  • MapReduce之RecordReader组件源码解析及实例,mapreduce实例
  • MapReduce编程之倒排索引,mapreduce编程索引
  • Hadoop MapReduce编程的一些个人理解,hadoopmapreduce
  • MapReduce处理表的自连接,mapreduce处理表

相关文章

  • 如何配置和使用Pivotal Cloud Foundry里的HAPorxy(下),pivotalhaporxy
  • 启动hive报错[ERROR] Terminal initialization failed; falling back to unsupported,hiveunsupported
  • 《转》ceilometer的数据采集机制入门,《转》ceilometer
  • python RESTful API框架:Eve 快速入门,pythonrestful
  • MapReduce处理表的自连接,mapreduce处理表
  • 分享Hadoop大数据高薪就业视频,分享hadoop高薪就业
  • 各种分布式文件系统的比较,文件
  • 你真的很熟分布式处理和事务吗?,事务
  • Andrew Ng机器学习课程10,andrewng机器课程
  • HIVE以及OOZIE添加第三方JAR包的方法,hiveoozie

文章分类

  • windows
  • 服务器硬件
  • 服务器运维
  • 云计算
  • 虚拟化
  • IIS教程
  • Linux
  • Apache
  • Ftp
  • DNS
  • Nginx

最近更新的内容

    • 如何解决Greenplum中无法通过标准命令修复的元数据错误,greenplum元数据
    • Spark Core and Cluster Managers(翻译自Learning.Spark.Lightning-Fast.Big.Data.Analysis),
    • sbt pom reader,sbtpom
    • 机器学习-统计学习方法概论,学习方法概论
    • MapReduce处理输出多文件格式(MultipleOutputs),mapreduce多文件输出
    • **FASPOT 功能简介**,faspot功能简介
    • Hadoop中查看一个文件位置信息,
    • 最小 Docker 镜像 hello-world 剖析,dockerhello-world
    • spark on yarn的cpu使用,sparkonyarncpu
    • R语言数据分析系列之一,数据分析系列之一

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有