18.9. Transactional WritesΒΆ
Kafka supports the concept of transactional writes. GeoMesa exposes this functionality through the GeoTools transaction API:
import org.geotools.api.data.DataStore;
import org.geotools.api.data.DataStoreFinder;
import org.geotools.api.data.FeatureWriter;
import org.geotools.api.data.Transaction;
import org.geotools.data.DefaultTransaction;
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();
}
}