gpt4 book ai didi

Redis - 存储频繁项集的数据结构

转载 作者:可可西里 更新时间:2023-11-01 11:12:08 24 4
gpt4 key购买 nike

我有一个针对大型文本语料库执行关联规则挖掘的应用程序。生成的项集具有以下结构:

Item1, Item2, Item3, Item4, Frequency

在这种情况下,所有项目都是单词(字符串元素),而频率是一个整数值。到目前为止,我们已经使用 MySql 来存储项目集。然而,数据库变得非常大,有人建议我使用 NoSql 数据库并专注于 Redis,因为它对各种数据类型有很好的支持(请注意,我对 Redis 或任何其他 NoSql 数据库没有太多经验)。

我的问题是:

  1. 用来存储这些项集的最合适的数据结构是什么?
  2. 我如何查询我的数据库以检索以特定单词开头的项集?

编辑:示例数据是(用 | 分隔的项目,最后一项是频率):

In - this - case - 3
Other - items - 2
This - is - an - 5
Lorem - ipsum - 3
In - other - terms - 2

查询将是:
查找第一项是单词“In”的所有项集及其频率。查询应返回:

In - this - case - 3
In - other - terms - 2

最佳答案

要实现类似于以下条件的行为,可以执行以下操作:

解决方案一:

示例数据集

In - this - case - 3
Other - items - 2
This - is - an - 5
Lorem - ipsum - 3
In - other - terms - 2

答案 1:根据用途,列表或集合可以用作数据结构。在您的情况下,存在重复键(“In”),因此使用列表。

答案 2:如何使用列表:

请记住,Redis 列表的行为类似于链表。

$ redis-cli lpush In.list "In - this - case - 3"
OK

$ redis-cli lpush Other.list "Other - items - 2"
OK

$ redis-cli lpush This.list "This - is - an - 5"
OK

$ redis-cli lpush Lorem.list "Lorem - ipsum - 3"
OK

$ redis-cli lpush In.list "In - other - terms - 2"
OK

$redis-cli lrange In.list 0 -1
1) "In - other - terms - 2"
2) "In - this - case - 3"

方案二:

其他解决方案将再次使用列表:

我们将有四个主要列表,它们的行为类似于数据库中的列和单独的单词列表,这些列表将存储它们在主键列表中出现的索引。

示例数据可以描述为:

索引Column1 Column2 Column3 Column4

 1    In         this    case     3
2 Other items " " 2
3 This is an 5
4 Lorem ipsum " " 3
5 In other terms 2

如果返回最多 4 个值,则此描述有效。我们也可以有一个动态列。对于动态列,第一列是键,第二键是数字部分,其余列将是字符串。

索引Column1 Column2 Column3 Column4 Column5

 1    In         3      this    case     " "
2 Other 2 items " " " "
3 This 5 an " " " "
4 Lorem 3 ipsum " " " "
5 In 2 other terms " "
6 Hello 4 world ! !

继续使用固定的 4 列解决方案:

   //first row
$ redis-cli lpush column1 "In"
1

$ redis-cli lpush In.list 1
1

$ redis-cli lpush column2 "this"
1
$ redis-cli lpush column3 "case"
1
$ redis-cli lpush column4 3
1

//second row
$ redis-cli lpush column1 "Other"
2

$ redis-cli lpush Other.list 2
1

$ redis-cli lpush column2 "items"
2
$ redis-cli lpush column3 " "
2
$ redis-cli lpush column4 2
2

//on same lines add 3rd, 4th row and then 5th row
$ redis-cli lpush column1 "In"
5

$ redis-cli lpush In.list 5
2

$ redis-cli lpush column2 "items"
5
$ redis-cli lpush column3 " "
5
$ redis-cli lpush column4 2
5

To fetch data you can do something like :
$ redis-cli lrange In.list 0 -1
1) 5
2) 1

Using these to values as index query columns as
$redis-cli lindex column1 5
"In"

$redis-cli lindex column2 5
"other"
$redis-cli lindex column3 5
"terms"
$redis-cli lindex column4 5
2

但是对于第二种解决方案,我们引入了将每个字符串插入单独列表的成本,但是您可以使用批量操作来执行它们。我们还保存空白以实现定义明确的行类型。

解决方案 3:

为每一行创建结构并将它们序列化存储在特定的键列表中。

第 1 行“In,this,case,3”

 lpush In.list StructureRepresent1stRow

如果您想使用结构并且要存储复杂的值,则可以选择此解决方案。

关于Redis - 存储频繁项集的数据结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20906792/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com