无法让mongoimport使用Tweepy流媒体示例

时间:2015-06-29 06:51:53

标签: python mongodb unix tweepy

我正在使用Tweepy中的here流媒体示例。

python streaming.py的输出:

{"created_at":"Mon Jun 29 06:44:55 +0000 2015","id":615410601523847168,"id_str":"615410601523847168","text":"RT @CanaryIslandsEN: Next Monday you could be here #MondayMotivation http:\/\/t.co\/veF8shqce5","source":"\u003ca href=\"http:\/\/twitter.com\/download\/android\" rel=\"nofollow\"\u003eTwitter for Android\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":2262161949,"id_str":"2262161949","name":"Dami\u00e1n Rodr\u00edguez","screen_name":"RguezDamian","location":"","url":null,"description":null,"protected":false,"verified":false,"followers_count":559,"friends_count":2005,"listed_count":18,"favourites_count":4798,"statuses_count":5440,"created_at":"Sat Jan 04 16:25:12 +0000 2014","utc_offset":null,"time_zone":null,"geo_enabled":false,"lang":"es","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/424994540911869952\/MQLbvuIh_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/424994540911869952\/MQLbvuIh_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/2262161949\/1409928598","default_profile":true,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweeted_status":{"created_at":"Mon Jun 29 06:18:55 +0000 2015","id":615404056262148096,"id_str":"615404056262148096","text":"Next Monday you could be here #MondayMotivation http:\/\/t.co\/veF8shqce5","source":"\u003ca href=\"http:\/\/twitter.com\" rel=\"nofollow\"\u003eTwitter Web Client\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":1025963442,"id_str":"1025963442","name":"The Canary Islands","screen_name":"CanaryIslandsEN","location":"Canary Islands","url":"http:\/\/www.canarias.es","description":"The Canary Island's tourist board, tweeting about our seven beautiful islands in English. #CanaryIslands for all things Canarian on Twitter :-D","protected":false,"verified":false,"followers_count":6195,"friends_count":1510,"listed_count":154,"favourites_count":7039,"statuses_count":8236,"created_at":"Fri Dec 21 08:46:58 +0000 2012","utc_offset":3600,"time_zone":"London","geo_enabled":true,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"1A1B1F","profile_background_image_url":"http:\/\/pbs.twimg.com\/profile_background_images\/878766124\/75cc68b5d0c7cbe8b5bc2729644b3cf9.jpeg","profile_background_image_url_https":"https:\/\/pbs.twimg.com\/profile_background_images\/878766124\/75cc68b5d0c7cbe8b5bc2729644b3cf9.jpeg","profile_background_tile":false,"profile_link_color":"2FC2EF","profile_sidebar_border_color":"FFFFFF","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/565526839540473856\/jJO10XYD_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/565526839540473856\/jJO10XYD_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1025963442\/1429884147","default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":{"id":"00ab08eef1b62e92","url":"https:\/\/api.twitter.com\/1.1\/geo\/id\/00ab08eef1b62e92.json","place_type":"city","name":"Las Palmas de Gran Canaria","full_name":"Las Palmas de Gran Canaria, Islas Canarias","country_code":"ES","country":"Espa\u00f1a","bounding_box":{"type":"Polygon","coordinates":[[[-15.5255036,28.0248125],[-15.5255036,28.1812125],[-15.3941943,28.1812125],[-15.3941943,28.0248125]]]},"attributes":{}},"contributors":null,"retweet_count":2,"favorite_count":4,"entities":{"hashtags":[{"text":"MondayMotivation","indices":[30,47]}],"trends":[],"urls":[],"user_mentions":[],"symbols":[],"media":[{"id":615404055108694016,"id_str":"615404055108694016","indices":[48,70],"media_url":"http:\/\/pbs.twimg.com\/media\/CIpaukIWsAAGv-F.jpg","media_url_https":"https:\/\/pbs.twimg.com\/media\/CIpaukIWsAAGv-F.jpg","url":"http:\/\/t.co\/veF8shqce5","display_url":"pic.twitter.com\/veF8shqce5","expanded_url":"http:\/\/twitter.com\/CanaryIslandsEN\/status\/615404056262148096\/photo\/1","type":"photo","sizes":{"small":{"w":340,"h":314,"resize":"fit"},"medium":{"w":600,"h":554,"resize":"fit"},"thumb":{"w":150,"h":150,"resize":"crop"},"large":{"w":1000,"h":924,"resize":"fit"}}}]},"extended_entities":{"media":[{"id":615404055108694016,"id_str":"615404055108694016","indices":[48,70],"media_url":"http:\/\/pbs.twimg.com\/media\/CIpaukIWsAAGv-F.jpg","media_url_https":"https:\/\/pbs.twimg.com\/media\/CIpaukIWsAAGv-F.jpg","url":"http:\/\/t.co\/veF8shqce5","display_url":"pic.twitter.com\/veF8shqce5","expanded_url":"http:\/\/twitter.com\/CanaryIslandsEN\/status\/615404056262148096\/photo\/1","type":"photo","sizes":{"small":{"w":340,"h":314,"resize":"fit"},"medium":{"w":600,"h":554,"resize":"fit"},"thumb":{"w":150,"h":150,"resize":"crop"},"large":{"w":1000,"h":924,"resize":"fit"}}}]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level":"low","lang":"en"},"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[{"text":"MondayMotivation","indices":[51,68]}],"trends":[],"urls":[],"user_mentions":[{"screen_name":"CanaryIslandsEN","name":"The Canary Islands","id":1025963442,"id_str":"1025963442","indices":[3,19]}],"symbols":[],"media":[{"id":615404055108694016,"id_str":"615404055108694016","indices":[69,91],"media_url":"http:\/\/pbs.twimg.com\/media\/CIpaukIWsAAGv-F.jpg","media_url_https":"https:\/\/pbs.twimg.com\/media\/CIpaukIWsAAGv-F.jpg","url":"http:\/\/t.co\/veF8shqce5","display_url":"pic.twitter.com\/veF8shqce5","expanded_url":"http:\/\/twitter.com\/CanaryIslandsEN\/status\/615404056262148096\/photo\/1","type":"photo","sizes":{"small":{"w":340,"h":314,"resize":"fit"},"medium":{"w":600,"h":554,"resize":"fit"},"thumb":{"w":150,"h":150,"resize":"crop"},"large":{"w":1000,"h":924,"resize":"fit"}},"source_status_id":615404056262148096,"source_status_id_str":"615404056262148096"}]},"extended_entities":{"media":[{"id":615404055108694016,"id_str":"615404055108694016","indices":[69,91],"media_url":"http:\/\/pbs.twimg.com\/media\/CIpaukIWsAAGv-F.jpg","media_url_https":"https:\/\/pbs.twimg.com\/media\/CIpaukIWsAAGv-F.jpg","url":"http:\/\/t.co\/veF8shqce5","display_url":"pic.twitter.com\/veF8shqce5","expanded_url":"http:\/\/twitter.com\/CanaryIslandsEN\/status\/615404056262148096\/photo\/1","type":"photo","sizes":{"small":{"w":340,"h":314,"resize":"fit"},"medium":{"w":600,"h":554,"resize":"fit"},"thumb":{"w":150,"h":150,"resize":"crop"},"large":{"w":1000,"h":924,"resize":"fit"}},"source_status_id":615404056262148096,"source_status_id_str":"615404056262148096"}]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level":"low","lang":"en","timestamp_ms":"1435560295836"}

{"created_at":"Mon Jun 29 06:44:57 +0000 2015","id":615410607773364224,"id_str":"615410607773364224","text":"RT @WeAreGEMSTONE: Are you ready for the new week?Do you have your goals set?Have you drawn out your 'To-Do' list? If you haven't, do it no\u2026","source":"\u003ca href=\"http:\/\/twitter.com\/download\/android\" rel=\"nofollow\"\u003eTwitter for Android\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":106645129,"id_str":"106645129","name":"MICHAEL HANSON","screen_name":"life2glory","location":"Uyo, Ng","url":"http:\/\/www.life2glory.org","description":"Your destiny is too important for you to be ignored by you.","protected":false,"verified":false,"followers_count":97,"friends_count":675,"listed_count":0,"favourites_count":24,"statuses_count":324,"created_at":"Wed Jan 20 06:00:55 +0000 2010","utc_offset":null,"time_zone":null,"geo_enabled":false,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/423570474703261697\/96NlX__2_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/423570474703261697\/96NlX__2_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/106645129\/1423830615","default_profile":true,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweeted_status":{"created_at":"Mon Jun 29 06:41:50 +0000 2015","id":615409823811768321,"id_str":"615409823811768321","text":"Are you ready for the new week?Do you have your goals set?Have you drawn out your 'To-Do' list? If you haven't, do it now.\n#MondayMotivation","source":"\u003ca href=\"http:\/\/twitter.com\/download\/android\" rel=\"nofollow\"\u003eTwitter for Android\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":929875382,"id_str":"929875382","name":"GEMSTONE ","screen_name":"WeAreGEMSTONE","location":"Nigeria","url":"http:\/\/www.gemstoneng.org","description":"...transformational leaders, transforming nations!!!\r  Join us: http:\/\/t.co\/WSMK0yELZv\u2026  info@gemstoneng.org\r  +23417375383 \/\/ +2348178809662\r  BBpin: 2","protected":false,"verified":false,"followers_count":10387,"friends_count":6102,"listed_count":26,"favourites_count":51,"statuses_count":12644,"created_at":"Tue Nov 06 14:45:09 +0000 2012","utc_offset":3600,"time_zone":"West Central Africa","geo_enabled":true,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/pbs.twimg.com\/profile_background_images\/344918034408325512\/0bb8079ebe543fe4394eca95832c9b2e.jpeg","profile_background_image_url_https":"https:\/\/pbs.twimg.com\/profile_background_images\/344918034408325512\/0bb8079ebe543fe4394eca95832c9b2e.jpeg","profile_background_tile":true,"profile_link_color":"0084B4","profile_sidebar_border_color":"FFFFFF","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/420921101657833473\/CLE4zJno_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/420921101657833473\/CLE4zJno_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/929875382\/1435211848","default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":1,"favorite_count":1,"entities":{"hashtags":[{"text":"MondayMotivation","indices":[123,140]}],"trends":[],"urls":[],"user_mentions":[],"symbols":[]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level":"low","lang":"en"},"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[{"text":"MondayMotivation","indices":[139,140]}],"trends":[],"urls":[],"user_mentions":[{"screen_name":"WeAreGEMSTONE","name":"GEMSTONE ","id":929875382,"id_str":"929875382","indices":[3,17]}],"symbols":[]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level":"low","lang":"en","timestamp_ms":"1435560297326"}

python streaming.py | mongoimport -d test -c twitter.messages的输出:

2015-06-29T06:47:22.806+0000    connected to: localhost
2015-06-29T06:47:25.588+0000    test.twitter.messages   0.0 B
2015-06-29T06:47:28.587+0000    test.twitter.messages   4.0 KB
2015-06-29T06:47:31.587+0000    test.twitter.messages   4.0 KB
2015-06-29T06:47:34.587+0000    test.twitter.messages   20.0 KB
2015-06-29T06:47:37.587+0000    test.twitter.messages   24.0 KB
2015-06-29T06:47:40.587+0000    test.twitter.messages   24.0 KB

但是,文档不会导入到集合中。

mongo
MongoDB shell version: 3.0.4
connecting to: test
> db.twitter.messages.count()
0
> db.twitter.messages.find()
> 

我知道我可以使用PyMongo直接在streaming.py内导入文档,但我想知道为什么这不会起作用。任何帮助将不胜感激。

由于

更新

但这有效:

python streaming.py > data.json
mongoimport -d test -c twitter.messages --file data.json

这样做:

cat data.json | mongoimport -d test -c twitter.messages

这并不是:

python streaming.py > data.json &
tail -f data.json | mongoimport -d test -c twitter.messages

1 个答案:

答案 0 :(得分:1)

你试过了吗?

python streaming.py | mongoimport -d test -c twitter.messages

或者有没有理由说明为什么需要先将文件保存到磁盘?