use of org.apache.pulsar.shade.com.fasterxml.jackson.core.JsonParser in project pulsar-flink by streamnative.
the class JacksonRecordParser method makeStructRootConverter.
private BiFunction<JsonParser, Row, Row> makeStructRootConverter(FieldsDataType st) {
List<String> fieldNames = ((RowType) st.getLogicalType()).getFieldNames();
List<Function<JsonParser, Object>> fieldConverters = new ArrayList<>();
RowType rowType = (RowType) st.getLogicalType();
for (int i = 0; i < fieldNames.size(); i++) {
// String fieldName = fieldNames.get(i);
LogicalType logicalType = rowType.getTypeAt(i);
DataType type = TypeConversions.fromLogicalToDataType(logicalType);
fieldConverters.add(makeConverter(type));
}
return (parser, row) -> {
try {
parseJsonToken(parser, st, new PartialFunc() {
@Override
public boolean isDefinedAt(JsonToken token) {
return token == JsonToken.START_OBJECT || token == JsonToken.START_ARRAY;
}
@Override
public Object apply(JsonToken token) {
if (token == JsonToken.START_OBJECT) {
try {
return convertObject(parser, st, fieldConverters, row);
} catch (IOException e) {
suroundWithRuntimeE(e);
}
} else {
throw new IllegalStateException("Message should be a single JSON object");
}
return null;
}
});
} catch (IOException e) {
suroundWithRuntimeE(e);
}
return null;
};
}
use of org.apache.pulsar.shade.com.fasterxml.jackson.core.JsonParser in project pulsar-flink by streamnative.
the class JacksonRecordParser method makeConverter.
private Function<JsonParser, Object> makeConverter(DataType dataType) {
LogicalTypeRoot tpe = dataType.getLogicalType().getTypeRoot();
switch(tpe) {
case BOOLEAN:
return parser -> {
try {
return parseJsonToken(parser, dataType, new PartialFunc() {
@Override
public boolean isDefinedAt(JsonToken token) {
return token == JsonToken.VALUE_TRUE || token == JsonToken.VALUE_FALSE;
}
@Override
public Object apply(JsonToken token) {
return token == JsonToken.VALUE_TRUE;
}
});
} catch (IOException e) {
suroundWithRuntimeE(e);
}
return null;
};
case TINYINT:
return parser -> {
try {
return parseJsonToken(parser, dataType, new PartialFunc() {
@Override
public boolean isDefinedAt(JsonToken token) {
return token == JsonToken.VALUE_NUMBER_INT;
}
@Override
public Object apply(JsonToken token) {
try {
return parser.getByteValue();
} catch (IOException e) {
suroundWithRuntimeE(e);
}
return null;
}
});
} catch (IOException e) {
suroundWithRuntimeE(e);
}
return null;
};
case SMALLINT:
return parser -> {
try {
return parseJsonToken(parser, dataType, new PartialFunc() {
@Override
public boolean isDefinedAt(JsonToken token) {
return token == JsonToken.VALUE_NUMBER_INT;
}
@Override
public Object apply(JsonToken token) {
try {
return parser.getShortValue();
} catch (IOException e) {
suroundWithRuntimeE(e);
}
return null;
}
});
} catch (IOException e) {
suroundWithRuntimeE(e);
}
return null;
};
case INTEGER:
return parser -> {
try {
return parseJsonToken(parser, dataType, new PartialFunc() {
@Override
public boolean isDefinedAt(JsonToken token) {
return token == JsonToken.VALUE_NUMBER_INT;
}
@Override
public Object apply(JsonToken token) {
try {
return parser.getIntValue();
} catch (IOException e) {
suroundWithRuntimeE(e);
}
return null;
}
});
} catch (IOException e) {
suroundWithRuntimeE(e);
}
return null;
};
case BIGINT:
return parser -> {
try {
return parseJsonToken(parser, dataType, new PartialFunc() {
@Override
public boolean isDefinedAt(JsonToken token) {
return token == JsonToken.VALUE_NUMBER_INT;
}
@Override
public Object apply(JsonToken token) {
try {
return parser.getLongValue();
} catch (IOException e) {
suroundWithRuntimeE(e);
}
return null;
}
});
} catch (IOException e) {
suroundWithRuntimeE(e);
}
return null;
};
case FLOAT:
return parser -> {
try {
return parseJsonToken(parser, dataType, new PartialFunc() {
@Override
public boolean isDefinedAt(JsonToken token) {
return token == JsonToken.VALUE_NUMBER_INT || token == JsonToken.VALUE_NUMBER_FLOAT || token == JsonToken.VALUE_STRING;
}
@Override
public Object apply(JsonToken token) {
try {
if (token == JsonToken.VALUE_NUMBER_INT || token == JsonToken.VALUE_NUMBER_FLOAT) {
return parser.getFloatValue();
} else {
String txt = parser.getText();
if (txt.equals("NaN")) {
return Float.NaN;
} else if (txt.equals("Infinity")) {
return Float.POSITIVE_INFINITY;
} else if (txt.equals("-Infinity")) {
return Float.NEGATIVE_INFINITY;
} else {
throw new IllegalArgumentException("Cannot parse " + txt + " as Float");
}
}
} catch (IOException e) {
suroundWithRuntimeE(e);
}
return null;
}
});
} catch (IOException e) {
suroundWithRuntimeE(e);
}
return null;
};
case DOUBLE:
return parser -> {
try {
return parseJsonToken(parser, dataType, new PartialFunc() {
@Override
public boolean isDefinedAt(JsonToken token) {
return token == JsonToken.VALUE_NUMBER_INT || token == JsonToken.VALUE_NUMBER_FLOAT || token == JsonToken.VALUE_STRING;
}
@Override
public Object apply(JsonToken token) {
try {
if (token == JsonToken.VALUE_NUMBER_INT || token == JsonToken.VALUE_NUMBER_FLOAT) {
return parser.getDoubleValue();
} else {
String txt = parser.getText();
if (txt.equals("NaN")) {
return Float.NaN;
} else if (txt.equals("Infinity")) {
return Float.POSITIVE_INFINITY;
} else if (txt.equals("-Infinity")) {
return Float.NEGATIVE_INFINITY;
} else {
throw new IllegalArgumentException("Cannot parse " + txt + " as Float");
}
}
} catch (IOException e) {
suroundWithRuntimeE(e);
}
return null;
}
});
} catch (IOException e) {
suroundWithRuntimeE(e);
}
return null;
};
case VARCHAR:
return parser -> {
try {
return parseJsonToken(parser, dataType, new PartialFunc() {
@Override
public boolean isDefinedAt(JsonToken token) {
return true;
}
@Override
public Object apply(JsonToken token) {
try {
if (token == JsonToken.VALUE_STRING) {
return parser.getText();
} else {
ByteArrayOutputStream writer = new ByteArrayOutputStream();
try (JsonGenerator generator = factory.createGenerator(writer, JsonEncoding.UTF8)) {
generator.copyCurrentStructure(parser);
}
return new String(writer.toByteArray(), StandardCharsets.UTF_8);
}
} catch (IOException e) {
suroundWithRuntimeE(e);
}
return null;
}
});
} catch (IOException e) {
suroundWithRuntimeE(e);
}
return null;
};
case TIMESTAMP_WITHOUT_TIME_ZONE:
return parser -> {
try {
return parseJsonToken(parser, dataType, new PartialFunc() {
@Override
public boolean isDefinedAt(JsonToken token) {
return token == JsonToken.VALUE_NUMBER_INT || token == JsonToken.VALUE_STRING;
}
@Override
public Object apply(JsonToken token) {
try {
if (token == JsonToken.VALUE_STRING) {
String v = parser.getText();
long t = options.getTimestampFormat().parse(v).getTime() * 1000L;
return DateTimeUtils.toJavaTimestamp(t).toLocalDateTime();
} else {
return DateTimeUtils.toJavaTimestamp(parser.getLongValue() * 1000000L).toLocalDateTime();
}
} catch (IOException | ParseException e) {
suroundWithRuntimeE(e);
}
return null;
}
});
} catch (IOException e) {
suroundWithRuntimeE(e);
}
return null;
};
case DATE:
return parser -> {
try {
return parseJsonToken(parser, dataType, new PartialFunc() {
@Override
public boolean isDefinedAt(JsonToken token) {
return token == JsonToken.VALUE_STRING;
}
@Override
public Object apply(JsonToken token) {
try {
String v = parser.getText();
int t = DateTimeUtils.millisToDays(options.getDateFormat().parse(v).getTime());
return DateTimeUtils.toJavaDate(t).toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
} catch (IOException | ParseException e) {
suroundWithRuntimeE(e);
}
return null;
}
});
} catch (IOException e) {
suroundWithRuntimeE(e);
}
return null;
};
case VARBINARY:
return parser -> {
try {
return parseJsonToken(parser, dataType, new PartialFunc() {
@Override
public boolean isDefinedAt(JsonToken token) {
return token == JsonToken.VALUE_STRING;
}
@Override
public Object apply(JsonToken token) {
try {
return parser.getBinaryValue();
} catch (IOException e) {
suroundWithRuntimeE(e);
}
return null;
}
});
} catch (IOException e) {
suroundWithRuntimeE(e);
}
return null;
};
case DECIMAL:
return parser -> {
try {
return parseJsonToken(parser, dataType, new PartialFunc() {
@Override
public boolean isDefinedAt(JsonToken token) {
return token == JsonToken.VALUE_NUMBER_INT || token == JsonToken.VALUE_NUMBER_FLOAT;
}
@Override
public Object apply(JsonToken token) {
try {
return parser.getDecimalValue();
} catch (IOException e) {
suroundWithRuntimeE(e);
}
return null;
}
});
} catch (IOException e) {
suroundWithRuntimeE(e);
}
return null;
};
case ROW:
RowType rowType = (RowType) dataType.getLogicalType();
List<String> fieldNames = rowType.getFieldNames();
List<Function<JsonParser, Object>> fieldConverters = new ArrayList<Function<JsonParser, Object>>();
for (int i = 0; i < fieldNames.size(); i++) {
LogicalType logicalType = rowType.getTypeAt(i);
fieldConverters.add(makeConverter(TypeConversions.fromLogicalToDataType(logicalType)));
}
return parser -> {
try {
return parseJsonToken(parser, dataType, new PartialFunc() {
@Override
public boolean isDefinedAt(JsonToken token) {
return token == JsonToken.START_OBJECT;
}
@Override
public Object apply(JsonToken token) {
try {
Row record = new Row(rowType.getFieldCount());
return convertObject(parser, (FieldsDataType) dataType, fieldConverters, record);
} catch (IOException e) {
suroundWithRuntimeE(e);
}
return null;
}
});
} catch (IOException e) {
suroundWithRuntimeE(e);
}
return null;
};
default:
throw new IllegalStateException(String.format("Failed to parse a value for data type %s (current: %s).", dataType.toString(), tpe.toString()));
}
}
Aggregations