使用MongoDB MapReduce进行联接操作 [英] Join operation with MongoDB MapReduce

查看:79
本文介绍了使用MongoDB MapReduce进行联接操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用经典的MR操作之前,我一直在使用MapReduce,相当于SQL中的GROUP BY.

I've been using MapReduce before to perform classical MR operation, the equivalent of GROUP BY in SQL.

我想知道使用MapReduce执行JOIN操作在概念上是否可行.知道如何实现吗?使用MapReduce进行这种操作有意义吗?

I was wondering if it would be conceptually possible to perform a JOIN operation with MapReduce. Any idea how that could be implemented? Does it make sense to use MapReduce for this kind of operation?

谢谢!

推荐答案

MongoDB不支持关系操作,例如joins.相反,您可以通过将要加入的行嵌入外部文档中来对数据进行非规范化.因此,您可以将products集合与以下模式结合起来,而不是将Products与Sales结合起来:

MongoDB doesn't support relational operations likes joins. Instead, you can denormalise your data by embedding the rows you'd JOIN on inside the outer document. So instead of joining Products to Sales, you could have a products collection with this schema:

产品

{
    _id: 123,
    name: "Widget",
    price: 9.99
    sales:
    [ 
        { id:1, date: "20100316", howMany: 2 },
        { id:2, date: "20100316", howMany: 5 }
    ]
}

然后,每当您检索产品时,也会获得其销售数据,因此无需在其他地方加入或查找信息.

Then whenever you retrieve a product, you also get its sales data so there's no need to join or lookup the info somewhere else.

或者,您可以像使用关系数据库一样将其拆分为两个集合,然后使用其他查询来获取产品的销售额,如下所示:

Alternatively, you could split into two collections as you might with a relational database, then use an additional query to get a product's sales, something like this:

SQL:SELECT Sales WHERE ProductId = 123

MongoDB:db.sales.find( { productid: 123 } )

产品

{
    _id: 123,
    name: "Widget",
    price: 9.99
}

销售

{
    id: 1,
    productid: 123,
    date: "20100316",
    howMany: 2 
}

{
    id: 2,
    productid: 123,
    date: "20100316",
    howMany: 5
}

这篇关于使用MongoDB MapReduce进行联接操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆