|
5 | 5 | import java.io.IOException; |
6 | 6 | import java.io.OutputStream; |
7 | 7 | import java.util.zip.GZIPOutputStream; |
8 | | - |
9 | 8 | import org.embulk.output.bigquery_java.config.PluginTask; |
10 | | - |
11 | 9 | import org.slf4j.Logger; |
12 | 10 | import org.slf4j.LoggerFactory; |
13 | 11 |
|
14 | 12 | public class BigqueryFileWriter { |
15 | | - private final Logger logger = LoggerFactory.getLogger(BigqueryFileWriter.class); |
16 | | - private PluginTask task; |
17 | | - private String compression; |
18 | | - private OutputStream os; |
19 | | - private long count = 0; |
| 13 | + private final Logger logger = LoggerFactory.getLogger(BigqueryFileWriter.class); |
| 14 | + private PluginTask task; |
| 15 | + private String compression; |
| 16 | + private OutputStream os; |
| 17 | + private long count = 0; |
20 | 18 |
|
21 | | - public BigqueryFileWriter(PluginTask task) { |
22 | | - this.task = task; |
23 | | - this.compression = this.task.getCompression(); |
24 | | - } |
| 19 | + public BigqueryFileWriter(PluginTask task) { |
| 20 | + this.task = task; |
| 21 | + this.compression = this.task.getCompression(); |
| 22 | + } |
25 | 23 |
|
26 | | - public BigqueryFileWriter() { |
27 | | - } |
| 24 | + public BigqueryFileWriter() {} |
28 | 25 |
|
29 | | - public void setTask(PluginTask task) { |
30 | | - this.task = task; |
31 | | - } |
32 | | - |
33 | | - public void setCompression(String compression) { |
34 | | - this.compression = compression; |
35 | | - } |
| 26 | + public void setTask(PluginTask task) { |
| 27 | + this.task = task; |
| 28 | + } |
36 | 29 |
|
37 | | - public OutputStream open(String path) throws IOException { |
38 | | - logger.info("embulk-output-bigquery: create {}", path); |
| 30 | + public void setCompression(String compression) { |
| 31 | + this.compression = compression; |
| 32 | + } |
39 | 33 |
|
40 | | - this.os = new FileOutputStream(path); |
41 | | - if (this.compression.equals("GZIP")) { |
42 | | - this.os = new GZIPOutputStream(this.os); |
43 | | - } |
44 | | - // embulk default page size |
45 | | - this.os = new BufferedOutputStream(this.os, 1024 * 32); |
| 34 | + public OutputStream open(String path) throws IOException { |
| 35 | + logger.info("embulk-output-bigquery: create {}", path); |
46 | 36 |
|
47 | | - return this.os; |
| 37 | + this.os = new FileOutputStream(path); |
| 38 | + if (this.compression.equals("GZIP")) { |
| 39 | + this.os = new GZIPOutputStream(this.os); |
48 | 40 | } |
| 41 | + // embulk default page size |
| 42 | + this.os = new BufferedOutputStream(this.os, 1024 * 32); |
49 | 43 |
|
50 | | - public OutputStream outputStream() throws IOException { |
51 | | - if (this.os != null) { |
52 | | - return this.os; |
53 | | - } |
54 | | - // TODO: pid, thread id format config |
55 | | - String path = String.format("%s.%d.%d%s", |
56 | | - this.task.getPathPrefix().get(), |
57 | | - BigqueryUtil.getPID(), |
58 | | - Thread.currentThread().getId(), |
59 | | - this.task.getFileExt().get()); |
60 | | - return open(path); |
61 | | - } |
| 44 | + return this.os; |
| 45 | + } |
62 | 46 |
|
63 | | - public void write(byte[] bytes) { |
64 | | - try { |
65 | | - outputStream().write(bytes); |
66 | | - this.count++; |
67 | | - } catch (Exception e) { |
68 | | - logger.info(e.getMessage()); |
69 | | - } |
| 47 | + public OutputStream outputStream() throws IOException { |
| 48 | + if (this.os != null) { |
| 49 | + return this.os; |
70 | 50 | } |
71 | | - |
72 | | - public long getCount() { |
73 | | - return this.count; |
| 51 | + // TODO: pid, thread id format config |
| 52 | + String path = |
| 53 | + String.format( |
| 54 | + "%s.%d.%d%s", |
| 55 | + this.task.getPathPrefix().get(), |
| 56 | + BigqueryUtil.getPID(), |
| 57 | + Thread.currentThread().getId(), |
| 58 | + this.task.getFileExt().get()); |
| 59 | + return open(path); |
| 60 | + } |
| 61 | + |
| 62 | + public void write(byte[] bytes) { |
| 63 | + try { |
| 64 | + outputStream().write(bytes); |
| 65 | + this.count++; |
| 66 | + } catch (Exception e) { |
| 67 | + logger.info(e.getMessage()); |
74 | 68 | } |
75 | | - |
76 | | - public void close() { |
77 | | - try { |
78 | | - this.outputStream().flush(); |
79 | | - this.outputStream().close(); |
80 | | - } catch (Exception e) { |
81 | | - logger.info(e.getMessage()); |
82 | | - } |
| 69 | + } |
| 70 | + |
| 71 | + public long getCount() { |
| 72 | + return this.count; |
| 73 | + } |
| 74 | + |
| 75 | + public void close() { |
| 76 | + try { |
| 77 | + this.outputStream().flush(); |
| 78 | + this.outputStream().close(); |
| 79 | + } catch (Exception e) { |
| 80 | + logger.info(e.getMessage()); |
83 | 81 | } |
| 82 | + } |
84 | 83 | } |
85 | | - |
86 | | - |
87 | | - |
0 commit comments