Search in sources :

Example 1 with ColumnInfoBean

use of org.apache.inlong.manager.common.pojo.query.ColumnInfoBean in project incubator-inlong by apache.

the class HiveServerDao method queryStructure.

/**
 * Query Hive column structure
 */
public List<ColumnInfoBean> queryStructure(String querySql, String jdbcUrl, String user, String password) throws Exception {
    try (Connection conn = this.getHiveConnection(jdbcUrl, user, password)) {
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(querySql);
        List<ColumnInfoBean> columnInfoBeans = new ArrayList<>();
        try {
            while (rs.next()) {
                ColumnInfoBean columnInfoBean = new ColumnInfoBean();
                columnInfoBean.setColumnName(rs.getString(1));
                columnInfoBean.setColumnType(rs.getString(2));
                columnInfoBean.setColumnDesc(rs.getString(3));
                columnInfoBeans.add(columnInfoBean);
            }
        } catch (Exception e) {
            LOG.error("query table structure error", e);
        }
        return columnInfoBeans;
    }
}
Also used : Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) ColumnInfoBean(org.apache.inlong.manager.common.pojo.query.ColumnInfoBean)

Example 2 with ColumnInfoBean

use of org.apache.inlong.manager.common.pojo.query.ColumnInfoBean in project incubator-inlong by apache.

the class DefaultHiveTableOperator method createTable.

private void createTable(String groupId, SinkForSortDTO config) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("begin create hive table for inlong group={}, config={}", groupId, config);
    }
    // Get all info from config
    HiveSinkDTO hiveInfo = HiveSinkDTO.getFromJson(config.getExtParams());
    HiveTableQueryBean tableBean = getTableQueryBean(config, hiveInfo);
    try {
        // create database if not exists
        dataSourceService.createDb(tableBean);
        // check if the table exists
        List<ColumnInfoBean> columns = dataSourceService.queryColumns(tableBean);
        if (columns.size() == 0) {
            // no such table, create one
            dataSourceService.createTable(tableBean);
        } else {
            // set columns, skip the first columns already exist in hive
            List<HiveColumnQueryBean> columnsSkipHistory = tableBean.getColumns().stream().skip(columns.size()).collect(toList());
            if (columnsSkipHistory.size() != 0) {
                tableBean.setColumns(columnsSkipHistory);
                dataSourceService.createColumn(tableBean);
            }
        }
        sinkService.updateStatus(config.getId(), EntityStatus.SINK_CONFIG_SUCCESSFUL.getCode(), "create hive table success");
    } catch (Throwable e) {
        LOGGER.error("create hive table error, ", e);
        sinkService.updateStatus(config.getId(), EntityStatus.SINK_CONFIG_FAILED.getCode(), e.getMessage());
        throw new WorkflowException("create hive table failed, reason: " + e.getMessage());
    }
    LOGGER.info("success create hive table for data group [" + groupId + "]");
}
Also used : HiveTableQueryBean(org.apache.inlong.manager.common.pojo.query.hive.HiveTableQueryBean) WorkflowException(org.apache.inlong.manager.common.exceptions.WorkflowException) HiveSinkDTO(org.apache.inlong.manager.common.pojo.sink.hive.HiveSinkDTO) ColumnInfoBean(org.apache.inlong.manager.common.pojo.query.ColumnInfoBean) HiveColumnQueryBean(org.apache.inlong.manager.common.pojo.query.hive.HiveColumnQueryBean)

Aggregations

ColumnInfoBean (org.apache.inlong.manager.common.pojo.query.ColumnInfoBean)2 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 Statement (java.sql.Statement)1 ArrayList (java.util.ArrayList)1 WorkflowException (org.apache.inlong.manager.common.exceptions.WorkflowException)1 HiveColumnQueryBean (org.apache.inlong.manager.common.pojo.query.hive.HiveColumnQueryBean)1 HiveTableQueryBean (org.apache.inlong.manager.common.pojo.query.hive.HiveTableQueryBean)1 HiveSinkDTO (org.apache.inlong.manager.common.pojo.sink.hive.HiveSinkDTO)1