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;
}
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() + "'");
}
}
}
}
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());
}
Aggregations