3

从这里可以看到(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
},
4

2 回答 2

2

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

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

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

编辑 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 被收藏了多少次。您每次都必须提取整行,进行一些操作/正则表达式/技巧来获取该字段,然后手动对其进行计数。

于 2012-08-15T07:02:48.977 回答
1

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

于 2012-08-17T05:04:07.960 回答