Skip to content

Commit 2674b70

Browse files
committed
change to import folder based
1 parent 4dc5a16 commit 2674b70

File tree

5 files changed

+108
-64
lines changed

5 files changed

+108
-64
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
npm-debug.log
33
*.swp
44
*.swo
5+
log/
56

lib/mongo-performance.js

+55-14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"dependencies" : {
3232
"fast-csv": ">=0.0.1",
3333
"mongodb": ">=1.3",
34+
"async" : ">=0.2.9",
3435
"mongoose": ">=3.6.20"
3536
}
3637
,"devDependencies": {

src/mongo-performance.coffee

+45-50
Original file line numberDiff line numberDiff line change
@@ -11,74 +11,66 @@ Schema = mongoose.Schema
1111
path = require "path"
1212
fs = require "fs"
1313
csv = require "fast-csv"
14+
async = require "async"
1415

1516
{MongoClient} = require "mongodb"
1617

17-
# {
18-
# "_id" : ObjectId("526c87bd11ba6145572c3475"),
19-
# "Name" : "周晓晨",
20-
# "CardNo" : "",
21-
# "Descriot" : "",
22-
# "CtfTp" :
23-
# "ID",
24-
# "CtfId" : NumberLong("370682199312040223"),
25-
# "Gender" : "F",
26-
# "Birthday" : NumberLong(19931204),
27-
# "Address" : "",
28-
# "Zip" : "",
29-
# "Dirty" : "F",
30-
# "District1" : "",
31-
# "District2" : "CHN",
32-
# "District3" : 37,
33-
# "District4" : 370682,
34-
# "District5" : "",
35-
# "District6" : "",
36-
# "FirstNm" : "",
37-
# "LastNm" : "",
38-
# "Duty" : "",
39-
# "Mobile" : "",
40-
# "Tel" : "",
41-
# "Fax" : "",
42-
# "EMail" : "",
43-
# "Nation" : "",
44-
# "Taste" : "",
45-
# "Education" : "",
46-
# "Company" : "",
47-
# "CTel" : "",
48-
# "CAddress" : "",
49-
# "CZip" : "",
50-
# "Family" : 0,
51-
# "Version" : "2012-12-16 7:43:30",
52-
# "id" : NumberLong(19720186)
53-
# }
54-
55-
5618
DC_CITYS = ["北京","南京","上海","汉口","青岛","大连","沈阳","哈尔","西安","天津","重庆", "广州","深圳","香港","台湾","澳门" ]
5719

20+
COUNT_CSV = 0
21+
COUNT_INSERTION = 0
22+
CSV_FILES = []
23+
CUR_CSV_FILE = null
24+
DB_COLLECTION = null
5825

5926
console.log "[mongo-performance::init] %j", process.argv
6027
#pathToCSVFile = path.join __dirname, process.argv[2]
6128
pathToCSVFile = process.argv[2]
6229
unless fs.existsSync(pathToCSVFile)
63-
console.log "[mongo-performance::init] missing csv sample at:#{pathToCSVFile}"
30+
console.log "[mongo-performance::init] missing csv folder at:#{pathToCSVFile}"
6431
process.exit(1)
6532

33+
34+
try
35+
files = fs.readdirSync pathToCSVFile
36+
#console.log "[mongo-performance::file] #{files}"
37+
38+
for file in files
39+
#console.log "[mongo-performance::file] #{path.extname(file)}"
40+
if path.extname(file) is ".csv"
41+
CSV_FILES.push(path.join(pathToCSVFile, file))
42+
43+
44+
unless CSV_FILES.length > 0
45+
console.log "[mongo-performance::init] missing csv files at:#{pathToCSVFile}"
46+
process.exit(1)
47+
48+
console.log "[mongo-performance::init] CSV files to process:#{CSV_FILES}"
49+
6650
MongoClient.connect "mongodb://127.0.0.1:27017/kf", (err, db) ->
6751
throw err if err
68-
#collection = db.collection("test_insert")
69-
collection = db.collection("members")
52+
#DB_COLLECTION = db.collection("test_insert")
53+
DB_COLLECTION = db.collection("members")
7054

7155
console.log "[mongo-performance::init] db is ready"
56+
async.eachSeries CSV_FILES, parseCSV, (err)->
57+
if err?
58+
console.log "[mongo-performance::each csv] error:#{err}"
59+
else
60+
console.log "DONE [mongo-performance::each csv] ALL DONE! csv entry:#{COUNT_CSV}, db etnry:#{COUNT_INSERTION}"
7261

73-
count = 0
74-
countInsert = 0
62+
parseCSV = (filepath, next)->
7563

7664
# init a csv parsing job
77-
job = csv pathToCSVFile,
65+
job = csv filepath,
7866
headers : true
7967

68+
countRead = 0
69+
countInsert = 0
70+
8071
job.on "data", (data) ->
81-
++count
72+
++COUNT_CSV
73+
++countRead
8274
delete data["id"]
8375
delete data["Version"]
8476
delete data["Taste"]
@@ -112,13 +104,14 @@ MongoClient.connect "mongodb://127.0.0.1:27017/kf", (err, db) ->
112104
#console.log "[mongo-performance::csv::ondata] province:#{province}, address:#{address}"
113105
data["province"] = province if province?
114106

115-
console.log "[mongo-performance::csv::ondata] %j \n count:%d", data, count
116-
collection.insert data, (err, docs) ->
107+
#console.log "[mongo-performance::csv::ondata] %j \n count:%d", data, count
108+
DB_COLLECTION.insert data, (err, docs) ->
117109
if err?
118110
console.log "[mongo-performance::db::insert] #{err}"
119111
else
112+
++COUNT_INSERTION
120113
++countInsert
121-
console.log "[mongo-performance::db::insert] succeed:%j \n insert count:%d", docs, countInsert
114+
console.log "[mongo-performance::db::insert] succeed. ALL csv:#{COUNT_CSV}, insertion:#{COUNT_INSERTION}, CUR: csv:#{countRead}, insert:#{countInsert}, from:#{filepath}"
122115

123116
return
124117

@@ -129,12 +122,14 @@ MongoClient.connect "mongodb://127.0.0.1:27017/kf", (err, db) ->
129122

130123
job.on "end", ->
131124
job.removeAllListeners()
132-
console.log "[mongo-performance::csv::on end] processed %d records", count
125+
console.log "[mongo-performance::csv::on end] complete #{filepath}"
133126
#db.close()
134127
#process.exit(0)
128+
next()
135129
return
136130

137131
job.parse()
132+
return
138133

139134

140135

start.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
mkdir -p log
2+
kill `cat ./log/mongp.pid`
3+
node lib/mongo-performance.js ~/2000W > log/mongp.log 2>&1 &
4+
echo $!>./log/mongp.pid
5+
tail -f ./log/mongp.log
6+

0 commit comments

Comments
 (0)