-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathTickDataReadWrite.java
More file actions
170 lines (158 loc) · 4.89 KB
/
TickDataReadWrite.java
File metadata and controls
170 lines (158 loc) · 4.89 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
/*
Copyright [2014] [Iljoon Hwang, ih138@columbia.edu]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.cijhwang.hadoop;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class TickDataReadWrite{
/*
* Write the collected tick data to Hadoop
* input:
* String line: tick data
* String loc: Hadoop file system path with file name
*/
private static void writeTick(String line, String loc) {
try {
Path pt = new Path(loc);
FileSystem fs = FileSystem.get(new Configuration());
BufferedWriter br=new BufferedWriter(new OutputStreamWriter(fs.create(pt,true)));
br.write(line);
br.close();
} catch (Exception e) {
System.out.println("File not found");
}
}
/*
* Parsing the tick data from Yahoo finance
* input:
* String url: Yahoo finance tickdata url
* otput:
* String res: tick data collected
*/
private static String getTick(String url) throws IOException {
URL yahoofinance = new URL(url);
URLConnection yc = yahoofinance.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream(), "UTF-8"));
String inputLine;
StringBuilder res= new StringBuilder();
boolean start=false;
// Take close price only
while ((inputLine = in.readLine()) != null){
if(inputLine.split(":")[0].equalsIgnoreCase("volume")){
start = true;
continue;
}
else if(start) {
res.append(inputLine.split(",")[1]);
res.append('\n');
}
else
continue;
}
in.close();
return res.toString();
}
/*
* Collect the all target tickers
* output: List<String> result: List of tickers
*/
public static List<String> getTickers (){
List<String> result=new ArrayList<String>();
BufferedReader breader = null;
try {
String line;
// breader = new BufferedReader(new FileReader("/home/hwang/bdproject/sp500"));
breader = new BufferedReader(new FileReader("/user/hduser/Real-time-Risk-Management-System/sp500"));
while ( (line=breader.readLine()) != null) {
result.add(line.split("\\t")[0]);
}
}
catch (IOException e) {
e.printStackTrace();
}
return result;
}
/*
* main
* input argument:
* 0 : 10 days 5-minute-tick-data
* 1 : 1 day 1-minute-tick-data
*/
public static void main(String[] args) {
int firstArg=0;
String loc_pre=null;
String loc=null;
String url2=null;
if (args.length > 0) {
try {
firstArg = Integer.parseInt(args[0]); } catch (NumberFormatException e) {
System.err.println("Argument" + args[0] + " must be an integer.");
System.exit(1);
}
if (firstArg == 0){
loc_pre = "hdfs://master:8020/user/hwang/data/";
url2 = "/chartdata;type=quote;range=10d/csv";
}
else if (firstArg == 1){
loc_pre = "hdfs://master:8020/user/hwang/dailyData/";
url2 = "/chartdata;type=quote;range=1d/csv";
}
else {
System.err.println("Argument" + args[0] + " must be an integer [0] for 10 day 5 min data or [1] for intra day 1 min data.");
System.exit(1);
}
}
else {
System.out.println("Usage: hadoop jar com.cijhwang.hadoop.TickDataReadWrite [ 0: 10 day 5 min data, 1: intra day 1 min data]");
System.exit(1);
}
// Read Ticker list
List<String> tickers_ls = new ArrayList<String>();
tickers_ls = TickDataReadWrite.getTickers();
Iterator<String> iter = tickers_ls.iterator();
while(iter.hasNext()) {
String url1 = "http://chartapi.finance.yahoo.com/instrument/1.0/";
String ticker = (String) iter.next();
String url = url1 + ticker + url2;
String res=null;
try {
res = TickDataReadWrite.getTick(url);
} catch (IOException e) {
e.printStackTrace();
}
// Write daily tick data to hadoop
loc = loc_pre + ticker;
try {
TickDataReadWrite.writeTick(res, loc);
}
catch (Exception e) {
e.printStackTrace();
}
finally {
continue;
}
}
}
}