/
LookupTrades.java
80 lines (65 loc) · 2.85 KB
/
LookupTrades.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
package tradeStorage;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
public class LookupTrades {
private final static DateFormat dateArgFormat = new SimpleDateFormat("yyyyMMdd");
public static void main(String[] args) throws IOException {
// args[0]: tall or flat
// args[1]: path to table
// args[2]: symbol
// args[3]: startdate
// args[4]: stopdate
// 1. Check for correct args. Parse args.
if (args.length < 3) { // quit if not enough args provided
System.out.println("Usage: LookupTrades {tall | flat} <path to table> symbol [startdate[, stopdate]]");
System.out.println("Format dates as YYYYMMDD.");
System.out.println("Example: LookupTrades flat /table/path CSCO 20131021 20131025");
return;
}
if (!(args[0].equals("tall") || args[0].equals("flat"))) { // quit if schema type not specified
System.out.println("Specify a schema type of 'tall' or 'flat'.");
return;
}
String symbol = args[2];
// Long startDate = Long.MIN_VALUE;
// Long stopDate = Long.MAX_VALUE;
Long startDate = dateArgFormat.parse("19710101", new ParsePosition(0)).getTime();
Long stopDate = dateArgFormat.parse("29991231", new ParsePosition(0)).getTime();
if (args.length == 3) { // Start & stop dates not provided
System.out.println("No start and stop dates specified. Retrieving all trades for " + symbol);
} else if (args.length == 4) { // Only a start date is provided
startDate = dateArgFormat.parse(args[3], new ParsePosition(0)).getTime();
System.out.println("Retrieving trades starting from " + args[3] + " for " + symbol);
} else { // Both start & stop dates are provided
startDate = dateArgFormat.parse(args[3], new ParsePosition(0)).getTime();
stopDate = dateArgFormat.parse(args[4], new ParsePosition(0)).getTime();
System.out.println("Retrieving trades for " + symbol + " from " + args[3] + " to " + args[4]);
}
// 2. Open connection to HBaseConfiguration and instantiate DAO to access the table.
Configuration conf = HBaseConfiguration.create();
TradeDAO tradeDao = null;
if (args[0].equals("tall")) {
tradeDao = new TradeDAOTall(conf, args[1]);
} else if (args[0].equals("flat")) {
tradeDao = new TradeDAOFlat(conf, args[1]);
}
System.out.println("Using DAO: " + tradeDao.getClass());
// 3. Read a set of trades via the DAO.
List<Trade> retrievedTradeSet = tradeDao.getTradesByDate(symbol, startDate, stopDate);
// 4. Print the results and exit.
printTrades(retrievedTradeSet);
tradeDao.close();
return;
}
private static void printTrades(List<Trade> trades) {
System.out.println("Printing " + trades.size() + " trades.");
for (Trade trade : trades) {
System.out.println(trade);
}
}
}