2

Estoy leyendo un archivo geojson y como pueden ser muy grandes creo que la mejor opción es hacer streaming del archivo geojson e ir metiendo registro a registro en la base de datos con SQL de forma manual.

Porque no es eficiente con un modelo JPA + Hibernate, porque tiene muchas rows y aunque sea lazy en cuanto quite los registros lo trae todo.

Por lo que estoy siguiendo esto:

Uso : https://spring.io/guides/gs/relational-data-access/

Los problemas que tengo son:

  • no sé como pasarle los parámetros name e inputStream a la clase RelationalDataAccessApplication desde SchemeService
  • no sé como obtener el nombre de la tabla porque aunque sea público solo me deja acceder al método run.
  • no sé como llamar a las funciones dropTable y readTable desde SchemeService.

Código de la clase 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;
    }

}

Código de la clase 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);  
    }

}

1 respuesta 1

2

He conseguido solucionarlo!!

Para ello he construido un servicio el cual crea la tabla e inserta los datos y además tiene las funciones para leer la tabla y eliminarla.

@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;
    }
}

clase 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);  
    }

}
0

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.