【问题标题】:Redis - Datastructure to store frequent itemsetsRedis - 存储频繁项集的数据结构
【发布时间】:2014-01-21 07:24:07
【问题描述】:

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

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

【问题讨论】:

  • 你能分享更多你想存储的数据集的例子吗?

标签: redis associations rules


【解决方案1】:

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

解决方案 1:

示例数据集

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

Ans 1:列表或集合可以用作数据结构,具体取决于使用情况。在您的情况下,存在重复键(“In”),因此使用列表。

Ans 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"

解决方案 2:

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

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

样本数据可以描述为:

索引 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 个值,则此描述有效。我们也可以有一个动态列。 对于动态列,第 1 列将是键,第 2 键将是数字部分,其余列将具有字符串。

索引 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 行“在,这个,案例,3”

 lpush In.list StructureRepresent1stRow

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

【讨论】:

  • 这是我可以使用的。但是,当我需要使用它时,我需要使用“-”分隔符来拆分列表。是否可以使用 redis 能够将每个单词(例如 In、This、Case)解释为单独对象的列表?
  • 完美,非常感谢。事实上,我不需要动态列,我们正在使用 k MAX 4,因此不需要超过 4 列。解决方案 2 完美运行。
猜你喜欢
  • 2011-10-06
  • 2014-01-25
  • 1970-01-01
  • 2018-01-20
  • 2018-11-06
  • 2016-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多