Search in sources :

Example 1 with JsonParser

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;
    };
}
Also used : DataType(org.apache.flink.table.types.DataType) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BiFunction(java.util.function.BiFunction) ExceptionUtils(org.apache.flink.util.ExceptionUtils) IOException(java.io.IOException) RowType(org.apache.flink.table.types.logical.RowType) Function(java.util.function.Function) StandardCharsets(java.nio.charset.StandardCharsets) ZoneId(java.time.ZoneId) JsonEncoding(org.apache.pulsar.shade.com.fasterxml.jackson.core.JsonEncoding) ArrayList(java.util.ArrayList) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) LogicalType(org.apache.flink.table.types.logical.LogicalType) JsonGenerator(org.apache.pulsar.shade.com.fasterxml.jackson.core.JsonGenerator) FieldsDataType(org.apache.flink.table.types.FieldsDataType) JsonToken(org.apache.pulsar.shade.com.fasterxml.jackson.core.JsonToken) TypeConversions(org.apache.flink.table.types.utils.TypeConversions) Row(org.apache.flink.types.Row) ParseException(java.text.ParseException) LogicalTypeRoot(org.apache.flink.table.types.logical.LogicalTypeRoot) JsonFactory(org.apache.pulsar.shade.com.fasterxml.jackson.core.JsonFactory) JsonParser(org.apache.pulsar.shade.com.fasterxml.jackson.core.JsonParser) ArrayList(java.util.ArrayList) RowType(org.apache.flink.table.types.logical.RowType) LogicalType(org.apache.flink.table.types.logical.LogicalType) IOException(java.io.IOException) BiFunction(java.util.function.BiFunction) Function(java.util.function.Function) DataType(org.apache.flink.table.types.DataType) FieldsDataType(org.apache.flink.table.types.FieldsDataType) JsonToken(org.apache.pulsar.shade.com.fasterxml.jackson.core.JsonToken)

Example 2 with JsonParser

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()));
    }
}
Also used : DataType(org.apache.flink.table.types.DataType) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BiFunction(java.util.function.BiFunction) ExceptionUtils(org.apache.flink.util.ExceptionUtils) IOException(java.io.IOException) RowType(org.apache.flink.table.types.logical.RowType) Function(java.util.function.Function) StandardCharsets(java.nio.charset.StandardCharsets) ZoneId(java.time.ZoneId) JsonEncoding(org.apache.pulsar.shade.com.fasterxml.jackson.core.JsonEncoding) ArrayList(java.util.ArrayList) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) LogicalType(org.apache.flink.table.types.logical.LogicalType) JsonGenerator(org.apache.pulsar.shade.com.fasterxml.jackson.core.JsonGenerator) FieldsDataType(org.apache.flink.table.types.FieldsDataType) JsonToken(org.apache.pulsar.shade.com.fasterxml.jackson.core.JsonToken) TypeConversions(org.apache.flink.table.types.utils.TypeConversions) Row(org.apache.flink.types.Row) ParseException(java.text.ParseException) LogicalTypeRoot(org.apache.flink.table.types.logical.LogicalTypeRoot) JsonFactory(org.apache.pulsar.shade.com.fasterxml.jackson.core.JsonFactory) JsonParser(org.apache.pulsar.shade.com.fasterxml.jackson.core.JsonParser) ArrayList(java.util.ArrayList) RowType(org.apache.flink.table.types.logical.RowType) LogicalType(org.apache.flink.table.types.logical.LogicalType) LogicalTypeRoot(org.apache.flink.table.types.logical.LogicalTypeRoot) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BiFunction(java.util.function.BiFunction) Function(java.util.function.Function) JsonGenerator(org.apache.pulsar.shade.com.fasterxml.jackson.core.JsonGenerator) JsonToken(org.apache.pulsar.shade.com.fasterxml.jackson.core.JsonToken) ParseException(java.text.ParseException) Row(org.apache.flink.types.Row) JsonParser(org.apache.pulsar.shade.com.fasterxml.jackson.core.JsonParser)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 StandardCharsets (java.nio.charset.StandardCharsets)2 ParseException (java.text.ParseException)2 ZoneId (java.time.ZoneId)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 BiFunction (java.util.function.BiFunction)2 Function (java.util.function.Function)2 Slf4j (lombok.extern.slf4j.Slf4j)2 DataType (org.apache.flink.table.types.DataType)2 FieldsDataType (org.apache.flink.table.types.FieldsDataType)2 LogicalType (org.apache.flink.table.types.logical.LogicalType)2 LogicalTypeRoot (org.apache.flink.table.types.logical.LogicalTypeRoot)2 RowType (org.apache.flink.table.types.logical.RowType)2 TypeConversions (org.apache.flink.table.types.utils.TypeConversions)2 Row (org.apache.flink.types.Row)2 ExceptionUtils (org.apache.flink.util.ExceptionUtils)2 JsonEncoding (org.apache.pulsar.shade.com.fasterxml.jackson.core.JsonEncoding)2 JsonFactory (org.apache.pulsar.shade.com.fasterxml.jackson.core.JsonFactory)2