DataStore通过geopkg写入SQLite数据库
2021/11/3 19:12:24
本文主要是介绍DataStore通过geopkg写入SQLite数据库,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
geotools工具类中 DataStore(或者说SimpleFeature)如何写入SQLite数据库。
引入依赖
只需要geopkg这个依赖,里面包括了sqlite-jdbc。
<dependency> <groupId>org.geotools</groupId> <artifactId>gt-geopkg</artifactId> <version>${gt.version}</version> </dependency>
写入
先获取DataStore,因为SQLite是基于文件实现,一个文件一个数据库,单写多读。
private DataStore getSqliteDataStore(String reqId) throws IOException { if (!Files.exists(Paths.get(cachePath))) { Files.createDirectory(Paths.get(cachePath)); } Map<String, String> params = new HashMap<>(); params.put("dbtype", "geopkg"); params.put("database", getCacheDb(reqId)); return DataStoreFinder.getDataStore(params); } private String getCacheDb(String reqId) { return cachePath + File.separator + reqId + ".db"; }
写入实现, 我们传入参数是一个SimpleFeature的迭代器,采用分批写入方式。
public void write(String reqId, SimpleFeatureIterator iterator) { if (!iterator.hasNext()) { return; } try { DataStore datastore = getSqliteDataStore(reqId); SimpleFeature f = iterator.next(); SimpleFeatureType featureType = f.getFeatureType(); final int batchSize = 512; List<SimpleFeature> batch = new ArrayList<>(batchSize); batch.add(f); // 建表 datastore.createSchema(featureType); SimpleFeatureSource source = datastore.getFeatureSource(featureType.getTypeName()); if (source instanceof SimpleFeatureStore) { SimpleFeatureStore store = (SimpleFeatureStore) source; while (iterator.hasNext()) { f = iterator.next(); batch.add(f); if (batch.size() == batchSize) { writeFeatures(featureType, batch, store); } } if (!batch.isEmpty()) { writeFeatures(featureType, batch, store); } } } catch (IOException e) { log.error("Write Error", e); } }
分批写入:
private void writeFeatures(SimpleFeatureType featureType, List<SimpleFeature> batch, SimpleFeatureStore store) throws IOException { Transaction transaction = new DefaultTransaction("create"); try { store.addFeatures(new ListFeatureCollection(featureType, batch)); transaction.commit(); batch.clear(); } catch (Exception e) { log.warn("Write SimpleFeature Error", e); transaction.rollback(); } finally { transaction.close(); } }
读取
读取采用直接用JDBC读SQLite数据库,返回分页数据。ResultSetFeatureIterator
在这里自己实现,就是ResultSet
转SimpleFeatureIterator
,并不重要。
public PageResultData read(String reqId, int current, int pageSize) { try (Connection connection = DriverManager.getConnection("jdbc:sqlite:" + getCacheDb(reqId))) { Statement statement = connection.createStatement(); statement.setQueryTimeout(30); // set timeout to 30 sec. ResultSet rs = statement.executeQuery(String.format("select count(1) from %s", reqId)); rs.next(); int total = rs.getInt(1); rs = statement.executeQuery(String.format("select * from %s limit %s offset %s", reqId, pageSize, current - 1)); ResultSetFeatureIterator it = new ResultSetFeatureIterator(reqId, rs); List<SimpleFeature> features = new ArrayList<>(pageSize); while (it.hasNext()) { features.add(it.next()); } return new PageResultData(features, current, pageSize, total); } catch (SQLException e) { log.error("Get Page Sqlite Error", e); } return new PageResultData(); }
测试
@Test public void testSqliteAccessFeature() { String reqId = "req" + System.currentTimeMillis(); // 写,构造数据 SimpleFeatureTypeBuilder bt = new SimpleFeatureTypeBuilder(); bt.add("name", String.class); bt.setName(reqId); SimpleFeatureType sft = bt.buildFeatureType(); ListFeatureCollection simpleFeatures = new ListFeatureCollection(sft, new SimpleFeature[]{ SimpleFeatureBuilder.build(sft, new Object[]{"jimo"}, null), SimpleFeatureBuilder.build(sft, new Object[]{"hehe"}, null), SimpleFeatureBuilder.build(sft, new Object[]{"lily"}, null) }); // 调用 write(reqId, new DelegateSimpleFeatureIterator(simpleFeatures.iterator())); // 分页读 PageResultData data = read(reqId, 1, 10); assertEquals(3, data.getTotal()); assertEquals(3, data.getFeatures().size()); }
这篇关于DataStore通过geopkg写入SQLite数据库的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2023-12-29"SQLite注入获取表名技巧"
- 2023-12-27SQLite数据库注入方式及安全性
- 2022-09-16Sqlite 并发读写的演进之路
- 2022-09-07修改ASQLite3D2010 支持中文显示
- 2022-09-0502-mORMot框架样例学习-02 - Embedded SQLite3 ORM(SQLite3 数据库)
- 2022-08-22基础复习——数据库SQLite——SQL的基本语法——数据库管理器SQLiteDatabase——数据库帮助器SQLiteOpenHelper
- 2022-08-21【数据库】SQLite数据库 工具
- 2022-08-06SQLite 错误:database disk image is malformed - 数据库磁盘映像格式不正确
- 2022-07-30学生信息管理系统(QT+SQLITE实现)
- 2022-07-22SQLite 建表、增删改查 简单操作