【问题标题】:Is there any best way to store tweets in MySQL?有没有最好的方法在 MySQL 中存储推文?
【发布时间】:2012-08-15 06:59:40
【问题描述】:

从这里 (https://dev.twitter.com/docs/api/1/get/statuses/home_timeline) 可以看出,一条推文有很多信息(字段),因此在 MySQL 中存储推文信息并不容易。

如果我把这个 JSON 做成数组,它就不是 1-depth 数组。例如,像这个 JSON 中的 URL 实体,它可能在一个“实体”字段中有很多 URL。

我是否应该将此信息存储为一个字段中的“urls[{"aa": a, "bb": b}, {"aa": c, "bb": d}]" 之类的字符串?或者有没有最好的存储方式?

{
    "coordinates": null,
    "favorited": false,
    "created_at": "Fri Jul 16 16:58:46 +0000 2010",
    "truncated": false,
    "entities": {
      "urls": [
        {
          "expanded_url": null,
          "url": "http://www.flickr.com/photos/cindyli/4799054041/",
          "indices": [
            75,
            123
          ]
        }
      ],
      "hashtags": [

      ],
      "user_mentions": [
        {
          "name": "Stephanie",
          "id": 15473839,
          "indices": [
            27,
            39
          ],
          "screen_name": "craftybeans"
        }
      ]
    },
    "text": "got a lovely surprise from @craftybeans. She sent me the best tshirt ever. http://www.flickr.com/photos/cindyli/4799054041/ ::giggles::",
    "annotations": null,
    "contributors": null,
    "id": 18700887835,
    "geo": null,
    "in_reply_to_user_id": null,
    "place": null,
    "in_reply_to_screen_name": null,
    "user": {
      "name": "cindy li",
      "profile_sidebar_border_color": "AD0066",
      "profile_background_tile": false,
      "profile_sidebar_fill_color": "AD0066",
      "created_at": "Wed Nov 29 06:08:08 +0000 2006",
      "profile_image_url": "http://a1.twimg.com/profile_images/553508996/43082001_N00_normal.jpg",
      "location": "San Francisco, CA",
      "profile_link_color": "FF8500",
      "follow_request_sent": false,
      "url": "http://www.cindyli.com",
      "favourites_count": 465,
      "contributors_enabled": false,
      "utc_offset": -28800,
      "id": 29733,
      "profile_use_background_image": true,
      "profile_text_color": "000000",
      "protected": false,
      "followers_count": 3395,
      "lang": "en",
      "notifications": true,
      "time_zone": "Pacific Time (US & Canada)",
      "verified": false,
      "profile_background_color": "cfe8f6",
      "geo_enabled": true,
      "description": "Just me, Cindy Li.Giving cute substance since 1997.\r\nMarried to @themattharris.\r\nProduct designer for Yahoo! ",
      "friends_count": 542,
      "statuses_count": 4847,
      "profile_background_image_url": "http://a3.twimg.com/profile_background_images/3368753/twitter_flowerbig.gif",
      "following": true,
      "screen_name": "cindyli"
    },
    "source": "web",
    "in_reply_to_status_id": null
},

【问题讨论】:

  • 只有 9 个字段。将它们作为列存储在数据库中的问题在哪里?
  • @DainisAbols 实际上,我无法理解您的意思。这怎么可能只有9个字段?我以为是这么多领域...
  • 这取决于您想在数据库中存储多少信息。您应该只存储基本的 tweet 信息。您不想存储所有推特数据。
  • @DainisAbols 不幸的是,我想尽可能多地存储数据。
  • MongoDB 可能更适合您正在寻找的内容,或者通常是面向文档的数据库。

标签: php mysql twitter field


【解决方案1】:

将其存储在列中。然后,您可以将索引放在您想要快速找到的东西上——而不是仅仅提到能够搜索所有内容。以您建议的格式搜索特定内容将是一场噩梦。

如果您想继续使用其中的位,请编写一个处理其数据库方面的对象,或者至少编写一个将它们弹出或取出的函数。现在看起来工作量更大,但从长远来看,它会在以后为您节省更多更多的工作量。

编辑:是的,我会将每一位数据保留在它自己的列中。话虽如此,您可能不需要存储每一个信息。例如,如果您不想保留有关“用户提及”的信息,请完全跳过它。

编辑 2L 以透视这一点,假设您要搜索“Bob”。如果你有这样的列结构:

+------+-----------+-----------+-----+
| user | favorited | truncated | url |
+------+-----------+-----------+-----+
| Bob  | true      | false     | ... |
| Sue  | true      | true      | ... |
| Tom  | true      | false     | ... |
+------+-----------+-----------+-----+

你可以只写一个死的简单查询。

对比这样的:

+--------------------------------------------------------------+
| tweetData                                                    |
+--------------------------------------------------------------+
| user:Bob;favorited:true;truncated:false;url:www.example.com  |
| user:Sue;favorited:true;truncated:true;url:www.example2.com  |
| user:Tom;favorited:true;truncated:false;url:www.example2.com |
+--------------------------------------------------------------+

想象一下,试图找出 Bob 被收藏了多少次。您必须每次都提取整行,进行一些操作/正则表达式/技巧来获取该字段,然后手动对其进行计数。

【讨论】:

  • 在“entities”中,它有几个数据,如“url”、“hashtags”、“user_mentions”等。在这种情况下,我应该将这些键分别作为一个列,如 url 列、hashtag列,user_mention 列?
  • 感谢您的精彩解释!我想我应该找到必要的信息来存储在列中。
  • 非常感谢您的友好回答!我必须创建一个可搜索的列(应该是列),并且可以从搜索结果中访问该列(可以是原始 JSON 字符串)。
【解决方案2】:

苛刻但真实的答案是阅读数据库设计基础知识。看起来你在想你必须把它存储在一张桌子上。您可能希望将其拆分为多个表并将它们连接在一起。

【讨论】:

    猜你喜欢
    • 2019-12-18
    • 2018-06-19
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 2017-02-18
    • 2016-10-29
    • 2021-02-23
    • 2014-09-02
    相关资源
    最近更新 更多