-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSchemaParser.java
More file actions
125 lines (102 loc) · 5.03 KB
/
Copy pathSchemaParser.java
File metadata and controls
125 lines (102 loc) · 5.03 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
import java.io.*;
import java.util.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.JsonNode;
public class SchemaParser {
static class ColumnMapping {
String columnName;
String field;
ColumnMapping(String columnName, String field) {
this.columnName = columnName;
this.field = field;
}
}
public static void main(String[] args) {
String textInputFile = "C:/path/to/input.txt";
String jsonInputFile = "C:/path/to/input.json";
String outputFile = "C:/path/to/output.csv";
try {
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(new File(jsonInputFile));
System.out.println("Full JSON content:");
System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(root));
// Verify the 'value' node
JsonNode valueNode = root.path("value");
System.out.println("\nValue node content:");
System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(valueNode));
// Check if 'table_columns' exists and is an array
JsonNode tableColumnsNode = valueNode.path("table_columns");
System.out.println("\nTable_columns node type: " + tableColumnsNode.getNodeType());
System.out.println("Is table_columns an array? " + tableColumnsNode.isArray());
List<ColumnMapping> mappings = readJsonMappings(root);
System.out.println("\nExtracted mappings:");
mappings.forEach(m -> System.out.printf("column_name: %s, field: %s%n", m.columnName, m.field));
generateCSV(textInputFile, outputFile, mappings);
} catch (IOException e) {
System.err.println("Error: " + e.getMessage());
e.printStackTrace();
}
}
private static List<ColumnMapping> readJsonMappings(JsonNode root) {
List<ColumnMapping> mappings = new ArrayList<>();
JsonNode tableColumns = root.path("value").path("table_columns");
if (!tableColumns.isArray()) {
System.err.println("Error: 'table_columns' is not an array!");
return mappings;
}
for (JsonNode column : tableColumns) {
String columnName = column.path("column_name").asText("");
String field = column.path("field").asText("");
if (!columnName.isEmpty() && !field.isEmpty()) {
mappings.add(new ColumnMapping(columnName, field));
System.out.printf("Added mapping: column_name='%s', field='%s'%n", columnName, field);
} else {
System.err.println("Skipping invalid column entry: " + column);
}
}
return mappings;
}
private static String findFactField(List<ColumnMapping> mappings, String column, String alias) {
String searchTerm = alias.isEmpty() ? column : alias;
System.out.printf("Searching for '%s'%n", searchTerm);
for (ColumnMapping mapping : mappings) {
if (mapping.columnName.equalsIgnoreCase(searchTerm)) {
return mapping.field;
}
}
return "";
}
private static void generateCSV(String inputFile, String outputFile, List<ColumnMapping> mappings) throws IOException {
try (BufferedReader reader = new BufferedReader(new FileReader(inputFile));
PrintWriter writer = new PrintWriter(new FileWriter(outputFile))) {
writer.println("Schema,Table,Column,Alias,FactField");
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.isEmpty()) continue;
String[] parts = line.split("\\s+as\\s+");
String fullPath = parts[0].trim();
String alias = parts.length > 1 ? parts[1].trim() : "";
String[] pathParts = fullPath.split("\\s*\\.\\s*");
if (pathParts.length >= 3) {
String schema = pathParts[0].trim();
String table = pathParts[1].trim();
String column = pathParts[2].trim();
String factField = findFactField(mappings, column, alias);
writer.printf("%s,%s,%s,%s,%s%n",
escapeCsvField(schema),
escapeCsvField(table),
escapeCsvField(column),
escapeCsvField(alias),
escapeCsvField(factField));
}
}
}
}
private static String escapeCsvField(String field) {
if (field.contains(",") || field.contains("\"") || field.contains("\n")) {
return "\"" + field.replace("\"", "\"\"") + "\"";
}
return field;
}
}