在索引表创建完成、时空对象创建完成后,即可调用数据写入接口将数据写入到Lindorm Ganos中。
单条记录写入
Lindorm Ganos通过GeoTools API中的SimpleFeatureWriter写入单条数据,SimpleFeatureWriter支持事务,可以通过DataStore的getFeatureWriterAppend方法获取。
说明
构造SimpleFeatureType以及SimpleFeature请参见快速入门。
/**
* 写入单条数据
* @param sft SimpleFeatureType
* @param feature 待写入的SimpleFeature
* @return 返回1,否则抛出错误
* @throws Exception
*/
public int upsert(SimpleFeatureType sft, SimpleFeature feature) throws Exception {
try{
SimpleFeatureWriter writer=(SimpleFeatureWriter)ds.getFeatureWriterAppend(sft.getTypeName(), Transaction.AUTO_COMMIT);
SimpleFeature toWrite=writer.next();
toWrite.setAttributes(feature.getAttributes());
toWrite.getUserData().putAll(feature.getUserData());
writer.write();
writer.close();
return 1;
} catch (Exception e){
throw new Exception("写入数据错误" + e.getMessage());
}
}
批量写入
Lindorm Ganos支持批量插入SimpleFeature,通过GeoTools API中的SimpleFeatureStore类实现。
说明
构造SimpleFeatureType以及SimpleFeature可参见快速入门。
/**
* 批量写入数据
*
* @param sft SimpleFeatureType
* @param features 待写入的SimpleFeature对象集合
* @return 返回实际写入成功的条数
* @throws Exception
*/
public int upsert(SimpleFeatureType sft, List<SimpleFeature> features) throws Exception {
try{
SimpleFeatureStore featureStore = (SimpleFeatureStore) ds.getFeatureSource(sft.getTypeName());
List<FeatureId> featureIds = featureStore.addFeatures(new ListFeatureCollection(sft,features));
return featureIds.size();
} catch (Exception e){
throw new Exception("写入数据错误" + e.getMessage());
}
}
文档内容是否对您有帮助?