文档首页 > > 开发指南> 开发GeoMesa应用> 样例代码说明> 插入数据

插入数据

分享
更新时间:2019/05/07 GMT+08:00

功能介绍

构造数据并将数据插入到指定表中。

样例代码

DefaultFeatureCollection featureCollection = new DefaultFeatureCollection();
String id;
Object[] NO_VALUES = {};
String[] PEOPLE_NAMES = {"Addams", "Bierce", "Clemens"};
Long SECONDS_PER_YEAR = 365L * 24L * 60L * 60L;
Random random = new Random(5771);
DateTime MIN_DATE = new DateTime(2014, 1, 1, 0, 0, 0, DateTimeZone.forID("UTC"));
Double MIN_X = -79.5;
Double MIN_Y = 37.0;
Double DX = 2.0;
Double DY = 2.0;
for (int i = 0; i < numNewFeatures; i++) {
  // create the new (unique) identifier and empty feature shell
  id = "Observation." + Integer.toString(i);
  SimpleFeature simpleFeature = SimpleFeatureBuilder.build(simpleFeatureType, NO_VALUES, id);
  // be sure to tell GeoTools explicitly that you want to use the ID you provided
  simpleFeature.getUserData().put(Hints.USE_PROVIDED_FID, java.lang.Boolean.TRUE);
  // populate the new feature's attributes
  // Who: string value
  simpleFeature.setAttribute("Who", PEOPLE_NAMES[i % PEOPLE_NAMES.length]);
  // What: long value
  simpleFeature.setAttribute("What", i);
  // Where: location: construct a random point within a 2-degree-per-side square
  double x = MIN_X + random.nextDouble() * DX;
  double y = MIN_Y + random.nextDouble() * DY;
  Geometry geometry = WKTUtils$.MODULE$.read("POINT(" + x + " " + y + ")");
  simpleFeature.setAttribute("Where", geometry);
  // When: date-time:construct a random instant within a year
  DateTime dateTime = MIN_DATE.plusSeconds((int) Math.round(random.nextDouble() * SECONDS_PER_YEAR));
  simpleFeature.setAttribute("When", dateTime.toDate());
  // Why: another string value
  // left empty, showing that not all attributes need values
  // accumulate this new feature in the collection
  featureCollection.add(simpleFeature);
  FeatureStore featureStore = (FeatureStore) dataStore.getFeatureSource(simpleFeatureTypeName);
  featureStore.addFeatures(featureCollection);
}
分享:

    相关文档

    相关产品

文档是否有解决您的问题?

提交成功!非常感谢您的反馈,我们会继续努力做到更好!
反馈提交失败,请稍后再试!

*必选

请至少选择或填写一项反馈信息

字符长度不能超过200

提交反馈 取消

如您有其它疑问,您也可以通过华为云社区问答频道来与我们联系探讨

智能客服提问云社区提问