Skip to content
This repository was archived by the owner on May 3, 2019. It is now read-only.

Commit 38d63d3

Browse files
committed
Support uuid and host fields
1 parent 3267c77 commit 38d63d3

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

src/main/java/com/proofpoint/hive/serde/JsonEventSerde.java

+29-11
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@
2323
import org.joda.time.format.DateTimeFormatter;
2424
import org.joda.time.format.ISODateTimeFormat;
2525

26+
import java.util.Map;
2627
import java.util.Properties;
2728

2829
public class JsonEventSerde
2930
extends JsonSerde
3031
{
3132
public static final DateTimeFormatter ISO_FORMATTER = ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC);
3233
public static final DateTimeFormatter HIVE_FORMATTER = DateTimeFormat.forPattern("yyyy-MM-dd' 'HH:mm:ss").withZone(DateTimeZone.UTC);
34+
private Integer uuidColumn;
35+
private Integer hostColumn;
3336
private Integer timestampColumn;
3437

3538
@Override
@@ -38,7 +41,10 @@ public void initialize(Configuration configuration, Properties table)
3841
{
3942
super.initialize(configuration, table);
4043

41-
timestampColumn = columnNameMap.getColumnNames(rootTypeInfo).get("ts");
44+
Map<String, Integer> columnNames = columnNameMap.getColumnNames(rootTypeInfo);
45+
uuidColumn = columnNames.get("uuid");
46+
hostColumn = columnNames.get("host");
47+
timestampColumn = columnNames.get("ts");
4248
}
4349

4450
@Override
@@ -55,29 +61,41 @@ protected Object[] buildStruct(JsonNode tree)
5561

5662
Object[] struct = processFields(dataNode);
5763

64+
if (uuidColumn != null) {
65+
struct[uuidColumn] = getTextNode(tree, "uuid");
66+
}
67+
if (hostColumn != null) {
68+
struct[hostColumn] = getTextNode(tree, "host");
69+
}
5870
if (timestampColumn != null) {
59-
struct[timestampColumn] = HIVE_FORMATTER.print(parseTimestamp(tree));
71+
long ts = parseTimestamp(getTextNode(tree, "timestamp"));
72+
struct[timestampColumn] = HIVE_FORMATTER.print(ts);
6073
}
6174

6275
return struct;
6376
}
6477

65-
private static long parseTimestamp(JsonNode tree)
78+
private static long parseTimestamp(String timestamp)
6679
throws SerDeException
6780
{
68-
if (!tree.has("timestamp")) {
69-
throw new SerDeException("timestamp field is missing");
70-
}
71-
JsonNode node = tree.get("timestamp");
72-
if (!node.isTextual()) {
73-
throw new SerDeException("timestamp field is not text");
74-
}
75-
String timestamp = node.getTextValue();
7681
try {
7782
return ISO_FORMATTER.parseMillis(timestamp);
7883
}
7984
catch (Exception e) {
8085
throw new SerDeException("invalid timestamp: " + timestamp);
8186
}
8287
}
88+
89+
private static String getTextNode(JsonNode tree, String field)
90+
throws SerDeException
91+
{
92+
if (!tree.has(field)) {
93+
throw new SerDeException(field + " field is missing");
94+
}
95+
JsonNode node = tree.get(field);
96+
if (!node.isTextual()) {
97+
throw new SerDeException(field + " field is not text");
98+
}
99+
return node.getTextValue();
100+
}
83101
}

0 commit comments

Comments
 (0)