-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetDataInMongo_Clean.java
123 lines (103 loc) · 3.16 KB
/
SetDataInMongo_Clean.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class SetDataInMongo_Clean {
static MongoClient mongoClient;
static MongoDatabase database;
static MongoCollection<Document> collection;
public SetDataInMongo_Clean(){
mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost:27017"));
database = mongoClient.getDatabase("zikaDB");
collection = database.getCollection("cleanData");
// collection.drop();
}
public static void main( String[] args )
{
SetDataInMongo_Clean con = new SetDataInMongo_Clean();
// con.insertSingleDoc();
try {
con.insertFileInMongo();
} catch (IOException e) {
e.printStackTrace();
}
// con.getMultipleDoc();
mongoClient.close();
}
public void insertFileInMongo() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String csvFile = br.readLine();
br = new BufferedReader( new FileReader(csvFile));
String line = null;
line = br.readLine();
String[] keys = {"Date","Country","State/City","Location_Type","State"};
System.out.println(Arrays.toString(keys));
List<Document> documents = new ArrayList<Document>();
String[] values;
while((line = br.readLine()) != null){
int i = 0;
String[] curr = line.split(",");
values = new String[5];
String cleanedDate = null;
if( (cleanedDate = cleanDate(curr[0]))== null){
continue;
}
values[i] = cleanedDate;
i++;
String[] country_state = curr[1].split("-");
String country = country_state[0];
String state = null;
if( country_state.length == 2){
state = country_state[1];
}
values[i++] = country;
values[i++] = state;
values[i++] = curr[2];
values[i] = curr[3];
Document doc = createJson(keys,values);
documents.add(doc);
}
br.close();
// System.out.println(documents.toString());
collection.insertMany(documents);
}
public Document createJson(String[] keys, String[] values){
Document doc= new Document();
for( int i = 0; i < keys.length; i++){
String key = keys[i].replace("\"","");
String value = null;
if( values[i] != null){
value = values[i].replace("\"","");
}
doc.append(key, value);
}
return doc;
}
public void insertSingleDoc(){
Document doc = new Document("name", "MongoDB")
.append("type", "database")
.append("count", 1)
.append("versions", Arrays.asList("v3.2", "v3.0", "v2.6"))
.append("info", new Document("x", 203).append("y", 102));
collection.insertOne(doc);
}
public static String cleanDate(String date){
String result = null;
date = date.replaceAll("_", "-");
date = date.replaceAll(" ", "-");
String[] dateArray = date.split("-");
if( dateArray == null || dateArray.length < 3)
return result;
else
return date;
}
}
///Users/Nish/Downloads/cdc_zika.csv