如何从 SQL 转换为 NoSQL/MapReduce?
我有使用关系数据库的背景,但最近开始涉足 CouchDB,并对一些非关系操作(在 SQL 中很简单)在 CouchDB 中并不是一流函数感到惊讶。
如果您花点时间将下面的每个 SQL 语句映射到其 MapReduce 等效项,我将不胜感激。
SELECT COUNT(*) FROM products WHERE price < 20.00;
SELECT category, SUM(price) FROM products GROUP BY category;
UPDATE products SET price = 19.99 WHERE price = 20.00;
DELETE FROM products WHERE expires_at <= NOW();
I have a background working with relational databases but recently started to dabble in CouchDB and was surprised by how some non-relational operations, which would be simple in SQL, were not first-class functions in CouchDB.
I would appreciate you taking a moment to map each SQL statement below to its MapReduce equivalent.
SELECT COUNT(*) FROM products WHERE price < 20.00;
SELECT category, SUM(price) FROM products GROUP BY category;
UPDATE products SET price = 19.99 WHERE price = 20.00;
DELETE FROM products WHERE expires_at <= NOW();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SELECT
命令非常简单。批量写入有点复杂。通常,您将使用某些视图来检索需要更改的文档,然后您将使用 < code>_bulk_docs 用于一次发送所有更改的 API。另外,请参阅有关视图的文档,了解有关如何发出查询的详细信息。这包括排序、分组等。
SELECT COUNT(*) FROM 产品,其中价格
20.00;
MapReduce
如果您需要使用任意数量(而不仅仅是 20),那么您需要在所有情况下发出价格,并使用
startkey
和 endkey 来缩小结果集范围。SUM(price) FROM products GROUP BYcate;
MapReduce
SELECTcategory ,
此映射函数本质上使用类别作为键,价格作为键/值对中的值。减少功能会将每个不同键的价格相加。
UPDATE products SET Price = 19.99 WHERE Price = 20.00;
Map
一旦您的应用程序拉取此视图的内容,您将在应用程序代码中执行所有操作,然后将结果发送回数据库通过
_bulk_docs
API。DELETE FROM products WHERE expires_at <= NOW();
地图
根据您存储日期时间值的方式,您可能需要调整地图功能以及对视图的查询。使用时间戳(JS 使用毫秒而不是秒)可能是实现此目的的最快方法。设置查询后,您将向每个文档添加一个新字段。
_deleted:true
。一旦您将此列表发送回数据库(再次使用_bulk_docs
),所有指定的文档都将被删除。The
SELECT
commands are pretty easy. Bulk writes are a bit more complicated. Generally, you'll use some view to retrieve the documents that need to be changed, then you'll use the_bulk_docs
API to send all the changes at once.Also, consult the documentation regarding views for details for how to issue queries. This includes ordering, grouping, etc.
SELECT COUNT(*) FROM products WHERE price < 20.00;
Map
Reduce
If you need this to work with an arbitrary amount, not just 20, then you'll need to emit the price in all cases, and use
startkey
andendkey
to narrow down your resultset.SELECT category, SUM(price) FROM products GROUP BY category;
Map
Reduce
This map function essentially uses the category as the key, with the price as the value in your key/value pair. The reduce function will add up the prices for each different key.
UPDATE products SET price = 19.99 WHERE price = 20.00;
Map
Once your application pulls down the contents of this view, you'll perform all the manipulations in your application code, then send back the results into the database via the
_bulk_docs
API.DELETE FROM products WHERE expires_at <= NOW();
Map
Depending on how your store your date-time values, you may need to adjust the map function as well as your query to the view. Using a timestamp (JS uses milliseconds instead of seconds) is probably the fastest way to accomplish this. Once you've set up your query, you'll add a new field to each of these documents.
_deleted: true
. Once you send this list back into the database (again with_bulk_docs
) all the specified documents will be deleted.