17.9. 事务性写入¶
卡夫卡支持 transactional writes 。GeoMesa通过GeoTool公开此功能 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();
}
}