-
Notifications
You must be signed in to change notification settings - Fork 621
Expand file tree
/
Copy pathFileDataSet.java
More file actions
182 lines (153 loc) · 5.97 KB
/
FileDataSet.java
File metadata and controls
182 lines (153 loc) · 5.97 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
package com.clickhouse.benchmark.data;
import com.clickhouse.client.api.metadata.TableSchema;
import com.clickhouse.data.ClickHouseColumn;
import com.clickhouse.data.ClickHouseDataProcessor;
import com.clickhouse.data.ClickHouseFormat;
import com.clickhouse.data.ClickHouseRecord;
import com.clickhouse.data.ClickHouseValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.shaded.com.google.common.collect.Table;
import java.io.BufferedReader;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class FileDataSet implements DataSet{
private static final Logger LOGGER = LoggerFactory.getLogger(FileDataSet.class);
private final String name;
private final String createTableStmt;
private final TableSchema schema;
private final Map<String, String> metadata = new HashMap<>();
private List<byte[]> lines =null;
private List<Map<String, Object>> data;
public FileDataSet(String filePath) {
File srcFile = new File(filePath);
try (BufferedReader r = new BufferedReader(new java.io.FileReader(srcFile))) {
String line;
String name = null;
StringBuilder createStatement = null;
boolean doneCreateStatement = false;
boolean isMetadata = false;
boolean isData = false;
while ((line = r.readLine()) != null) {
if (name == null) {
name = line.trim();
} else if (line.startsWith("--Create Table Statement")) {
createStatement = new StringBuilder();
} else if (line.startsWith("--End Create Table Statement")) {
doneCreateStatement = true;
} else if (!doneCreateStatement && createStatement != null) {
createStatement.append(line).append("\n");
} else if (line.startsWith("--Metadata")) {
isMetadata = true;
} else if (line.startsWith("--End Metadata")) {
isMetadata = false;
} else if (isMetadata) {
String[] parts = line.split("=");
metadata.put(parts[0].toLowerCase().trim(), parts[1].trim());
} else if (line.startsWith("DATA>>")) {
int rows = Integer.parseInt(metadata.get("rows"));
lines = new ArrayList<>(rows);
isData = true;
} else if (isData) {
line = line + "\n"; // not optimal but ok for now.
lines.add(line.getBytes());
} else {
throw new IllegalArgumentException("Invalid file format: " + srcFile.getAbsolutePath());
}
}
this.name = name;
this.createTableStmt = createStatement != null ? createStatement.toString() : null;
this.schema = DataSets.parseSchema(createTableStmt);
LOGGER.info("Read " + lines.size() + " lines from " + srcFile.getAbsolutePath());
} catch (Exception e) {
throw new IllegalArgumentException("Failed to read file: " + srcFile.getAbsolutePath(), e);
}
}
@Override
public String getName() {
return name;
}
@Override
public String getTableName() {
return "data_" + name;
}
@Override
public int getSize() {
return lines.size();
}
@Override
public String getCreateTableString() {
return createTableStmt;
}
@Override
public String getTrucateTableString() {
return "TRUNCATE TABLE " + getTableName();
}
private TableSchema tableSchema = new TableSchema();
@Override
public TableSchema getSchema() {
return tableSchema;
}
@Override
public List<ClickHouseFormat> supportedFormats() {
return Collections.singletonList(ClickHouseFormat.CSV);
}
@Override
public List<byte[]> getBytesList(ClickHouseFormat format) {
return lines;
}
@Override
public List<Map<String, Object>> getRows() {
return data;
}
@Override
public ClickHouseFormat getFormat() {
return ClickHouseFormat.CSV;
}
private List<ClickHouseRecord> clickHouseRecords;
@Override
public List<ClickHouseRecord> getClickHouseRecords() {
return clickHouseRecords;
}
@Override
public void setClickHouseRecords(List<ClickHouseRecord> records) {
this.clickHouseRecords = records;
List<ClickHouseColumn> columns = tableSchema.getColumns();
data = new ArrayList<>(records.size());
for (ClickHouseRecord record : records) {
Iterator<ClickHouseValue> vIter = record.iterator();
int i = 0;
Map<String, Object> row = new HashMap<>();
while (vIter.hasNext()) {
ClickHouseValue v = vIter.next();
row.put(columns.get(i++).getColumnName(), v.asObject());
}
data.add(row);
}
}
private ClickHouseDataProcessor dataProcessor;
@Override
public ClickHouseDataProcessor getClickHouseDataProcessor() {
return dataProcessor;
}
@Override
public void setClickHouseDataProcessor(ClickHouseDataProcessor dataProcessor) {
this.dataProcessor = dataProcessor;
for (ClickHouseColumn column : dataProcessor.getColumns()) {
tableSchema.addColumn(column.getColumnName(), column.getOriginalTypeName());
}
}
@Override
public String toString() {
return "FileDataSet{" +
"name='" + name + '\'' +
", createTableStmt='" + createTableStmt + '\'' +
", metadata=" + metadata +
'}';
}
}