spout

 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
import com.lambdaworks.redis.protocol.Charsets;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.nio.file.*;
import static java.lang.Thread.sleep;
class GeneratorSpout implements IBaseSpout {
OutputCollector outputCollector;
private BufferedReader reader = null;
private Iterator<File> fileIt = null;
private File file = null;
@Override
public void init(OutputCollector outputCollector) {
this.outputCollector = outputCollector;
fillFills();
}
private void fillFills() {
File dir = new File("C:\\Users\\Марина\\Desktop\\input");
File[] arraySimple = dir.listFiles(); // получили список файлов в dir
if (arraySimple.length == 0) {
fileIt = null;
return;
}
ArrayList<File> arrayOfStrings = new ArrayList<File>(arraySimple.length);
for(File item:arraySimple) {
arrayOfStrings.add(item);
}
fileIt = arrayOfStrings.iterator();
}
@Override
public void nextTuple() {
if (reader == null) {
if (fileIt != null) {
if (fileIt.hasNext()) {
file = fileIt.next();
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file),
Charsets.ASCII));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} else {
fileIt = null;
}
} else {
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
fillFills();
}
} else {
String line = null;
try {
line = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (line != null) {
outputCollector.put("splitterbolt", line);
} else {
reader = null;
move();
}
}
}
private void move() {
// ????????????
}
}