我是 Geotools 的新手并面临这个问题:我在 PostGis 中注入了大约 2MB 的 shapefile 信息(大约 5800 个条目),令人惊讶的是,它大约需要 6 分钟才能完成!很烦人,因为我的“真实”数据集按 shapefile 组(shp、dbf ...)可能高达 25MB,需要 100 个组。
有人告诉我这可能是一个索引问题,因为 Postgre 会在每个 INSERT 上更新表的索引。有没有办法在我的大量插入期间“禁用”这些索引并告诉数据库最后创建所有索引?还是有更好的方法来做到这一点?
这是我的代码片段:
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();
感谢您的帮助!
所以我测试了你的解决方案,但没有什么能帮助我更多......完成时间仍然相同。这是我的表定义:
- FID 序列号 10
- the_geom 几何 2147483647
- xxx varchar 10
- xxx int4 10
- xxx varchar 3
- xxx varchar 2
- xxx浮动8 17
- xxx浮动8 17
- xxx浮动8 17
所以我不认为问题与我的代码或数据库直接相关,可能是由于系统限制(RAM、缓冲区......)。我会在接下来的几天里看看这个。
你有更多的想法吗?