我正在读取geojson文件,它们可能很大,我认为最好的选择是流处理geojson文件,然后使用SQL手动将其记录到数据库中。
因为它在JPA + Hibernate模型中效率不高,因为它有很多行,即使我删除记录后就很懒,它也会带来一切。
所以我正在关注:
我正在使用jdbc模板:https://sites.google.com/site/gson/streaming
我的问题是:
这是我的RelationalDataAccessApplication类的代码:
@SpringBootApplication
public class RelationalDataAccessApplication implements CommandLineRunner {
private static final Logger log = LoggerFactory.getLogger(RelationalDataAccessApplication.class);
private String checkedNameTable;
public static void main(String[] args) {
SpringApplication.run(RelationalDataAccessApplication.class, args);
}
@Autowired
JdbcTemplate jdbcTemplate;
@Override
public void run(String... strings) throws Exception {
log.info("Deserialize GEOJSON");
DeserializeJSONFileToPropsAndGeom.initialize(inputStream);
List<String> properties = DeserializeJSONFileToPropsAndGeom.getProperties();
List<String> geometries = DeserializeJSONFileToPropsAndGeom.getGeometries();
log.info("CHECK IF TABLE EXISTS");
checkedNameTable= nameTable("nombre pasado por el usuario");
log.info("CREATING TABLE");
jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS " + checkedNameTable + " ( table_id SERIAL, properties jsonb not null, geom geometry(GeometryZ,4326), primary key (table_id));");
log.info("INSERT DATA");
for (int i=0; i<properties.size(); i++) {
Object property = properties.get(i).toString().replace("'", "´");
Object geometry = geometries.get(i);
String SQL = "INSERT INTO " + checkedNameTable + " ( properties, geom ) VALUES ( '" + property + "', ST_Force3D(ST_SetSRID(ST_GeomFromGeoJSON('" + geometry + "'), 4326) ));";
jdbcTemplate.batchUpdate(SQL);
}
}
public String getCheckedNameTable() {
return checkedNameTable;
}
private void dropTable(String nameTable) throws Exception {
log.info("DROP TABLE " + nameTable);
if (nameTable != null) {
String SQL = "DROP TABLE IF EXISTS " + nameTable + ";";
jdbcTemplate.execute(SQL);
}
}
private List<Map<String, Object>> readTable(String nameTable) throws Exception {
log.info("READ TABLE " + nameTable);
if (nameTable != null) {
String SQL = "SELECT table_id, CAST(properties AS text), GeometryType(geom) FROM " + nameTable + " ORDER BY table_id ASC;";
return jdbcTemplate.queryForList(SQL);
}
return null;
}
private String nameTable(String name) {
UUID uuid = UUID.randomUUID();
return (checkifExistTable(name)) ?
name.toLowerCase() :
name.toLowerCase() + "_" + uuid.toString().replace("-", "_");
}
private boolean checkifExistTable(String name) {
String SQL = "select table_name from information_schema.tables where table_schema = 'public'";
boolean exists = false;
int count = jdbcTemplate.queryForObject(SQL, new Object[] { "paramValue" }, Integer.class);
exists = count > 0;
return exists;
}
}
这是我的SchemeService类的代码:
@Service
@Transactional
public class SchemeService extends DaoCrudService {
@Autowired
private SchemeDao schemeDao;
public <T> T createScheme(final String creatorId, final String name, final String description, final InputStream inputStream, final String attachmentId, final String filePath, final Class<T> targetVOClass) {
Scheme scheme = createScheme(creatorId, name, description, inputStream, attachmentId, filePath);
return getDozerService().map(scheme, targetVOClass);
}
protected Scheme createScheme(final String creatorId, final String name, final String description, final InputStream inputStream, final String attachmentId, final String filePath) {
RelationalDataAccessApplication.main(name, inputStream);
String tableId = RelationalDataAccessApplication.getCheckedNameTable();
Scheme scheme = new Scheme();
scheme.setCreator(creatorId);
scheme.setName(name);
scheme.setDescription(description);
scheme.setTableId(tableId);
scheme.setAttachmentId(attachmentId);
scheme.setFilePath(filePath);
createScheme(scheme);
return scheme;
}
public List<Map<String, Object>> readTable(final String name) {
return RelationalDataAccessApplication.readTable(name);
}
public void deleteScheme(final SchemeCriteria schemeCriteria) {
SchemeVO scheme = read(schemeDao, schemeCriteria, SchemeVO.class);
String nameTable = scheme.getTableId();
// Call drop Table
RelationalDataAccessApplication.dropTable(nameTable);
delete(schemeDao, schemeCriteria);
}
}
答案 0 :(得分:0)
我解决了!
为此,我建立了一个服务,该服务创建表并插入数据,还具有读取和删除表的功能。
@Service
@Transactional
public class TableGeoJsonGenerator {
private String checkedNameTable;
@Autowired
JdbcTemplate jdbcTemplate;
public String createTable(final String name, final InputStream inputStream) {
DeserializeJSONFileToPropsAndGeom.initialize(inputStream);
List<String> properties = DeserializeJSONFileToPropsAndGeom.getProperties();
List<String> geometries = DeserializeJSONFileToPropsAndGeom.getGeometries();
checkedNameTable= nameTable(name);
jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS " + checkedNameTable + " ( table_id SERIAL, properties jsonb not null, geom geometry(GeometryZ,4326), primary key (table_id));");
for (int i=0; i<properties.size(); i++) {
Object property = properties.get(i).toString().replace("'", "´");
Object geometry = geometries.get(i);
String SQL = "INSERT INTO " + checkedNameTable + " ( properties, geom ) VALUES ( '" + property + "', ST_Force3D(ST_SetSRID(ST_GeomFromGeoJSON('" + geometry + "'), 4326) ));";
jdbcTemplate.batchUpdate(SQL);
}
return checkedNameTable;
}
private String nameTable(String name) {
UUID uuid = UUID.randomUUID();
return (checkifExistTable(name)) ?
name.toLowerCase() :
name.toLowerCase() + "_" + uuid.toString().replace("-", "_");
}
private boolean checkifExistTable(String name) {
String SQL = "select count(*) from information_schema.tables where table_schema = 'public' AND table_name LIKE '" + name + "'";
boolean exists = false;
int count = jdbcTemplate.queryForObject(SQL, Integer.class);
exists = count > 0;
return exists;
}
public void dropTable(String nameTable) {
if (nameTable != null) {
String SQL = "DROP TABLE IF EXISTS " + nameTable + ";";
jdbcTemplate.execute(SQL);
}
}
public List<Map<String, Object>> readTable(String nameTable) {
if (nameTable != null) {
String SQL = "SELECT table_id, CAST(properties AS text), GeometryType(geom) FROM " + nameTable + " ORDER BY table_id ASC;";
return jdbcTemplate.queryForList(SQL);
}
return null;
}
}
SchemeService类
@Service
@Transactional
public class SchemeService extends DaoCrudService {
@Autowired
private SchemeDao schemeDao;
@Autowired
TableGeoJsonGenerator table;
public <T> T createScheme(final String creatorId, final String name, final String description, final InputStream inputStream, final String attachmentId, final String filePath, final Class<T> targetVOClass) {
Scheme scheme = createScheme(creatorId, name, description, inputStream, attachmentId, filePath);
return getDozerService().map(scheme, targetVOClass);
}
protected Scheme createScheme(final String creatorId, final String name, final String description, final InputStream inputStream, final String attachmentId, final String filePath) {
String tableId = table.createTable(name, inputStream);
Scheme scheme = new Scheme();
scheme.setCreator(creatorId);
scheme.setName(name);
scheme.setDescription(description);
scheme.setTableId(tableId);
scheme.setAttachmentId(attachmentId);
scheme.setFilePath(filePath);
createScheme(scheme);
return scheme;
}
protected void createScheme(final Scheme scheme) {
schemeDao.create(scheme);
}
public List<Map<String, Object>> readTable(final String name) {
return table.readTable(name);
}
public void deleteScheme(final SchemeCriteria schemeCriteria) {
SchemeVO scheme = read(schemeDao, schemeCriteria, SchemeVO.class);
String nameTable = scheme.getTableId();
// Call drop Table
table.dropTable(nameTable);
delete(schemeDao, schemeCriteria);
}
}