-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoadIcesatDB.java
More file actions
246 lines (204 loc) · 9.28 KB
/
Copy pathLoadIcesatDB.java
File metadata and controls
246 lines (204 loc) · 9.28 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
Copyright (c) 2007 The Regents of the University of California
Permission to use, copy, modify, and distribute this software and its documentation
for educational, research and non-profit purposes, without fee, and without a written
agreement is hereby granted, provided that the above copyright notice, this
paragraph and the following three paragraphs appear in all copies.
Permission to make commercial use of this software may be obtained
by contacting:
Technology Transfer Office
9500 Gilman Drive, Mail Code 0910
University of California
La Jolla, CA 92093-0910
(858) 534-5815
invent@ucsd.edu
THIS SOFTWARE IS PROVIDED BY THE REGENTS OF THE UNIVERSITY OF CALIFORNIA AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@author: Minh Phan (mnphan@ucsd.edu)
@date: July 24th 2017
@desc: Program loads the ICESat data file in CSV format (generated from HDF5.java) into the postgres database table.
@project_url: www.openaltimetry.org
*/
import java.io.File;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import org.postgresql.copy.CopyManager;
import org.postgresql.core.BaseConnection;
public class LoadIcesatDB {
// Postgresql database connection
static public final String PDB_CONNECTION = Property.getProperty("pdb.connection");
static public final String PDB_USERNAME = Property.getProperty("pdb.username");
static public final String PDB_PASSWORD = Property.getProperty("pdb.password");
static public Connection postgresConn = null;
// Data dir that store CSV files to be loaded to database
static public final String DATA_DIR = Property.getProperty("data_dir");
// Variable to count table_40hz
static public long count_table_40hz = 0;
// Variable to count table_1hz
static public long count_table_1hz = 0;
// Variable to count number of row of table_fileId
static public long count_table_fileId = 0;
// Variable to count number of file_40Hz
static public long count_file_40hz = 0;
// Variable to count number of file_1Hz
static public long count_file_1hz = 0;
@SuppressWarnings("ConvertToTryWithResources")
public static void main(String[] args) {
Date startTime = new Date();
System.out.println("Program started at: " + startTime.toString());
try {
// Connect to Postgresql database
if (!postgresConnect()) {
System.out.println("Cannot connect to postgreSQL database");
return;
}
System.out.println("Connected to postgreSQL database");
String sql;
FileReader reader;
try {
// Start loading process
CopyManager cm = new CopyManager((BaseConnection) postgresConn);
// Get data dir object
File data_dir_obj = new File(DATA_DIR);
if (!data_dir_obj.exists() || !data_dir_obj.isDirectory()) {
System.out.println("Data dir does not exist: " + DATA_DIR);
return;
}
// Load table_fileId.csv to table, this is just metadata table
sql = "COPY HDF5_FILE FROM STDIN WITH DELIMITER AS ',' CSV";
reader = new FileReader(new File(data_dir_obj, "table_fileId.csv"));
count_table_fileId = cm.copyIn(sql, reader);
reader.close();
// Get all csv files as File object
File[] csv_file_objs = data_dir_obj.listFiles();
// Scan for each CSV file
for (File csv_file_obj : csv_file_objs) {
// Make sure it is a file
if (csv_file_obj.isDirectory()) {
continue;
}
// Make sure it is a CSV extension
String fileName = csv_file_obj.getName();
if (!fileName.endsWith(".csv")) {
continue;
}
// If it is a CSV file for table_1hz
if (fileName.startsWith("table_1hz")) {
count_file_1hz++;
sql = "COPY DATA_1HZ (FILEID, EPOCH_2000_1, DATETIME_1, TRACK_ID, TIME_IDX) FROM STDIN WITH DELIMITER AS ',' CSV";
reader = new FileReader(csv_file_obj);
count_table_1hz += cm.copyIn(sql, reader);
reader.close();
System.out.println(count_file_1hz + "\n" + fileName);
} // Else if it is a CSV file for table_40hz
else if (fileName.startsWith("table_40hz")) {
count_file_40hz++;
sql = "COPY DATA_40HZ (FILEID, EPOCH_2000_40, DATETIME_40, LAT, LON, ELEV, TIME_IDX) FROM STDIN WITH DELIMITER AS ',' CSV";
reader = new FileReader(csv_file_obj);
count_table_40hz += cm.copyIn(sql, reader);
reader.close();
System.out.println(count_file_40hz + "\n" + fileName);
}
}
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
// Don't forget to disconnect database before exit
postgresDisconnect();
System.out.println("\nDisconnected to postgreSQL database");
}
System.out.println("count_file_40hz: " + count_file_40hz);
System.out.println("count_file_1hz: " + count_file_1hz);
System.out.println("count_table_40hz: " + count_table_40hz);
System.out.println("count_table_1hz: " + count_table_1hz);
System.out.println("count_table_fileId: " + count_table_fileId);
/*
count_file_40hz: 597
count_file_1hz: 597
count_table_40hz: 1038759677
count_table_1hz: 36336251
count_table_fileId: 34208
*/
} catch (Exception e) {
e.printStackTrace();
}
Date endTime = new Date();
System.out.println("Program started at: " + startTime.toString());
System.out.println("Program endded at: " + endTime.toString());
System.out.println("Total runtime: " + getTimeDifference(endTime, startTime));
}
public static boolean postgresConnect() {
return postgresConnect(false);
}
public static boolean postgresConnect(boolean readonly) {
try {
postgresConn = DriverManager.getConnection(PDB_CONNECTION, PDB_USERNAME, PDB_PASSWORD);
postgresConn.setReadOnly(readonly);
} catch (SQLException ex) {
ex.printStackTrace();
return false;
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
return true;
}
/**
* Desc: Close connection from Postgres Database
*
* @return
*/
public static boolean postgresDisconnect() {
try {
if (postgresConn != null) {
postgresConn.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
return false;
}
return true;
}
public static int executeUpdateQuery(String sql) {
System.out.println(sql);
int rows = -1;
try {
Statement stmt = postgresConn.createStatement();
rows = stmt.executeUpdate(sql);
stmt.close();
} catch (Exception ex) {
ex.printStackTrace();
}
System.out.println("Returned: " + rows + "\n------------------");
return rows;
}
/**
* @param d2 the later date
* @param d1 the earlier date
* @return
*/
public static String getTimeDifference(Date d2, Date d1) {
Date diff = new Date(d2.getTime() - d1.getTime());
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
calendar.setTime(diff);
int hours = calendar.get(Calendar.HOUR_OF_DAY);
int minutes = calendar.get(Calendar.MINUTE);
int seconds = calendar.get(Calendar.SECOND);
return hours + "h:" + minutes + "m:" + seconds + "s";
}
}