-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataStoreRedisByProperties.java
166 lines (144 loc) · 6.02 KB
/
DataStoreRedisByProperties.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package com.yelpdatasetchallenge.dataprocessing;
/**
* @author feiyu
*
* Example Code of GeoJSON
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"businessName": "business name 1",
"businessCategories":"category 1, category 2",
"businessAddress":"address 1",
"checkInCountTimeWindow":"6",
"timeWindow":"17-3"
},
"geometry": {
"type": "Point",
"coordinates": [-104.98999178409578, 39.74683938093909]
}
},{
"type": "Feature",
"properties": {
"businessName": "business name 2",
"businessCategories":"category 3, category 2",
"businessAddress":"address 2",
"checkInCountTimeWindow":"2",
"timeWindow":"15-4"
},
"geometry": {
"type": "Point",
"coordinates": [-104.98689115047450, 39.747924136466561]
}
}
]
};
* http://wiki.fasterxml.com/JacksonInFiveMinutes
*/
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import driven.com.fasterxml.jackson.core.JsonFactory;
import driven.com.fasterxml.jackson.core.JsonGenerationException;
import driven.com.fasterxml.jackson.core.JsonParser;
import driven.com.fasterxml.jackson.databind.JsonMappingException;
import driven.com.fasterxml.jackson.databind.JsonNode;
import driven.com.fasterxml.jackson.databind.ObjectMapper;
public class DataStoreRedisByProperties extends DataStoreRedis {
public DataStoreRedisByProperties(String logFilePath, String businessFilePath,
String checkinFilePath) throws Exception {
super(logFilePath, businessFilePath, checkinFilePath);
}
private void getStateCityCategoryHourWeekBusinessMapping() throws IOException {
BufferedReader br = new BufferedReader(new FileReader(checkinFilePath));
ObjectMapper checkInMapper = new ObjectMapper();
try {
String line = br.readLine();
while (line != null) {
JsonNode checkInObj = checkInMapper.readTree(line);
line = br.readLine();
// get checkin info for each business_id in yelp_academic_dataset_checkin.json
JsonNode businessID = checkInObj.get("business_id");
String businessInfo = jedis.get(businessID.asText());
if (businessInfo != null) {
// fetch business Info from Redis and map the string result to JSON
ObjectMapper businessInfoMapper = new ObjectMapper();
JsonNode businessObj = businessInfoMapper.readTree(businessInfo);
// Get chech-in info
JsonNode checkinInfoObj = checkInObj.get("checkin_info");
this.propertyCategory(businessObj, checkinInfoObj);
} else {
logWriter.print("\nCan't get info for business "+businessID.asText());
}
}
} finally {
br.close();
}
}
private void propertyCategory(JsonNode businessObj,JsonNode checkinInfoObj) throws IOException {
ObjectMapper categoryListMapper = new ObjectMapper();
JsonFactory categoryListFactory = categoryListMapper.getFactory();
JsonParser categoryListParser = categoryListFactory.createParser(businessObj.get("categories").toString());
@SuppressWarnings("unchecked")
List<String> categoryList = categoryListMapper.readValue(categoryListParser, List.class);
for (String category : categoryList) {
this.propertyHourWeekTimeWindow(businessObj, checkinInfoObj, category);
}
}
private void propertyHourWeekTimeWindow(JsonNode businessObj, JsonNode checkinInfoObj, String category)
throws JsonGenerationException, JsonMappingException, IOException {
/*
e.g.: checkinInfoObj: {"9-5":1,"7-5":1,"13-3":1,"17-6":1,"13-0":1,"17-3":1,"10-0":1,"18-4":1,"14-6":1}
iterate through checkinInfoObj and get all of the <keyTimeWindow,valueCount> pairs,
and generate the GeoJSONBusinessFeature object of each pair
*/
Iterator<Map.Entry<String,JsonNode>> checkinInfoFields = checkinInfoObj.fields();
while (checkinInfoFields.hasNext()) {
Map.Entry<String,JsonNode> checkinInfoField = checkinInfoFields.next();
String keyTimeWindow = checkinInfoField.getKey();
JsonNode valueCount = checkinInfoField.getValue();
// System.out.println(keyTimeWindow+": "+valueCount);
String wholeKey = businessObj.get("state").asText()+":"
+businessObj.get("city").asText()+":"
+category+":"
+keyTimeWindow;
String wholeValue = businessObj.get("business_id").asText()+":"
+ valueCount.asText();
System.out.println(wholeKey+" <----> "+wholeValue);
ObjectMapper businessCountMapper = new ObjectMapper();
Map<String, String> businessCountMap = new HashMap<String,String>();
businessCountMap.put(businessObj.get("business_id").asText(), valueCount.asText());
String businessCountMapValue = businessCountMapper.writeValueAsString(businessCountMap);
jedis.set(wholeKey, businessCountMapValue);
}
};
@Override
public void callMethods() throws IOException {
this.initJedis("localhost");
this.clearJedis();
this.saveBusinessInfoToDataStore();
this.getStateCityCategoryHourWeekBusinessMapping();
Set<String> keys = jedis.keys("PA:*:Bars:23-5");
System.out.println("=====================================");
for (String key:keys) {
System.out.print(key+" <--> ");
System.out.println(jedis.get(key));
}
// this.getBusinessCheckInInfo();
this.closeJedis();
}
public static void main(String[] argv) throws Exception {
DataStoreRedisByProperties dsRedisByProp = new DataStoreRedisByProperties(
"src/main/resources/yelp-dataset/log_redis_properties_yelp_academic_dataset.txt",
"src/main/resources/yelp-dataset/yelp_academic_dataset_business.json",
"src/main/resources/yelp-dataset/yelp_academic_dataset_checkin.json");
dsRedisByProp.run();
}
}