494c75b0845aa23a5fa12740f01093d5b2175c53
[sqoop.git] / src / test / org / apache / sqoop / manager / db2 / DB2XmlTypeImportManualTest.java
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package org.apache.sqoop.manager.db2;
19
20 import java.io.BufferedReader;
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.IOException;
24 import java.io.InputStreamReader;
25 import java.sql.Connection;
26 import java.sql.SQLException;
27 import java.sql.Statement;
28 import java.util.ArrayList;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.hadoop.fs.Path;
33 import org.apache.hadoop.io.IOUtils;
34 import org.apache.sqoop.manager.Db2Manager;
35 import org.apache.sqoop.testcategories.thirdpartytest.Db2Test;
36 import org.apache.sqoop.testcategories.sqooptest.ManualTest;
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Test;
40
41 import org.apache.sqoop.SqoopOptions;
42 import org.apache.sqoop.testutil.CommonArgs;
43 import org.apache.sqoop.testutil.ImportJobTestCase;
44 import org.apache.sqoop.util.FileListing;
45 import org.junit.experimental.categories.Category;
46
47 import static org.junit.Assert.assertEquals;
48 import static org.junit.Assert.assertTrue;
49 import static org.junit.Assert.fail;
50
51 /**
52 * Test the DB2 XML data type.
53 *
54 * This uses JDBC to import data from an DB2 database into HDFS.
55 *
56 * Since this requires an DB2 Server installation,
57 * this class is named in such a way that Sqoop's default QA process does
58 * not run it. You need to run this manually with
59 * -Dtestcase=DB2XmlTypeImportManualTest
60
61 * You need to put DB2 JDBC driver library (db2jcc.jar) in a location
62 * where Sqoop will be able to access it (since this library cannot be checked
63 * into Apache's tree for licensing reasons).
64 *
65 * To set up your test environment:
66 * Install DB2 Express 9.7 C server.
67 * Create a database SQOOP
68 * Create a login SQOOP with password PASSWORD and grant all
69 * access for database SQOOP to user SQOOP.
70 */
71 @Category({ManualTest.class, Db2Test.class})
72 public class DB2XmlTypeImportManualTest extends ImportJobTestCase {
73
74 public static final Log LOG = LogFactory.getLog(
75 DB2XmlTypeImportManualTest.class.getName());
76
77 static final String HOST_URL = System.getProperty(
78 "sqoop.test.db2.connectstring.host_url",
79 "jdbc:db2://db2host:60000");
80
81 static final String DATABASE_NAME = System.getProperty(
82 "sqoop.test.db2.connectstring.database",
83 "SQOOP");
84 static final String DATABASE_USER = System.getProperty(
85 "sqoop.test.db2.connectstring.username",
86 "SQOOP");
87 static final String DATABASE_PASSWORD = System.getProperty(
88 "sqoop.test.db2.connectstring.password",
89 "SQOOP");
90
91 static final String TABLE_NAME = "COMPANY";
92 static final String CONNECT_STRING = HOST_URL
93 + "/" + DATABASE_NAME;
94 static final String HIVE_TABLE_NAME = "HCOMPANY";
95 static String ExpectedResults =
96 "1,doc1,<company name=\"Company1\"><emp id=\"31201\" salary=\"60000\" gender=\"Female\"><name><first>Laura </first><last>Brown</last></name><dept id=\"M25\">Finance</dept></emp></company>";
97
98
99 static {
100 LOG.info("Using DB2 CONNECT_STRING HOST_URL is : "+HOST_URL);
101 LOG.info("Using DB2 CONNECT_STRING: " + CONNECT_STRING);
102 }
103
104 // instance variables populated during setUp, used during tests
105 private Db2Manager manager;
106
107 protected String getTableName() {
108 return TABLE_NAME;
109 }
110
111
112 @Before
113 public void setUp() {
114 super.setUp();
115
116 SqoopOptions options = new SqoopOptions(CONNECT_STRING, getTableName());
117 options.setUsername(DATABASE_USER);
118 options.setPassword(DATABASE_PASSWORD);
119
120 manager = new Db2Manager(options);
121
122 // Drop the existing table, if there is one.
123 Connection conn = null;
124 Statement stmt = null;
125 try {
126 conn = manager.getConnection();
127 stmt = conn.createStatement();
128 stmt.execute("DROP TABLE " + getTableName());
129 } catch (SQLException sqlE) {
130 LOG.info("Table was not dropped: " + sqlE.getMessage());
131 } finally {
132 try {
133 if (null != stmt) {
134 stmt.close();
135 }
136 } catch (Exception ex) {
137 LOG.warn("Exception while closing stmt", ex);
138 }
139 }
140
141 // Create and populate table
142 try {
143 conn = manager.getConnection();
144 conn.setAutoCommit(false);
145 stmt = conn.createStatement();
146 String xml ="xmlparse(document '<company name=\"Company1\">\n"+
147 "<emp id=\"31201\" salary=\"60000\" gender=\"Female\">"+
148 "<name><first>Laura </first><last>Brown</last></name>"+
149 "<dept id=\"M25\">Finance</dept></emp></company>')";
150
151
152 // create the database table and populate it with data.
153 stmt.executeUpdate("CREATE TABLE " + getTableName() + " ("
154 + "ID int, "
155 + "DOCNAME VARCHAR(20), "
156 + "DOC XML)");
157
158 stmt.executeUpdate("INSERT INTO " + getTableName() + " VALUES("
159 + "1,'doc1', "
160 + xml
161 +" )");
162 conn.commit();
163 } catch (SQLException sqlE) {
164 LOG.error("Encountered SQL Exception: ", sqlE);
165 fail("SQLException when running test setUp(): " + sqlE);
166 } finally {
167 try {
168 if (null != stmt) {
169 stmt.close();
170 }
171 } catch (Exception ex) {
172 LOG.warn("Exception while closing connection/stmt", ex);
173 }
174 }
175 }
176
177 @After
178 public void tearDown() {
179 super.tearDown();
180 try {
181 manager.close();
182 } catch (SQLException sqlE) {
183 LOG.error("Got SQLException: " + sqlE);
184 }
185 }
186
187 @Test
188 public void testDb2Import() throws IOException {
189
190 runDb2Test(ExpectedResults);
191
192 }
193
194 private String [] getArgv() {
195 ArrayList<String> args = new ArrayList<String>();
196
197 CommonArgs.addHadoopFlags(args);
198 args.add("--connect");
199 args.add(CONNECT_STRING);
200 args.add("--username");
201 args.add(DATABASE_USER);
202 args.add("--password");
203 args.add(DATABASE_PASSWORD);
204
205 args.add("--table");
206 args.add(TABLE_NAME);
207 args.add("--warehouse-dir");
208 args.add(getWarehouseDir());
209 args.add("--hive-table");
210 args.add(HIVE_TABLE_NAME);
211 args.add("--num-mappers");
212 args.add("1");
213
214 return args.toArray(new String[0]);
215 }
216
217 private void runDb2Test(String expectedResults) throws IOException {
218
219 Path warehousePath = new Path(this.getWarehouseDir());
220 Path tablePath = new Path(warehousePath, getTableName());
221 Path filePath = new Path(tablePath, "part-m-00000");
222
223 File tableFile = new File(tablePath.toString());
224 if (tableFile.exists() && tableFile.isDirectory()) {
225 // remove the directory before running the import.
226 FileListing.recursiveDeleteDir(tableFile);
227 }
228
229 String [] argv = getArgv();
230 try {
231 runImport(argv);
232 LOG.info("finish runImport with argv is : "+argv);
233 } catch (IOException ioe) {
234 LOG.error("Got IOException during import: " + ioe);
235 fail(ioe.toString());
236 }
237
238 File f = new File(filePath.toString());
239 assertTrue("Could not find imported data file", f.exists());
240 BufferedReader r = null;
241 try {
242 // Read through the file and make sure it's all there.
243 r = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
244 assertEquals(expectedResults, r.readLine());
245 } catch (IOException ioe) {
246 LOG.error("Got IOException verifying results: " + ioe);
247 fail(ioe.toString());
248 } finally {
249 IOUtils.closeStream(r);
250 }
251 }
252
253 }