在python中表达berkeley db中的多列?
假设我有一个简单的表,其中包含用户名、名字、姓氏。
我如何在 berkeley Db 中表达这一点?
我目前使用 bsddb 作为接口。
干杯。
Say I have a simple table that contains username, firstname, lastname.
How do I express this in berkeley Db?
I'm currently using bsddb as the interface.
Cheers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须选择一个“列”作为键(必须是唯一的;我想在您的情况下这将是“用户名”)——这是搜索可能发生的唯一方式。其他列可以通过您喜欢的任何方式成为该键的单个字符串值,从腌制到简单连接保证永远不会出现在任何列中的字符,例如对于许多类型的“\0” “可读文本字符串”。
如果您需要能够通过不同的键进行搜索,您将需要将其他补充性和单独的 bsddb 数据库设置为主表中的“索引”——这是大量的工作,并且有很多关于该主题的文献。 (或者,您可以转向更高抽象的技术,例如 sqlite,它可以为您巧妙地处理索引;-)。
You have to pick one "column" as the key (must be unique; I imagine that would be "username" in your case) -- the only way searches will ever possibly happen. The other columns can be made to be the single string value of that key by any way you like, from pickling to simple joining with a character that's guaranteed to never occur in any of the columns, such as `\0' for many kind of "readable text strings".
If you need to be able to search by different keys you'll need other, supplementary and separate bsddb databases set up as "indices" into your main table -- it's lots of work, and there's lots of literature on the subject. (Alternatively, you move to a higher-abstraction technology, such as sqlite, which handles the indexing neatly on your behalf;-).
tl,dr:要在像 Berkley db 这样的有序键值存储中表达多个列,您需要了解键组合。查看我关于 bsddb 的其他答案以了解更多信息。
有多种方法可以使用有序键/值存储来做到这一点。
最简单的解决方案是使用正确的键将文档存储为 json 值。
现在,您可能希望在这些列上构建索引来检索文档,而不必迭代所有哈希图来查找正确的对象。为此,您可以使用 secondaryDB 它将自动构建索引你。或者您可以自己构建索引。
如果您不想处理密钥打包(这是启动的好主意),您可以利用DB.set_bt_compare 允许您使用 cpickle、json 或msgpack 用于键和值,同时仍然具有使创建索引和执行查询有意义的顺序。这是较慢的方法,但引入了键组合的模式。
要充分利用有序键的优势,您可以使用 Cursor.set_range(key) 来设置查询开头的数据库位置。
另一种模式称为EAV 模式,它存储遵循方案
(实体、属性、值)
的元组,然后通过使用该元组的排列来构建各种索引。我通过学习原子学学到了这个模式。对于资源消耗较少的数据库,您将采用“静态类型”方式,将尽可能多的公共信息存储在“元数据”表中,并将文档(实际上是 RDBMS 表)拆分到它们自己的哈希图中。
为帮助您入门,我们提供了一个使用 bsddb 的示例数据库(但您可以使用另一个有序键/值存储(如wiredtiger 或 leveldb)来构建它),该数据库实现了 EAV 模式。在此实现中,我将 EAV 替换为 IKV,IKV 转换为唯一标识符、键、值。总体结果是您拥有一个完全索引的无模式文档数据库。我认为这是效率和易用性之间的一个很好的折衷。
tl,dr: To express multiple columns in an ordered key value store like berkley db you need to learn about key composition. Look up my other answers about bsddb to learn more.
There is several ways to do that using ordered key/value store.
The simplest solution is to store documents as json values with a correct key.
Now you probably want to build index over those columns to retrieve documents without having to iterate over all the hashmap to find the correct object. For that you can use a secondaryDB that will build automatically the index for you. Or you can build the index yourself.
If you don't want to deal with key packing (and it's a good idea for starting up), you can take advantage of DB.set_bt_compare which will allow you to use cpickle, json or msgpack for both keys and values while still having an order that makes sens to create indices and doing queries. This is slower method but introduce the pattern of key composition.
To fully take advantage what ordered key is, you can make use of
Cursor.set_range(key)
to set the position of the db at the beginning of a query.Another pattern, is called the EAV pattern stores tuples that follow the scheme
(entity, attribute, value)
and then you build various index by using permutation of that tuple. I learned this pattern studing datomic.For less ressource hungry database, you will go the "static typed" way and store as much as possible of common information in the "metadata" table and split documents (which are really RDBMS tables) into their own hashmap.
To get you started here is an example database using bsddb (but you could build it using another ordered key/value store like wiredtiger or leveldb) that implements the EAV pattern. In this implementation I swap EAV for IKV which translates to Unique identifier, Key, Value. The overal result is that you have a fully indexed schema less document database. I think it's a good compromise between efficiency and ease-of-use.