Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
migration_scripts
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
serpucga
migration_scripts
Commits
33782e37
Commit
33782e37
authored
Jul 15, 2019
by
serpucga
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Create one Mongo connection for each process
parent
ed2c9d74
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
12 additions
and
7 deletions
+12
-7
pymongoexport_csv.py
pymongoexport_csv.py
+12
-7
No files found.
pymongoexport_csv.py
View file @
33782e37
...
...
@@ -10,10 +10,7 @@ from lib import utils
def
get_page_index
(
collection
,
page_size
:
int
):
page_index
=
[]
for
i
in
range
(
0
,
ceil
(
collection
.
count
()
/
page_size
)):
page_index
.
append
(
get_tweets_page
(
collection
,
page_size
,
i
))
return
page_index
return
list
(
range
(
0
,
ceil
(
collection
.
count
()
/
page_size
)))
def
get_tweets_page
(
collection
,
page_size
:
int
,
num_page
:
int
):
...
...
@@ -21,8 +18,12 @@ def get_tweets_page(collection, page_size: int, num_page: int):
return
tweets
def
write_tweets_to_files
(
header
:
str
,
output_dir
:
str
,
tweets_page
):
def
write_tweets_to_files
(
host
,
port
,
database
,
pagesize
,
header
:
str
,
output_dir
:
str
,
page_index
):
print
(
"Hi there! write_tweets_to_files executing"
)
client
=
pymongo
.
MongoClient
(
host
,
port
)
database_tweets
=
client
[
database
][
"tweets"
]
tweets_page
=
get_tweets_page
(
database_tweets
,
pagesize
,
page_index
)
buffer_tweets
=
{}
for
tweet
in
tweets_page
:
# Get output path and contents for the new CSV file
...
...
@@ -39,6 +40,7 @@ def write_tweets_to_files(header: str, output_dir: str, tweets_page):
buffer_tweets
[
csv_tweet_output_path
][
0
]
+=
csv_tweet_contents
buffer_tweets
[
csv_tweet_output_path
][
1
]
+=
1
client
.
close
()
# Perform the write operations in each of the files
for
output_path
in
buffer_tweets
.
keys
():
with
open
(
output_path
,
"a"
)
as
tweet_writer
:
...
...
@@ -74,11 +76,14 @@ if __name__ == "__main__":
num_page
=
0
page_index
=
get_page_index
(
database_tweets
,
args
.
pagesize
)
client
.
close
()
output
=
mp
.
Queue
()
processes
=
(
mp
.
Process
(
target
=
write_tweets_to_files
,
args
=
(
header
,
output_dir
,
page
))
for
page
in
page_index
)
target
=
write_tweets_to_files
,
args
=
(
args
.
host
,
args
.
port
,
args
.
database
,
args
.
pagesize
,
header
,
output_dir
,
page
))
for
page
in
page_index
)
for
p
in
processes
:
p
.
start
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment