简体   繁体   English

Hive语句未使用Java类将数据插入表中

[英]Hive statement not inserting data into the table using java class

I am trying to insert data into hive tables from other tables in java class using hive statement instance, both, source tables and target table, are present in same hive database. 我正在尝试使用hive语句实例将数据从java类中的其他表插入hive表中,源表和目标表都存在于同一个hive数据库中。

All source tables does contain data. 所有源表都包含数据。

Following class executes the above query: 以下类执行上述查询:

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

When executed a SELECT query it does bring data but it fails/returns 0 for insert query. 当执行SELECT查询时,它确实带来了数据,但是对于插入查询,它失败/返回0。

When executed the same INSERT query from hive CLI it works as expected, inserts the data into target table. 从hive CLI执行相同的INSERT查询时,它会按预期工作,将数据插入目标表中。

Try to specify queue name in connection string: 尝试在连接字符串中指定队列名称:

CONNECTION_STRING="jdbc:hive2://host:port/db;principal=principal=hive/HiveServer2Host@YOUR-REALM.COM?mapreduce.job.queuename=prod"; CONNECTION_STRING =“ jdbc:hive2:// host:port / db; principal=principal=hive/HiveServer2Host@YOUR-REALM.COM?mapreduce.job.queuename = prod”;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM