Search in sources :

Example 1 with AvroAttributeValues

use of org.locationtech.geowave.adapter.vector.avro.AvroAttributeValues in project geowave by locationtech.

the class GeoWaveAvroFeatureUtils method buildAttributeValue.

/**
 * Create an AttributeValue from the SimpleFeature's attributes
 *
 * @param sf
 * @param sft
 * @return the attribute value
 */
public static synchronized AvroAttributeValues buildAttributeValue(final SimpleFeature sf, final SimpleFeatureType sft) {
    final AvroAttributeValues attributeValue = new AvroAttributeValues();
    final List<ByteBuffer> values = new ArrayList<>(sft.getAttributeCount());
    attributeValue.setSerializationVersion(ByteBuffer.wrap(new byte[] { FieldUtils.SERIALIZATION_VERSION }));
    attributeValue.setFid(sf.getID());
    for (final AttributeDescriptor attr : sft.getAttributeDescriptors()) {
        final Object o = sf.getAttribute(attr.getLocalName());
        byte[] bytes;
        if (o instanceof Geometry) {
            bytes = WKB_WRITER.write((Geometry) o);
        } else {
            final FieldWriter fw = FieldUtils.getDefaultWriterForClass(attr.getType().getBinding());
            bytes = fw.writeField(o);
        }
        values.add(ByteBuffer.wrap(bytes));
    }
    attributeValue.setValues(values);
    return attributeValue;
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) AvroAttributeValues(org.locationtech.geowave.adapter.vector.avro.AvroAttributeValues) ArrayList(java.util.ArrayList) AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor) ByteBuffer(java.nio.ByteBuffer) FieldWriter(org.locationtech.geowave.core.store.data.field.FieldWriter)

Example 2 with AvroAttributeValues

use of org.locationtech.geowave.adapter.vector.avro.AvroAttributeValues in project geowave by locationtech.

the class VectorLocalExportCommand method execute.

@Override
public void execute(final OperationParams params) throws IOException, CQLException {
    // Ensure we have all the required arguments
    if (parameters.size() != 1) {
        throw new ParameterException("Requires arguments: <store name>");
    }
    final String storeName = parameters.get(0);
    // Attempt to load store.
    inputStoreOptions = CLIUtils.loadStore(storeName, getGeoWaveConfigFile(params), params.getConsole());
    final PersistentAdapterStore adapterStore = inputStoreOptions.createAdapterStore();
    final IndexStore indexStore = inputStoreOptions.createIndexStore();
    final DataStore dataStore = inputStoreOptions.createDataStore();
    final InternalAdapterStore internalAdapterStore = inputStoreOptions.createInternalAdapterStore();
    try (final DataFileWriter<AvroSimpleFeatureCollection> dfw = new DataFileWriter<>(new GenericDatumWriter<AvroSimpleFeatureCollection>(AvroSimpleFeatureCollection.SCHEMA$))) {
        dfw.setCodec(CodecFactory.snappyCodec());
        dfw.create(AvroSimpleFeatureCollection.SCHEMA$, options.getOutputFile());
        // get appropriate feature adapters
        final List<GeotoolsFeatureDataAdapter> featureAdapters = new ArrayList<>();
        if ((options.getTypeNames() != null) && (options.getTypeNames().size() > 0)) {
            for (final String typeName : options.getTypeNames()) {
                final short adapterId = internalAdapterStore.getAdapterId(typeName);
                final InternalDataAdapter<?> internalDataAdapter = adapterStore.getAdapter(adapterId);
                if (internalDataAdapter == null) {
                    params.getConsole().println("Type '" + typeName + "' not found");
                    continue;
                } else if (!(internalDataAdapter.getAdapter() instanceof GeotoolsFeatureDataAdapter)) {
                    params.getConsole().println("Type '" + typeName + "' does not support vector export. Instance of " + internalDataAdapter.getAdapter().getClass());
                    continue;
                }
                featureAdapters.add((GeotoolsFeatureDataAdapter) internalDataAdapter.getAdapter());
            }
        } else {
            final InternalDataAdapter<?>[] adapters = adapterStore.getAdapters();
            for (final InternalDataAdapter<?> adapter : adapters) {
                if (adapter.getAdapter() instanceof GeotoolsFeatureDataAdapter) {
                    featureAdapters.add((GeotoolsFeatureDataAdapter) adapter.getAdapter());
                }
            }
        }
        if (featureAdapters.isEmpty()) {
            params.getConsole().println("Unable to find any vector data types in store");
        }
        Index queryIndex = null;
        if (options.getIndexName() != null) {
            queryIndex = indexStore.getIndex(options.getIndexName());
            if (queryIndex == null) {
                params.getConsole().println("Unable to find index '" + options.getIndexName() + "' in store");
                return;
            }
        }
        for (final GeotoolsFeatureDataAdapter adapter : featureAdapters) {
            params.getConsole().println("Exporting type '" + adapter.getTypeName() + "'");
            final VectorQueryBuilder bldr = VectorQueryBuilder.newBuilder();
            if (options.getIndexName() != null) {
                bldr.indexName(options.getIndexName());
            }
            if (options.getCqlFilter() != null) {
                bldr.constraints(bldr.constraintsFactory().cqlConstraints(options.getCqlFilter()));
            }
            bldr.addTypeName(adapter.getTypeName());
            try (final CloseableIterator<SimpleFeature> it = dataStore.query(bldr.build())) {
                int iteration = 0;
                while (it.hasNext()) {
                    final AvroSimpleFeatureCollection simpleFeatureCollection = new AvroSimpleFeatureCollection();
                    final SimpleFeature next = it.next();
                    final SimpleFeatureType featureType = next.getFeatureType();
                    simpleFeatureCollection.setFeatureType(GeoWaveAvroFeatureUtils.buildFeatureDefinition(null, featureType, null, ""));
                    final List<AvroAttributeValues> avList = new ArrayList<>(options.getBatchSize());
                    avList.add(GeoWaveAvroFeatureUtils.buildAttributeValue(next, featureType));
                    while (it.hasNext() && (avList.size() < options.getBatchSize())) {
                        avList.add(GeoWaveAvroFeatureUtils.buildAttributeValue(it.next(), featureType));
                    }
                    params.getConsole().println("Exported " + (avList.size() + (iteration * options.getBatchSize())) + " features from '" + adapter.getTypeName() + "'");
                    iteration++;
                    simpleFeatureCollection.setSimpleFeatureCollection(avList);
                    dfw.append(simpleFeatureCollection);
                    dfw.flush();
                }
                params.getConsole().println("Finished exporting '" + adapter.getTypeName() + "'");
            }
        }
    }
}
Also used : VectorQueryBuilder(org.locationtech.geowave.core.geotime.store.query.api.VectorQueryBuilder) AvroAttributeValues(org.locationtech.geowave.adapter.vector.avro.AvroAttributeValues) ArrayList(java.util.ArrayList) Index(org.locationtech.geowave.core.store.api.Index) GeotoolsFeatureDataAdapter(org.locationtech.geowave.core.geotime.store.GeotoolsFeatureDataAdapter) DataStore(org.locationtech.geowave.core.store.api.DataStore) InternalDataAdapter(org.locationtech.geowave.core.store.adapter.InternalDataAdapter) ParameterException(com.beust.jcommander.ParameterException) InternalAdapterStore(org.locationtech.geowave.core.store.adapter.InternalAdapterStore) DataFileWriter(org.apache.avro.file.DataFileWriter) AvroSimpleFeatureCollection(org.locationtech.geowave.adapter.vector.avro.AvroSimpleFeatureCollection) SimpleFeature(org.opengis.feature.simple.SimpleFeature) PersistentAdapterStore(org.locationtech.geowave.core.store.adapter.PersistentAdapterStore) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) IndexStore(org.locationtech.geowave.core.store.index.IndexStore)

Example 3 with AvroAttributeValues

use of org.locationtech.geowave.adapter.vector.avro.AvroAttributeValues in project geowave by locationtech.

the class GeoWaveAvroIngestPlugin method toGeoWaveDataInternal.

@Override
protected CloseableIterator<GeoWaveData<SimpleFeature>> toGeoWaveDataInternal(final AvroSimpleFeatureCollection featureCollection, final String[] indexNames) {
    final AvroFeatureDefinition featureDefinition = featureCollection.getFeatureType();
    final List<GeoWaveData<SimpleFeature>> retVal = new ArrayList<>();
    SimpleFeatureType featureType;
    try {
        featureType = GeoWaveAvroFeatureUtils.avroFeatureDefinitionToGTSimpleFeatureType(featureDefinition);
        final FeatureDataAdapter adapter = new FeatureDataAdapter(featureType);
        final List<String> attributeTypes = featureDefinition.getAttributeTypes();
        for (final AvroAttributeValues attributeValues : featureCollection.getSimpleFeatureCollection()) {
            try {
                final SimpleFeature simpleFeature = GeoWaveAvroFeatureUtils.avroSimpleFeatureToGTSimpleFeature(featureType, attributeTypes, attributeValues);
                retVal.add(new GeoWaveData<>(adapter, indexNames, simpleFeature));
            } catch (final Exception e) {
                LOGGER.warn("Unable to read simple feature from Avro", e);
            }
        }
    } catch (final ClassNotFoundException e) {
        LOGGER.warn("Unable to read simple feature type from Avro", e);
    }
    return new Wrapper<>(retVal.iterator());
}
Also used : Wrapper(org.locationtech.geowave.core.store.CloseableIterator.Wrapper) AvroAttributeValues(org.locationtech.geowave.adapter.vector.avro.AvroAttributeValues) ArrayList(java.util.ArrayList) SimpleFeature(org.opengis.feature.simple.SimpleFeature) IOException(java.io.IOException) AvroFeatureDefinition(org.locationtech.geowave.adapter.vector.avro.AvroFeatureDefinition) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) GeoWaveData(org.locationtech.geowave.core.store.ingest.GeoWaveData) FeatureDataAdapter(org.locationtech.geowave.adapter.vector.FeatureDataAdapter)

Aggregations

ArrayList (java.util.ArrayList)3 AvroAttributeValues (org.locationtech.geowave.adapter.vector.avro.AvroAttributeValues)3 SimpleFeature (org.opengis.feature.simple.SimpleFeature)2 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)2 ParameterException (com.beust.jcommander.ParameterException)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 DataFileWriter (org.apache.avro.file.DataFileWriter)1 FeatureDataAdapter (org.locationtech.geowave.adapter.vector.FeatureDataAdapter)1 AvroFeatureDefinition (org.locationtech.geowave.adapter.vector.avro.AvroFeatureDefinition)1 AvroSimpleFeatureCollection (org.locationtech.geowave.adapter.vector.avro.AvroSimpleFeatureCollection)1 GeotoolsFeatureDataAdapter (org.locationtech.geowave.core.geotime.store.GeotoolsFeatureDataAdapter)1 VectorQueryBuilder (org.locationtech.geowave.core.geotime.store.query.api.VectorQueryBuilder)1 Wrapper (org.locationtech.geowave.core.store.CloseableIterator.Wrapper)1 InternalAdapterStore (org.locationtech.geowave.core.store.adapter.InternalAdapterStore)1 InternalDataAdapter (org.locationtech.geowave.core.store.adapter.InternalDataAdapter)1 PersistentAdapterStore (org.locationtech.geowave.core.store.adapter.PersistentAdapterStore)1 DataStore (org.locationtech.geowave.core.store.api.DataStore)1 Index (org.locationtech.geowave.core.store.api.Index)1 FieldWriter (org.locationtech.geowave.core.store.data.field.FieldWriter)1