-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetDataFromMongo.java
More file actions
137 lines (122 loc) · 3.92 KB
/
Copy pathGetDataFromMongo.java
File metadata and controls
137 lines (122 loc) · 3.92 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import org.bson.Document;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.Arrays;
import java.util.HashMap;
import com.mongodb.BasicDBObject;
import com.mongodb.Block;
import com.mongodb.client.MongoCursor;
import static com.mongodb.client.model.Filters.*;
import com.mongodb.client.result.DeleteResult;
import static com.mongodb.client.model.Updates.*;
import com.mongodb.client.result.UpdateResult;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class GetDataFromMongo {
static MongoClient mongoClient;
static MongoDatabase database;
static MongoCollection<Document> collection;
public GetDataFromMongo(){
mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost:27017"));
database = mongoClient.getDatabase("zikaDB");
// (database).drop();
collection = database.getCollection("rawData");
// (collection).drop();
}
public static void main( String[] args )
{
GetDataFromMongo con = new GetDataFromMongo();
MongoCursor<Document> cursor = collection.find().iterator();
// con.getMultipleDoc();
// con.getDate(cursor);
// con.getLocation(cursor);
con.getLocType(cursor, "Brazil");
}
public void getMultipleDoc(){
MongoCursor<Document> cursor = collection.find().iterator();
try {
while (cursor.hasNext()) {
System.out.println(cursor.next().toJson());
}
} finally {
cursor.close();
}
}
public JSONObject getDate(MongoCursor<Document> cursor){
HashMap<String, Integer> map = new HashMap<String, Integer>();
try {
while (cursor.hasNext()) {
Document doc = cursor.next();
String date = (String)doc.get("report_date");
if( map.containsKey(date)){
map.put(date,map.get(date)+1);
}
else{
map.put(date,1);
}
}
} finally {
cursor.close();
}
JSONObject obj = new JSONObject(map);
System.out.println(obj.toString());
return obj;
}
public JSONObject getLocation(MongoCursor<Document> cursor){
HashMap<String, Integer> map = new HashMap<String, Integer>();
try {
while (cursor.hasNext()) {
Document doc = cursor.next();
String location = (String)doc.get("location");
if( map.containsKey(location)){
map.put(location,map.get(location)+1);
}
else{
map.put(location,1);
}
}
} finally {
cursor.close();
}
JSONObject obj = new JSONObject(map);
System.out.println(obj.toString());
return obj;
}
public JSONObject getLocType(MongoCursor<Document> cursor, String country){
cursor = collection.find().iterator();
HashMap<String, Integer> map = new HashMap<String, Integer>();
try {
while (cursor.hasNext()) {
Document doc = cursor.next();
String location = (String)doc.get("location");
if( location.contains(country)){
String type = (String)doc.get("location_type");
if( map.containsKey(type)){
map.put(type,map.get(type)+1);
}
else{
map.put(type,1);
}
}
}
} finally {
cursor.close();
}
JSONObject obj = new JSONObject(map);
System.out.println(obj.toString());
return obj;
}
}