0

如何删除重复条目?

在这种情况下,Twitter 使用雪花作为JSON推文中的唯一 ID 字段,如下所示。按 ID建立推文索引,并删除每个附加$tweet的重复项?但是如何不递归地删除每一条推文......?

一个简单 FLWOR的打印数据库中每条推文的 ID:

for $tweets  in db:open("twitter")
return <tweet>{$tweets/json/id__str}</tweet>

结果:

thufir@dur:~/flwor/twitter$ 
thufir@dur:~/flwor/twitter$ basex tweetID.xq 
<tweet>
  <id__str>1224165280068382720</id__str>
</tweet>
<tweet>
  <id__str>1224160851797643264</id__str>
</tweet>
<tweet>
  <id__str>1224134565280862208</id__str>
</tweet>
...
<tweet>
  <id__str>1224016596634603520</id__str>
</tweet>
<tweet>
  <id__str>1224001430417297410</id__str>
</tweet>
<tweet>
  <id__str>1223987662094249991</id__str>
</tweet>thufir@dur:~/flwor/twitter$ 
thufir@dur:~/flwor/twitter$ 

这里的重复是故意的,但正在寻找某种“清理”数据库的方法。

只是寻找一个大致的轮廓或方法。我的想法是将输出从一个管道传输xquery到另一个,但我被困在如何构建索引上。大概这是数据库本身内置的功能,只需要利用正确的模块(可能)。

--

这看起来至少会返回一个不同的结果:

distinct-values(
    for $tweets in db:open("twitter") 
    return ($tweets/json/id__str))

虽然我不太确定它是所有id__str值的集合。

4

2 回答 2

2

您可以在 FLOWR 中使用 group by 来获取重复项。它也应该比distinct().

for $tweets  in db:open("twitter")
let $id := $tweets/json/id__str
group by $id
return
  if (count($tweets) > 1)
  then (for-each(tail($tweets), function ($tweet) { (: remove $tweet from DB :) } ) 
  else () (: nothing to do :)

于 2020-02-04T10:05:33.840 回答
1

我遇到了同样的问题,并测试了这里已经讨论过的两种方法。两种方法都可用于删除重复项,但存在性能差异。

  1. distinct-values方法:

    (: Open database :)
    let $db := db:open('db-name')
    
    (: Get all distinct IDs :)
    let $ids := distinct-values($db/record/id)
    
    for $id in $ids
      (: Get all records with the same ID :)
      let $recsWithSameId := data($db/record/id)=$id
    
      (: Return only duplicate records :)
      return if (count($recsWithSameId)>1) then
        $recsWithSameId
    
        (: Instead of returning the duplicate records you can now delete all records except the one you want to keep. Then you removed the duplicates. :)
    
    
  2. group by方法:

    for $recs in db:open('db-name')/record
      let $id := $recs/id
      group by $id
      return
        if (count($recs) > 1) then      
          $recs
          (: Instead of returning the duplicate records you can now delete all records except the one you want to keep. Then you removed the duplicates. :)
    

第二种方法 ( group by) 比第一种方法 ( ) 快得多distinct-values

于 2020-11-11T07:58:32.083 回答