
org.apache.gobblin.converter.MetadataConverterWrapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gobblin-metadata Show documentation
Show all versions of gobblin-metadata Show documentation
A distributed data integration framework for streaming and batch data ecosystems.
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.gobblin.converter;
import java.io.IOException;
import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import javax.annotation.Nullable;
import org.apache.gobblin.configuration.State;
import org.apache.gobblin.configuration.WorkUnitState;
import org.apache.gobblin.metadata.types.Metadata;
import org.apache.gobblin.type.RecordWithMetadata;
/**
* Wraps a given converter to accept a RecordWithMetadata with a given data type, convert the underlying record using
* the wrapped converter, and pass the metadata through untouched
*/
public class MetadataConverterWrapper extends Converter> {
private final Converter innerConverter;
public MetadataConverterWrapper(Converter innerConverter) {
this.innerConverter = innerConverter;
}
@Override
public Converter> init(WorkUnitState workUnit) {
super.init(workUnit);
innerConverter.init(workUnit);
return this;
}
@Override
public void close()
throws IOException {
innerConverter.close();
}
@Override
public SO convertSchema(SI inputSchema, WorkUnitState workUnit)
throws SchemaConversionException {
return innerConverter.convertSchema(inputSchema, workUnit);
}
@Override
public Iterable> convertRecord(SO outputSchema, Object inputRecord, WorkUnitState workUnit)
throws DataConversionException {
final Metadata metadata = getMetadataFromRecord(inputRecord);
final DI innerRecord = getRecordFromRecord(inputRecord);
Iterable outputRecords = innerConverter.convertRecord(outputSchema, innerRecord, workUnit);
return Iterables.transform(outputRecords, new Function>() {
@Nullable
@Override
public RecordWithMetadata apply(@Nullable DO input) {
return new RecordWithMetadata<>(input, metadata);
}
});
}
@SuppressWarnings("unchecked")
private DI getRecordFromRecord(Object inputRecord) {
if (inputRecord instanceof RecordWithMetadata>) {
Object uncastedRecord = ((RecordWithMetadata) inputRecord).getRecord();
return (DI)uncastedRecord;
} else {
return (DI)inputRecord;
}
}
private Metadata getMetadataFromRecord(Object inputRecord) {
if (inputRecord instanceof RecordWithMetadata>) {
return ((RecordWithMetadata) inputRecord).getMetadata();
} else {
return new Metadata();
}
}
@Override
public State getFinalState() {
return innerConverter.getFinalState();
}
/**
* Intended to be overridden by converters who want to manipulate metadata as it flows through
* @param metadata
* @return
*/
protected Metadata convertMetadata(Metadata metadata) {
return metadata;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy