18.9. Transactional WritesΒΆ
Kafka supports the concept of transactional writes. GeoMesa exposes this functionality through the GeoTools transaction API:
import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.DefaultTransaction;
import org.geotools.data.FeatureWriter;
import org.geotools.data.Transaction;
DataStore store = DataStoreFinder.getDataStore(params);
// the transaction will contain the Kafka producer, so make sure to close it when finished
try (Transaction transaction = new DefaultTransaction()) {
// pass the transaction when getting a feature writer
try (FeatureWriter<SimpleFeatureType, SimpleFeature> writer =
store.getFeatureWriterAppend("my-type", transaction)) {
// write some features (elided), then commit the transaction:
transaction.commit();
// if you get an error (elided), then rollback the transaction:
transaction.rollback();
}
// re-using the transaction will re-use the Kafka producer
try (FeatureWriter<SimpleFeatureType, SimpleFeature> writer =
store.getFeatureWriterAppend("my-type", transaction)) {
// write some features (elided), then commit the transaction:
transaction.commit();
}
}