|
@@ -99,10 +99,31 @@ implements OutputFormat<K,V> {
|
|
|
|
|
|
/**
|
|
/**
|
|
* Constructs the query used as the prepared statement to insert data.
|
|
* Constructs the query used as the prepared statement to insert data.
|
|
|
|
+ *
|
|
|
|
+ * @param table
|
|
|
|
+ * the table to insert into
|
|
|
|
+ * @param fieldNames
|
|
|
|
+ * the fields to insert into. If field names are unknown, supply an
|
|
|
|
+ * array of nulls.
|
|
*/
|
|
*/
|
|
protected String constructQuery(String table, String[] fieldNames) {
|
|
protected String constructQuery(String table, String[] fieldNames) {
|
|
|
|
+ if(fieldNames == null) {
|
|
|
|
+ throw new IllegalArgumentException("Field names may not be null");
|
|
|
|
+ }
|
|
|
|
+
|
|
StringBuilder query = new StringBuilder();
|
|
StringBuilder query = new StringBuilder();
|
|
- query.append("INSERT INTO ").append(table);
|
|
|
|
|
|
+ query.append("INSERT INTO ").append(table);
|
|
|
|
+
|
|
|
|
+ if (fieldNames.length > 0 && fieldNames[0] != null) {
|
|
|
|
+ query.append(" (");
|
|
|
|
+ for (int i = 0; i < fieldNames.length; i++) {
|
|
|
|
+ query.append(fieldNames[i]);
|
|
|
|
+ if (i != fieldNames.length - 1) {
|
|
|
|
+ query.append(",");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ query.append(")");
|
|
|
|
+ }
|
|
query.append(" VALUES (");
|
|
query.append(" VALUES (");
|
|
|
|
|
|
for (int i = 0; i < fieldNames.length; i++) {
|
|
for (int i = 0; i < fieldNames.length; i++) {
|
|
@@ -145,9 +166,13 @@ implements OutputFormat<K,V> {
|
|
/**
|
|
/**
|
|
* Initializes the reduce-part of the job with the appropriate output settings
|
|
* Initializes the reduce-part of the job with the appropriate output settings
|
|
*
|
|
*
|
|
- * @param job The job
|
|
|
|
- * @param tableName The table to insert data into
|
|
|
|
- * @param fieldNames The field names in the table
|
|
|
|
|
|
+ * @param job
|
|
|
|
+ * The job
|
|
|
|
+ * @param tableName
|
|
|
|
+ * The table to insert data into
|
|
|
|
+ * @param fieldNames
|
|
|
|
+ * The field names in the table. If unknown, supply the appropriate
|
|
|
|
+ * number of nulls.
|
|
*/
|
|
*/
|
|
public static void setOutput(JobConf job, String tableName, String... fieldNames) {
|
|
public static void setOutput(JobConf job, String tableName, String... fieldNames) {
|
|
job.setOutputFormat(DBOutputFormat.class);
|
|
job.setOutputFormat(DBOutputFormat.class);
|