Я читаю файл геоджона, и, поскольку он может быть очень большим, я думаю, что лучшим вариантом является потоковая передача файла геоджона и его ручная запись в базу данных с помощью SQL.
Потому что это неэффективно с моделью JPA + Hibernate, потому что в ней много строк и даже если она ленива, как только я удаляю записи, она приносит все.
Так что я следую за этим:
Использовать шаблон jdbc: https : //spring.io/guides/gs/relational-data-access/
У меня проблемы:
Код класса 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 properties = DeserializeJSONFileToPropsAndGeom.getProperties();
List 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> 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 createScheme(final String creatorId, final String name, final String description, final InputStream inputStream, final String attachmentId, final String filePath, final Class 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
Я смог решить это!!
Для этого я построил услугу, которая верила бы в таблицу, и введи данные и ademГЎs у него есть функции, чтобы читать таблицу и удалять ее.
@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);
}
}
INSERT INTO table VALUES (...), (...), (...), ...
– Jose Hermosilla Rodrigo 29.11.2019, 14:20