jcamachor commented on a change in pull request #829: Hive JDBC Storage Handler: Support For Writing Data to JDBC Data Source URL: https://github.com/apache/hive/pull/829#discussion_r339816176
########## File path: jdbc-handler/src/main/java/org/apache/hive/storage/jdbc/dao/GenericJdbcDatabaseAccessor.java ########## @@ -172,6 +177,67 @@ public int getTotalNumberOfRecords(Configuration conf) throws HiveJdbcDatabaseAc } } + public RecordWriter getRecordWriter(TaskAttemptContext context) + throws IOException { + Configuration conf = context.getConfiguration(); + Connection conn = null; + PreparedStatement ps = null; + String dbProductName = conf.get(JdbcStorageConfig.DATABASE_TYPE.getPropertyName()).toUpperCase(); + String tableName = conf.get(JdbcStorageConfig.TABLE.getPropertyName()); + + if (tableName == null || tableName.isEmpty()) { + throw new IllegalArgumentException("Table name should be defined"); + } + + String[] columnNames = conf.get(serdeConstants.LIST_COLUMNS).split(","); + + try { + initializeDatabaseConnection(conf); + conn = dbcpDataSource.getConnection(); + ps = conn.prepareStatement( + constructQuery(tableName, columnNames, dbProductName)); + return new org.apache.hadoop.mapreduce.lib.db.DBOutputFormat() + .new DBRecordWriter(conn, ps); + } catch (Exception e) { + cleanupResources(conn, ps, null); + throw new IOException(e.getMessage()); + } + } + + /** + * Constructs the query used as the prepared statement to insert data. + * + * @param table + * the table to insert into + * @param columnNames + * the columns to insert into. + * @param dbProductName + * type of database + * + */ + public String constructQuery(String table, String[] columnNames, String dbProductName) { + if(columnNames == null) { + throw new IllegalArgumentException("Column names may not be null"); + } + + StringBuilder query = new StringBuilder(); + query.append("INSERT INTO ").append(table).append(" VALUES ("); + + for (int i = 0; i < columnNames.length; i++) { + query.append("?"); + if(i != columnNames.length - 1) { + query.append(","); + } + } + + if (!dbProductName.equals(DatabaseType.DERBY.toString()) && !dbProductName.equals(DatabaseType.ORACLE.toString()) Review comment: Specific logic for different database products should not be handled with if clauses over here. Instead, you can create a method with default implementation for most common case in this generic accessor and override behavior if needed on a per-accessor basis (see e.g., `addLimitToQuery` method for an example, which is overridden for Oracle accessor, MySQL, etc). ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: gitbox-unsubscr...@hive.apache.org For additional commands, e-mail: gitbox-h...@hive.apache.org