免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 14258 | 回复: 3
打印 上一主题 下一主题

hadoop如何实现关联计算 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-11-25 16:24 |只看该作者 |倒序浏览
    选择Hadoop,低成本和高扩展性是主要原因,但但它的开发效率实在无法让人满意。

    以关联计算为例。
    假设:HDFS上有2个文件,分别是客户信息和订单信息,customerID是它们之间的关联字段。如何进行关联计算,以便将客户名称添加到订单列表中?
    一般方法是:输入2个源文件。根据文件名在Map中处理每条数据,如果是Order,则在foreign key上加标记”O”,形成combined key;如果是Customer则做标记”C”。Map之后的数据按照key分区,再按照combined key分组排序。最后在reduce中合并结果再输出。
实现代码:
public static class JMapper extends Mapper<LongWritable, Text, TextPair, Text> {
    //mark every row with "O" or "C" according to file name
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
    String pathName = ((FileSplit) context.getInputSplit()).getPath().toString();
    if (pathName.contains("order.txt")) {//identify order by file name
            String values[] = value.toString().split("\t");
            TextPair tp = new TextPair(new Text(values[1]), new Text("O"));//mark with "O"
            context.write(tp, new Text(values[0] + "\t" + values[2]));
        }
   if (pathName.contains("customer.txt")) {//identify customer by file name
           String values[] = value.toString().split("\t");
           TextPair tp = new TextPair(new Text(values[0]), new Text("C"));//mark with "C"
           context.write(tp, new Text(values[1]));
        }
    }
}
public static class JPartitioner extends Partitioner<TextPair, Text> {
    //partition by key, i.e. customerID
    @Override
    public int getPartition(TextPair key, Text value, int numParititon) {
        return Math.abs(key.getFirst().hashCode() * 127) % numParititon;
    }
}
public static class JComparator extends WritableComparator {
    //group by muti-key
    public JComparator() {
        super(TextPair.class, true);
    }
    @SuppressWarnings("unchecked")
    public int compare(WritableComparable a, WritableComparable b) {
        TextPair t1 = (TextPair) a;
        TextPair t2 = (TextPair) b;
        return t1.getFirst().compareTo(t2.getFirst());
    }
}
public static class JReduce extends Reducer<TextPair, Text, Text, Text> {
    //merge and output
    protected void reduce(TextPair key, Iterable<Text> values, Context context) throws IOException,InterruptedException {
    Text pid = key.getFirst();
    String desc = values.iterator().next().toString();
    while (values.iterator().hasNext()) {
        context.write(pid, new Text(values.iterator().next().toString() + "\t" + desc));
   }
    }
}
public class TextPair implements WritableComparable<TextPair> {
    //make muti-key
    private Text first;
    private Text second;
    public TextPair() {
        set(new Text(), new Text());
    }
    public TextPair(String first, String second) {
        set(new Text(first), new Text(second));
    }
    public TextPair(Text first, Text second) {
        set(first, second);
    }
    public void set(Text first, Text second) {
  this.first = first;
  this.second = second;
    }
    public Text getFirst() {
  return first;
    }
    public Text getSecond() {
  return second;
    }
    public void write(DataOutput out) throws IOException {
  first.write(out);
  second.write(out);
    }
    public void readFields(DataInput in) throws IOException {
  first.readFields(in);
  second.readFields(in);
    }
    public int compareTo(TextPair tp) {
  int cmp = first.compareTo(tp.first);
  if (cmp != 0) {
       return cmp;
  }
    return second.compareTo(tp.second);
    }
}
public static void main(String agrs[]) throws IOException, InterruptedException, ClassNotFoundException {
    //job entrance
    Configuration conf = new Configuration();
    GenericOptionsParser parser = new GenericOptionsParser(conf, agrs);
    String[] otherArgs = parser.getRemainingArgs();
    if (agrs.length < 3) {
   System.err.println("Usage: J <in_path_one> <in_path_two> <output>");
   System.exit(2);
    }
    Job job = new Job(conf, "J");
    job.setJarByClass(J.class);//Join class
    job.setMapperClass(JMapper.class);//Map class
    job.setMapOutputKeyClass(TextPair.class);//Map output key class
    job.setMapOutputValueClass(Text.class);//Map output value class
    job.setPartitionerClass(JPartitioner.class);//partition class
    job.setGroupingComparatorClass(JComparator.class);//condition group class after partition
    job.setReducerClass(Example_Join_01_Reduce.class);//reduce class
    job.setOutputKeyClass(Text.class);//reduce output key class
    job.setOutputValueClass(Text.class);//reduce ouput value class
    FileInputFormat.addInputPath(job, new Path(otherArgs[0]));//one of source files
    FileInputFormat.addInputPath(job, new Path(otherArgs[1]));//another file
    FileOutputFormat.setOutputPath(job, new Path(otherArgs[2]));//output path
    System.exit(job.waitForCompletion(true) ? 0 : 1);//run untill job ends
}

    不能直接使用原始数据,而是要搞一堆代码处理标记,并绕过MapReduce原本的架构,最后从底层设计并计算数据之间的关联关系。这还是最简单的关联计算,如果用MapReduce进行多表关联或逻辑更复杂的关联计算,复杂度会呈几何级数递增。

欢迎访问我的blog,大数据相关特别是hive:http://blog.chinaunix.net/uid/29242841.html

论坛徽章:
0
2 [报告]
发表于 2013-12-16 23:20 |只看该作者
                                100% pass ,220-701 - CompTIA A+ Essentials (2009 Edition) Certification Exam
100% pass ,220-702 - CompTIA A+ Practical Application (2009 Edition) Certification Exam
100% pass ,220-801 - CompTIA A+ Certification Exam
100% pass ,220-802 - CompTIA A+ Certification Exam
100% pass ,225-030 - CompTIA CDIA+ Certification Exam
100% pass ,ADR-001 - CompTIA Mobile App Security+ Certification Exam (Android Edition)
100% pass ,CAS-001 - CompTIA Advanced Security Practitioner (CASP) Exam
100% pass ,CD0-001 - CompTIA CDIA+ Certification Exam
100% pass ,CLO-001 - CompTIA Cloud Essentials Exam

100% guaranteed pass ,Once failed 100% refund
The Fastest and Guaranteed Way to Certify Now
[url]http://www.eliteguides.cn[/url]
certification,certify,certified,braindumps,braindump,dumps,exam,exams,study guides,study guide,test,tests,testing,training,free,book,pdf,question,questions

100% pass ,CV0-001 - CompTIA Cloud+ Certification Exam
100% pass ,FC0-101 - CompTIA Strata PC Hardware Engineer Exam
100% pass ,FC0-GR1 - CompTIA Strata Green IT Exam
100% pass ,FC0-TS1 - CompTIA IT for Sales Specialist Exam
100% pass ,FC0-U11 - CompTIA Strata Fundamentals of PC Functionality Exam
100% pass ,FC0-U21 - CompTIA Strata Fundamentals of PC Technology Exam
100% pass ,FC0-U41 - CompTIA Strata IT Fundamentals Exam
100% pass ,HIT-001 - CompTIA Healthcare IT Technician Exam
100% pass ,iCLO-001 - Invitation Only - CompTIA Cloud Essentials Exam
100% pass ,iFC0-U41 - Invitation Only - CompTIA Strata IT Fundamentals Exam
100% pass ,IOS-001 - CompTIA Mobile App Security+ Certification Exam (iOS Edition)
100% pass ,ISS-001 - Intel? Server Specialist Certification Exam
100% pass ,JK0-017 - CompTIA Academic/E2C Project+ Certification Exam
100% pass ,JK0-018 - CompTIA Academic/E2C Security+ Certification Exam

100% guaranteed pass ,Once failed 100% refund
The Fastest and Guaranteed Way to Certify Now
[url]http://www.eliteguides.cn[/url]
certification,certify,certified,braindumps,braindump,dumps,exam,exams,study guides,study guide,test,tests,testing,training,free,book,pdf,question,questions

100% pass ,JK0-019 - CompTIA Academic/E2C Network+ Certification Exam
100% pass ,JK0-020 - CompTIA Academic/E2C Cloud+ Certification Exam
100% pass ,JK0-701 - CompTIA Academic/E2C A+ Essentials (2009 Edition) Exam
100% pass ,JK0-702 - CompTIA Academic/E2C A+ Practical Application (2009 Edition) Exam
100% pass ,JK0-801 - CompTIA Academic/E2C A+ Certification Exam
100% pass ,JK0-802 - CompTIA Academic/E2C A+ Certification Exam
100% pass ,JK0-U11 - CompTIA Academic/E2C Strata Fundamentals of PC Functionality Exam
100% pass ,JK0-U21 - CompTIA Academic/E2C Strata Fundamentals of PC Technology Exam
100% pass ,JK0-U31 - CompTIA Academic/E2C Strata IT Fundamentals Exam
100% pass ,LX0-101 - CompTIA Linux+ [Powered by LPI] Exam 1
100% pass ,LX0-102 - CompTIA Linux+ [Powered by LPI] Exam 2
100% pass ,MB0-001 - CompTIA Mobility+ Certification Exam
100% pass ,N10-005 - CompTIA Network+ Certification Exam
100% pass ,PD0-001 - CompTIA PDI+ Certification Exam

100% guaranteed pass ,Once failed 100% refund
The Fastest and Guaranteed Way to Certify Now
[url]http://www.eliteguides.cn[/url]
certification,certify,certified,braindumps,braindump,dumps,exam,exams,study guides,study guide,test,tests,testing,training,free,book,pdf,question,questions

100% pass ,PK0-003 - CompTIA Project+ Certification Exam
100% pass ,SG0-001 - CompTIA Storage+ Powered By SNIA Certification Exam
100% pass ,SK0-003 - CompTIA Server+ Certification Exam
100% pass ,SY0-301 - CompTIA Security+ Certification Exam
100% pass ,TE0-301 - OS X Directory Services Specialist Certification Exam
100% pass ,TE0-302 - OS X Deployment Specialist Certification Exam
100% pass ,TE0-303 - OS X Mobile Device and Profile Specialist Certification Exam
100% pass ,TK0-201 - CompTIA CTT+ Essentials Certification Exam
100% pass ,UK0-001 - UKI Social Media Security Professional (SMSP) Certification exam
100% pass ,zUK1-001 - Invitation Only - UKI Social Media Professional (SMP) Certification Beta Exam

100% guaranteed pass ,Once failed 100% refund
The Fastest and Guaranteed Way to Certify Now
[url]http://www.eliteguides.cn[/url]
certification,certify,certified,braindumps,braindump,dumps,exam,exams,study guides,study guide,test,tests,testing,training,free,book,pdf,question,questions
           
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP