All Downloads are FREE. Search and download functionalities are using the official Maven repository.

uk.gov.gchq.gaffer.spark.operation.dataframe.ConvertElementToRow Maven / Gradle / Ivy

There is a newer version: 2.3.1
Show newest version
/*
 * Copyright 2016-2020 Crown Copyright
 *
 * Licensed 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 uk.gov.gchq.gaffer.spark.operation.dataframe;

import org.apache.spark.sql.Row;
import org.apache.spark.sql.Row$;
import scala.collection.mutable.MutableList;
import scala.runtime.AbstractFunction1;

import uk.gov.gchq.gaffer.data.element.Edge;
import uk.gov.gchq.gaffer.data.element.Element;
import uk.gov.gchq.gaffer.data.element.Entity;
import uk.gov.gchq.gaffer.data.element.ReservedPropertyNames;
import uk.gov.gchq.gaffer.data.element.id.EdgeId;
import uk.gov.gchq.gaffer.spark.operation.dataframe.converter.exception.ConversionException;
import uk.gov.gchq.gaffer.spark.operation.dataframe.converter.property.Converter;
import uk.gov.gchq.gaffer.spark.operation.dataframe.converter.schema.SchemaToStructTypeConverter;

import java.io.Serializable;
import java.util.LinkedHashSet;
import java.util.Map;

/**
 * Class to convert a {@link Element} into a {@link Row} object for use in a Spark
 * SQL dataframe.
 */
public class ConvertElementToRow extends AbstractFunction1
        implements Serializable {

    private static final long serialVersionUID = -361239524365928808L;
    private final LinkedHashSet properties;
    private final Map propertyNeedsConversion;
    private final Map convertersByProperty;

    public ConvertElementToRow(final LinkedHashSet properties,
                               final Map propertyNeedsConversion,
                               final Map convertersByProperty) {
        this.properties = properties;
        this.propertyNeedsConversion = propertyNeedsConversion;
        this.convertersByProperty = convertersByProperty;
    }

    @Override
    public Row apply(final Element element) {
        final MutableList fields = new MutableList<>();
        for (final String property : properties) {
            switch (property) {
                case SchemaToStructTypeConverter.GROUP:
                    fields.appendElem(element.getGroup());
                    break;
                case SchemaToStructTypeConverter.SRC_COL_NAME:
                    if (element instanceof Edge) {
                        fields.appendElem(((Edge) element).getSource());
                    } else {
                        fields.appendElem(null);
                    }
                    break;
                case SchemaToStructTypeConverter.DST_COL_NAME:
                    if (element instanceof Edge) {
                        fields.appendElem(((Edge) element).getDestination());
                    } else {
                        fields.appendElem(null);
                    }
                    break;
                case SchemaToStructTypeConverter.VERTEX_COL_NAME:
                    if (element instanceof Entity) {
                        fields.appendElem(((Entity) element).getVertex());
                    } else {
                        fields.appendElem(null);
                    }
                    break;
                case SchemaToStructTypeConverter.DIRECTED_COL_NAME:
                    if (element instanceof Entity) {
                        fields.appendElem(null);
                    } else {
                        fields.appendElem(((Edge) element).getDirectedType().isDirected());
                    }
                    break;
                case SchemaToStructTypeConverter.MATCHED_VERTEX_COL_NAME:
                    if (element instanceof Entity) {
                        fields.appendElem(null);
                    } else {
                        final EdgeId.MatchedVertex matchedVertex = ((Edge) element).getMatchedVertex();
                        if (null != matchedVertex) {
                            fields.appendElem(matchedVertex.toString());
                        } else {
                            fields.appendElem(null);
                        }
                    }
                    break;
                default:
                    final Object value = element.getProperties().get(property);
                    if (null == value) {
                        fields.appendElem(null);
                    } else if (!ReservedPropertyNames.contains(property)) {
                        if (!propertyNeedsConversion.get(property)) {
                            fields.appendElem(element.getProperties().get(property));
                        } else {
                            final Converter converter = convertersByProperty.get(property);
                            if (null != converter) {
                                try {
                                    fields.appendElem(converter.convert(value));
                                } catch (final ConversionException e) {
                                    fields.appendElem(null);
                                }
                            } else {
                                fields.appendElem(null);
                            }
                        }
                    }
            }
        }
        return Row$.MODULE$.fromSeq(fields);
    }
}