I'm new to Geotools and facing this issue : I'm injecting in PostGis about 2MB of shapefile info (about 5800 entries) and surprisingly it takes more or less 6 minutes to complete! Quite annoying because my "real" data set might be up to 25MB by shapefile group (shp, dbf...), 100 groups needed.
I was told that it might be an index issue, because Postgre updates tables' indexes on each INSERT. Is there a way to "disable" these indexes during my mass INSERTs and tell the database to create all indexes on the end? Or is there a better way to do that?
Here is my code snippet :
Map<String, Object> shpparams = new HashMap<String, Object>();
shpparams.put("url", "file://" + path);
FileDataStore shpStore = (FileDataStore) shpFactory.createDataStore(shpparams);
SimpleFeatureCollection features = shpStore.getFeatureSource().getFeatures();
if (schema == null) {
// Copy schema and change name in order to refer to the same
// global schema for all files
SimpleFeatureType originalSchema = shpStore.getSchema();
Name originalName = originalSchema.getName();
NameImpl theName = new NameImpl(originalName.getNamespaceURI(), originalName.getSeparator(), POSTGIS_TABLENAME);
schema = factory.createSimpleFeatureType(theName, originalSchema.getAttributeDescriptors(), originalSchema.getGeometryDescriptor(),
originalSchema.isAbstract(), originalSchema.getRestrictions(), originalSchema.getSuper(), originalSchema.getDescription());
pgStore.createSchema(schema);
}
// String typeName = shpStore.getTypeNames()[0];
SimpleFeatureStore featureStore = (SimpleFeatureStore) pgStore.getFeatureSource(POSTGIS_TABLENAME);
// Ajout des objets du shapefile dans la table PostGIS
DefaultTransaction transaction = new DefaultTransaction("create");
featureStore.setTransaction(transaction);
try {
featureStore.addFeatures(features);
transaction.commit();
} catch (Exception problem) {
LOGGER.error(problem.getMessage(), problem);
transaction.rollback();
} finally {
transaction.close();
}
shpStore.dispose();
Thank you for your help!
So I tested your solutions but nothing helped me more... The completion time is still the same. Here is my table definition :
So I do not think that the problem is directly linked to my code or the database, maybe it is due to system limitations (RAM, buffers...). I will have a look at this in the next few days.
Do you have more ideas?
I'm back with the solution for this problem. After many investigations, I found that the physical network was the issue : with a local DB (local to geotools app) there were no problem. The network added 200 or 300 millisec to each INSERT statement request. With the large amount of data injected in DB came the very long response time!
So no problem with the orignal Postgis config or my code snippet...
Thank you all for your participation.