DataLoader.java
4.88 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
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
167
168
169
170
171
172
173
174
175
176
package com.bsth.data.arrival;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.bsth.data.BasicData;
import com.bsth.data.LineConfigData;
import com.bsth.entity.realcontrol.LineConfig;
import com.bsth.util.DateUtils;
import com.bsth.util.db.DBUtils_MS;
/**
*
* @ClassName: DataLoader
* @Description: TODO(从数据库加载进出站数据)
* @author PanZhao
* @date 2016年8月19日 上午9:59:21
*
*/
@Component
public class DataLoader {
private static Long prveLoadTime;
private final static long DAY_TIME = 1000 * 60 * 60 * 24;
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
LineConfigData lineConfigData;
/**
*
* @Title: load
* @Description: TODO(根据上次加载时间,查询之后的增量数据)
*/
public Set<ArrivalEntity> load(){
Set<ArrivalEntity> set = null;
if(null == prveLoadTime)
set = recovery();
else{
Calendar cal = Calendar.getInstance();
//周数,表分区字段
int weeks_year = cal.get(Calendar.WEEK_OF_YEAR);
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select * from bsth_c_arrival_info where weeks_year=? AND create_timestamp > ? AND create_timestamp <=? AND ABS(create_timestamp - ts) < 3600000 order by ts";
try{
long t = System.currentTimeMillis() /*prveLoadTime + (1000 * 60 * 60)*/;
conn = DBUtils_MS.getConnection();
ps = conn.prepareStatement(sql);
ps.setInt(1, weeks_year);
ps.setLong(2, prveLoadTime);
ps.setLong(3, t);
rs = ps.executeQuery();
set = resultSet2Set(rs);
prveLoadTime = t;
}catch(Exception e){
logger.error("", e);
}finally {
DBUtils_MS.close(rs, ps, conn);
}
}
return set;
}
/**
*
* @Title: recovery
* @Description: TODO(从数据库恢复数据,按照线路的开始运营时间恢复)
*/
public Set<ArrivalEntity> recovery(){
Collection<LineConfig> confs = lineConfigData.getAll();
long t = System.currentTimeMillis()
,st;
Set<ArrivalEntity> all = new HashSet<>();
for(LineConfig conf : confs){
st = conf.getCurrStartTime();
if(t < st)
st = st - DAY_TIME;
all.addAll(loadByLineAndTime(conf.getLine().getLineCode(), st, t));
}
prveLoadTime = t;
return all;
}
/**
*
* @Title: loadByLineAndStartTime
* @Description: TODO(根据线路和时间戳加载数据)
*/
public Set<ArrivalEntity> loadByLineAndTime(String lineCode, long st, long et){
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(st);
int weeks_year = cal.get(Calendar.WEEK_OF_YEAR);
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
Set<ArrivalEntity> set = new HashSet<>();
String sql = "select * from bsth_c_arrival_info where weeks_year=? and line_id=? AND create_timestamp > ? AND create_timestamp <=? AND ABS(create_timestamp - ts) < 3600000 order by ts";
try{
conn = DBUtils_MS.getConnection();
ps = conn.prepareStatement(sql);
ps.setInt(1, weeks_year);
ps.setString(2, lineCode);
ps.setLong(3, st);
ps.setLong(4, et);
rs = ps.executeQuery();
set = resultSet2Set(rs);
System.out.println("加载 " + BasicData.lineCode2NameMap.get(lineCode) + DateUtils.sdfyyyyMMddHHmm.format(new Date(st))
+ "至" + DateUtils.sdfyyyyMMddHHmm.format(new Date(et)) + " 数据,共:" + set.size());
}catch(Exception e){
logger.error("", e);
}finally {
DBUtils_MS.close(rs, ps, conn);
}
return set;
}
public Set<ArrivalEntity> resultSet2Set(ResultSet rs) throws SQLException{
Set<ArrivalEntity> set = new HashSet<>();
ArrivalEntity arr;
while(rs.next()){
arr = new ArrivalEntity();
arr.setDeviceId(rs.getString("device_id"));
arr.setNbbm(BasicData.deviceId2NbbmMap.get(arr.getDeviceId()));
if(null == arr.getNbbm()){
logger.warn("未注册的设备号," + arr.getDeviceId());
continue;
}
arr.setTs(rs.getLong("ts"));
arr.setLineCode(rs.getString("line_id"));
arr.setUpDown(rs.getInt("up_down"));
arr.setStopNo(rs.getString("stop_no"));
arr.setStopName(BasicData.stationCode2NameMap.get(arr.getStopNo()));
arr.setInOut(rs.getInt("in_out"));
arr.setCreateDate(rs.getLong("create_timestamp"));
arr.setWeeksYear(rs.getInt("weeks_year"));
set.add(arr);
}
return set;
}
public static void setPrveLoadTime(long t){
prveLoadTime = t;
}
}