Hive语句不使用java类将数据插入表中

时间:2017-11-14 13:55:32

标签: java hadoop hive spring-jdbc

我正在尝试使用hive语句实例将数据从java类中的其他表插入到hive表中,源表和目标表都存在于同一个配置单元数据库中。

所有源表都包含数据。

以下课程执行以上查询:

@Component
public class HiveExecutor implements IExecutor{

    static final Logger logger = Logger.getLogger(HiveExecutor.class);

    @Autowired 
    ConnectionFactory connectionFactory;

    @Override
    public ResultSetHolder executeSql(String sql) throws IOException {
        logger.info(" Inside hive executor  for SQL : " + sql);
        ResultSetHolder rsHolder = new ResultSetHolder();
        IConnector connector = connectionFactory.getConnector("hive");
        ConnectionHolder conHolder = connector.getConnection();
        Object obj = conHolder.getConObject();
        if(obj instanceof Statement) {
            Statement stmt = (Statement) obj;
            ResultSet rs=null;
            try {
                if(sql.toUpperCase().contains("INSERT")) {
                    int result = stmt.executeUpdate(sql);
                    if(result != 0)
                        logger.info("Successfull insertion operation. Number of rows changed: "+result);
                    else
                        logger.info("Unsuccessfull insertion operation.");
                }
                else
                    rs = stmt.executeQuery(sql);
                rsHolder.setResultSet(rs);
                rsHolder.setType(ResultType.resultset);
            } catch (SQLException e) {              
                e.printStackTrace();
            }
        }       
        return rsHolder;
    }

    @Override
    public Boolean registerTempTable(DataFrame df, String tableName) {
        // TODO Auto-generated method stub
        return null;
    }
}

执行SELECT查询时,它确实会带来数据,但它会失败/返回0表示插入查询。

当从hive CLI执行相同的INSERT查询时,它按预期工作,将数据插入目标表。

1 个答案:

答案 0 :(得分:-1)

  

尝试在连接字符串中指定队列名称:

CONNECTION_STRING = “JDBC:hive2://主机:端口/分贝; principal=principal=hive/HiveServer2Host@YOUR-REALM.COM mapreduce.job.queuename = PROD”;