Search in sources :

Example 1 with FeatureAttributeProtos

use of org.locationtech.geowave.service.grpc.protobuf.FeatureAttributeProtos in project geowave by locationtech.

the class GeoWaveGrpcVectorService method vectorIngest.

@Override
public StreamObserver<VectorIngestParametersProtos> vectorIngest(final StreamObserver<StringResponseProtos> responseObserver) {
    return new StreamObserver<VectorIngestParametersProtos>() {

        private boolean firstFeature = true;

        private String storeName = null;

        private DataStore dataStore = null;

        private String typeName = null;

        private String indexName = null;

        private Writer<SimpleFeature> writer = null;

        private DataTypeAdapter adapter = null;

        private Index pIndex = null;

        private SimpleFeatureTypeBuilder typeBuilder = null;

        private SimpleFeatureBuilder featureBuilder = null;

        private static final int batchSize = 100;

        private int batchCount = 0;

        private int totalCount = 0;

        @Override
        public void onNext(final VectorIngestParametersProtos f) {
            if (firstFeature) {
                firstFeature = false;
                // parse top level required parameters
                storeName = f.getBaseParams().getStoreName();
                final StoreLoader storeLoader = new StoreLoader(storeName);
                typeName = f.getBaseParams().getTypeName();
                indexName = f.getBaseParams().getIndexName();
                // This only needs to happen once
                if (typeBuilder == null) {
                    typeBuilder = new SimpleFeatureTypeBuilder();
                    for (final Map.Entry<String, FeatureAttributeProtos> mapEntry : f.getFeatureMap().entrySet()) {
                        switch(mapEntry.getValue().getValueCase()) {
                            case VALSTRING:
                                {
                                    typeBuilder.add(mapEntry.getKey(), String.class);
                                    break;
                                }
                            case VALINT32:
                                {
                                    typeBuilder.add(mapEntry.getKey(), Integer.class);
                                    break;
                                }
                            case VALINT64:
                                {
                                    typeBuilder.add(mapEntry.getKey(), Long.class);
                                    break;
                                }
                            case VALFLOAT:
                                {
                                    typeBuilder.add(mapEntry.getKey(), Float.class);
                                    break;
                                }
                            case VALDOUBLE:
                                {
                                    typeBuilder.add(mapEntry.getKey(), Double.class);
                                    break;
                                }
                            case VALDATE:
                                {
                                    typeBuilder.add(mapEntry.getKey(), Date.class);
                                    break;
                                }
                            case VALGEOMETRY:
                                {
                                    typeBuilder.add(mapEntry.getKey(), Geometry.class);
                                    break;
                                }
                            default:
                                break;
                        }
                        ;
                    }
                }
                // This a factory class that builds simple feature objects
                // based
                // on the
                // type
                typeBuilder.setName(typeName);
                final SimpleFeatureType featureType = typeBuilder.buildFeatureType();
                featureBuilder = new SimpleFeatureBuilder(featureType);
                // get a handle to the relevant stores
                if (!storeLoader.loadFromConfig(GeoWaveGrpcServiceOptions.geowaveConfigFile)) {
                    throw new ParameterException("Cannot find store name: " + storeLoader.getStoreName());
                }
                dataStore = storeLoader.createDataStore();
                final PersistentAdapterStore adapterStore = storeLoader.createAdapterStore();
                final InternalAdapterStore internalAdapterStore = storeLoader.createInternalAdapterStore();
                final Short internalAdapterId = internalAdapterStore.getAdapterId(typeName);
                if (internalAdapterId != null) {
                    adapter = adapterStore.getAdapter(internalAdapterId);
                } else {
                    adapter = null;
                }
                if (adapter == null) {
                    adapter = new FeatureDataAdapter(featureType);
                }
                // Load the Indexes
                final List<Index> indices = DataStoreUtils.loadIndices(storeLoader.createIndexStore(), indexName);
                // assuming one index for now
                // (PrimaryIndex)
                pIndex = indices.get(0);
                // indexStore.getIndex(indexId);
                if (pIndex == null) {
                    throw new ParameterException("Failed to instantiate primary index");
                }
                // create index writer to actually write data
                dataStore.addType(adapter, pIndex);
                writer = dataStore.createWriter(adapter.getTypeName());
            }
            // Set the values for all the attributes in the feature
            for (final Map.Entry<String, FeatureAttributeProtos> attribute : f.getFeatureMap().entrySet()) {
                switch(attribute.getValue().getValueCase()) {
                    case VALSTRING:
                        {
                            featureBuilder.set(attribute.getKey(), attribute.getValue().getValString());
                            break;
                        }
                    case VALINT32:
                        {
                            featureBuilder.set(attribute.getKey(), attribute.getValue().getValInt32());
                            break;
                        }
                    case VALINT64:
                        {
                            featureBuilder.set(attribute.getKey(), attribute.getValue().getValInt64());
                            break;
                        }
                    case VALFLOAT:
                        {
                            featureBuilder.set(attribute.getKey(), attribute.getValue().getValFloat());
                            break;
                        }
                    case VALDOUBLE:
                        {
                            featureBuilder.set(attribute.getKey(), attribute.getValue().getValDouble());
                            break;
                        }
                    case VALDATE:
                        {
                            featureBuilder.set(attribute.getKey(), new Date(Timestamps.toMillis(attribute.getValue().getValDate())));
                            break;
                        }
                    case VALGEOMETRY:
                        {
                            Geometry geom = null;
                            try {
                                geom = new WKBReader(JTSFactoryFinder.getGeometryFactory()).read(attribute.getValue().getValGeometry().toByteArray());
                            } catch (FactoryRegistryException | org.locationtech.jts.io.ParseException e) {
                                LOGGER.error("Failed to parse string for geometry", e);
                            }
                            if (geom != null) {
                                featureBuilder.set(attribute.getKey(), geom);
                            }
                            break;
                        }
                    default:
                        break;
                }
                ;
            }
            final SimpleFeature sf = featureBuilder.buildFeature(String.valueOf(totalCount));
            writer.write(sf);
            // onComplete and onError
            if (++batchCount >= batchSize) {
                // writer.flush();
                batchCount = 0;
            }
            final StringResponseProtos resp = StringResponseProtos.newBuilder().setResponseValue(String.valueOf(++totalCount)).build();
            responseObserver.onNext(resp);
        }

        @Override
        public void onError(final Throwable t) {
            LOGGER.error("Exception encountered during vectorIngest", t);
            writer.flush();
            writer.close();
            final StringResponseProtos resp = StringResponseProtos.newBuilder().setResponseValue("Error during ingest: ").build();
            responseObserver.onNext(resp);
            responseObserver.onCompleted();
        }

        @Override
        public void onCompleted() {
            writer.flush();
            writer.close();
            final StringResponseProtos resp = StringResponseProtos.newBuilder().setResponseValue("Ingest completed successfully").build();
            responseObserver.onNext(resp);
            responseObserver.onCompleted();
        }
    };
}
Also used : SimpleFeatureTypeBuilder(org.geotools.feature.simple.SimpleFeatureTypeBuilder) Index(org.locationtech.geowave.core.store.api.Index) ByteString(com.google.protobuf.ByteString) VectorIngestParametersProtos(org.locationtech.geowave.service.grpc.protobuf.VectorIngestParametersProtos) GeoWaveGTDataStore(org.locationtech.geowave.adapter.vector.plugin.GeoWaveGTDataStore) DataStore(org.locationtech.geowave.core.store.api.DataStore) StoreLoader(org.locationtech.geowave.core.store.cli.store.StoreLoader) FeatureAttributeProtos(org.locationtech.geowave.service.grpc.protobuf.FeatureAttributeProtos) ParameterException(com.beust.jcommander.ParameterException) StreamObserver(io.grpc.stub.StreamObserver) InternalAdapterStore(org.locationtech.geowave.core.store.adapter.InternalAdapterStore) DataTypeAdapter(org.locationtech.geowave.core.store.api.DataTypeAdapter) StringResponseProtos(org.locationtech.geowave.service.grpc.protobuf.GeoWaveReturnTypesProtos.StringResponseProtos) Date(java.util.Date) WKBReader(org.locationtech.jts.io.WKBReader) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Geometry(org.locationtech.jts.geom.Geometry) PersistentAdapterStore(org.locationtech.geowave.core.store.adapter.PersistentAdapterStore) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) Map(java.util.Map) FeatureDataAdapter(org.locationtech.geowave.adapter.vector.FeatureDataAdapter) WKBWriter(org.locationtech.jts.io.WKBWriter) Writer(org.locationtech.geowave.core.store.api.Writer) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Aggregations

ParameterException (com.beust.jcommander.ParameterException)1 ByteString (com.google.protobuf.ByteString)1 StreamObserver (io.grpc.stub.StreamObserver)1 Date (java.util.Date)1 Map (java.util.Map)1 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)1 SimpleFeatureTypeBuilder (org.geotools.feature.simple.SimpleFeatureTypeBuilder)1 FeatureDataAdapter (org.locationtech.geowave.adapter.vector.FeatureDataAdapter)1 GeoWaveGTDataStore (org.locationtech.geowave.adapter.vector.plugin.GeoWaveGTDataStore)1 InternalAdapterStore (org.locationtech.geowave.core.store.adapter.InternalAdapterStore)1 PersistentAdapterStore (org.locationtech.geowave.core.store.adapter.PersistentAdapterStore)1 DataStore (org.locationtech.geowave.core.store.api.DataStore)1 DataTypeAdapter (org.locationtech.geowave.core.store.api.DataTypeAdapter)1 Index (org.locationtech.geowave.core.store.api.Index)1 Writer (org.locationtech.geowave.core.store.api.Writer)1 StoreLoader (org.locationtech.geowave.core.store.cli.store.StoreLoader)1 FeatureAttributeProtos (org.locationtech.geowave.service.grpc.protobuf.FeatureAttributeProtos)1 StringResponseProtos (org.locationtech.geowave.service.grpc.protobuf.GeoWaveReturnTypesProtos.StringResponseProtos)1 VectorIngestParametersProtos (org.locationtech.geowave.service.grpc.protobuf.VectorIngestParametersProtos)1 Geometry (org.locationtech.jts.geom.Geometry)1