Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 48BDC200CC5 for ; Tue, 11 Jul 2017 18:24:55 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 473AE1665DF; Tue, 11 Jul 2017 16:24:55 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 661DF1665D7 for ; Tue, 11 Jul 2017 18:24:54 +0200 (CEST) Received: (qmail 31774 invoked by uid 500); 11 Jul 2017 16:24:51 -0000 Mailing-List: contact common-commits-help@hadoop.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Delivered-To: mailing list common-commits@hadoop.apache.org Received: (qmail 31010 invoked by uid 99); 11 Jul 2017 16:24:50 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 11 Jul 2017 16:24:50 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id E4C86F5536; Tue, 11 Jul 2017 16:24:48 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: rakeshr@apache.org To: common-commits@hadoop.apache.org Date: Tue, 11 Jul 2017 16:24:53 -0000 Message-Id: <92b566c5c68647749036b5b499ac695c@git.apache.org> In-Reply-To: <338233d9fb704c7586da676be7e68fa8@git.apache.org> References: <338233d9fb704c7586da676be7e68fa8@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: =?utf-8?q?=5B07/50=5D_=5Babbrv=5D_hadoop_git_commit=3A_MAPREDUCE-6?= =?utf-8?q?246=2E_DBOutputFormat=2Ejava_appending_extra_semicolon_to_query_w?= =?utf-8?q?hich_is_incompatible_with_DB2=2E_Contributed_by_ramtin_and_Gergel?= =?utf-8?b?eSBOb3bDoWsu?= archived-at: Tue, 11 Jul 2017 16:24:55 -0000 MAPREDUCE-6246. DBOutputFormat.java appending extra semicolon to query which is incompatible with DB2. Contributed by ramtin and Gergely Novák. Project: http://git-wip-us.apache.org/repos/asf/hadoop/repo Commit: http://git-wip-us.apache.org/repos/asf/hadoop/commit/f484a6ff Tree: http://git-wip-us.apache.org/repos/asf/hadoop/tree/f484a6ff Diff: http://git-wip-us.apache.org/repos/asf/hadoop/diff/f484a6ff Branch: refs/heads/HDFS-10285 Commit: f484a6ff602d48413556a1d046670e2003c71c2e Parents: f10864a Author: Junping Du Authored: Fri Jul 7 13:23:43 2017 -0700 Committer: Junping Du Committed: Fri Jul 7 13:26:16 2017 -0700 ---------------------------------------------------------------------- .../hadoop/mapreduce/lib/db/DBOutputFormat.java | 15 ++++++- .../mapreduce/lib/db/TestDBOutputFormat.java | 45 ++++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/hadoop/blob/f484a6ff/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/lib/db/DBOutputFormat.java ---------------------------------------------------------------------- diff --git a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/lib/db/DBOutputFormat.java b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/lib/db/DBOutputFormat.java index 2e3a9d8..c222bf5 100644 --- a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/lib/db/DBOutputFormat.java +++ b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/lib/db/DBOutputFormat.java @@ -20,6 +20,7 @@ package org.apache.hadoop.mapreduce.lib.db; import java.io.IOException; import java.sql.Connection; +import java.sql.DatabaseMetaData; import java.sql.PreparedStatement; import java.sql.SQLException; @@ -51,6 +52,8 @@ public class DBOutputFormat extends OutputFormat { private static final Log LOG = LogFactory.getLog(DBOutputFormat.class); + public String dbProductName = "DEFAULT"; + public void checkOutputSpecs(JobContext context) throws IOException, InterruptedException {} @@ -158,7 +161,12 @@ extends OutputFormat { query.append(","); } } - query.append(");"); + + if (dbProductName.startsWith("DB2") || dbProductName.startsWith("ORACLE")) { + query.append(")"); + } else { + query.append(");"); + } return query.toString(); } @@ -177,7 +185,10 @@ extends OutputFormat { try { Connection connection = dbConf.getConnection(); PreparedStatement statement = null; - + + DatabaseMetaData dbMeta = connection.getMetaData(); + this.dbProductName = dbMeta.getDatabaseProductName().toUpperCase(); + statement = connection.prepareStatement( constructQuery(tableName, fieldNames)); return new DBRecordWriter(connection, statement); http://git-wip-us.apache.org/repos/asf/hadoop/blob/f484a6ff/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-jobclient/src/test/java/org/apache/hadoop/mapreduce/lib/db/TestDBOutputFormat.java ---------------------------------------------------------------------- diff --git a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-jobclient/src/test/java/org/apache/hadoop/mapreduce/lib/db/TestDBOutputFormat.java b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-jobclient/src/test/java/org/apache/hadoop/mapreduce/lib/db/TestDBOutputFormat.java index 014855f..e547c8a 100644 --- a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-jobclient/src/test/java/org/apache/hadoop/mapreduce/lib/db/TestDBOutputFormat.java +++ b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-jobclient/src/test/java/org/apache/hadoop/mapreduce/lib/db/TestDBOutputFormat.java @@ -18,7 +18,9 @@ package org.apache.hadoop.mapreduce.lib.db; import java.io.IOException; +import java.lang.reflect.Field; +import org.apache.commons.lang.StringUtils; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.io.NullWritable; import org.apache.hadoop.mapreduce.Job; @@ -26,6 +28,7 @@ import org.junit.Test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; +import static org.junit.Assert.fail; public class TestDBOutputFormat { private String[] fieldNames = new String[] { "id", "name", "value" }; @@ -47,6 +50,48 @@ public class TestDBOutputFormat { } @Test + public void testDB2ConstructQuery() { + String db2expected = StringUtils.removeEnd(expected, ";"); + String db2nullExpected = StringUtils.removeEnd(nullExpected, ";"); + + try { + Class clazz = this.format.getClass(); + Field field = clazz.getDeclaredField("dbProductName"); + field.setAccessible(true); + field.set(format, "DB2"); + } catch (IllegalAccessException | NoSuchFieldException e) { + fail(e.getMessage()); + } + + String actual = format.constructQuery("hadoop_output", fieldNames); + assertEquals(db2expected, actual); + + actual = format.constructQuery("hadoop_output", nullFieldNames); + assertEquals(db2nullExpected, actual); + } + + @Test + public void testORACLEConstructQuery() { + String oracleExpected = StringUtils.removeEnd(expected, ";"); + String oracleNullExpected = StringUtils.removeEnd(nullExpected, ";"); + + try { + Class clazz = this.format.getClass(); + Field field = clazz.getDeclaredField("dbProductName"); + field.setAccessible(true); + field.set(format, "ORACLE"); + } catch (IllegalAccessException | NoSuchFieldException e) { + fail(e.getMessage()); + } + + String actual = format.constructQuery("hadoop_output", fieldNames); + assertEquals(oracleExpected, actual); + + actual = format.constructQuery("hadoop_output", nullFieldNames); + assertEquals(oracleNullExpected, actual); + } + + @Test public void testSetOutput() throws IOException { Job job = Job.getInstance(new Configuration()); DBOutputFormat.setOutput(job, "hadoop_output", fieldNames); --------------------------------------------------------------------- To unsubscribe, e-mail: common-commits-unsubscribe@hadoop.apache.org For additional commands, e-mail: common-commits-help@hadoop.apache.org