|
| 1 | +/** |
| 2 | + * Copyright 2011 Nube Technologies |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software distributed |
| 11 | + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | + * CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and limitations under the License. |
| 14 | + */ |
| 15 | +package co.nubetech.hiho.job; |
| 16 | + |
| 17 | +import org.apache.hadoop.conf.Configuration; |
| 18 | +import org.apache.hadoop.conf.Configured; |
| 19 | +import org.apache.hadoop.fs.Path; |
| 20 | +import org.apache.hadoop.mapreduce.Job; |
| 21 | +import org.apache.hadoop.mapreduce.lib.input.TextInputFormat; |
| 22 | +import org.apache.hadoop.util.Tool; |
| 23 | +import org.apache.hadoop.util.ToolRunner; |
| 24 | + |
| 25 | +import co.nubetech.apache.hadoop.DBConfiguration; |
| 26 | +import co.nubetech.apache.hadoop.MRJobConfig; |
| 27 | +import co.nubetech.hiho.common.HIHOConf; |
| 28 | +import co.nubetech.hiho.common.HIHOException; |
| 29 | +import co.nubetech.hiho.mapreduce.GenericDBLoadDataMapper; |
| 30 | +import co.nubetech.hiho.mapreduce.lib.db.GenericDBOutputFormat; |
| 31 | + |
| 32 | +public class ExportToDB extends Configured implements Tool { |
| 33 | + |
| 34 | + private String inputPath = null; |
| 35 | + private String tableName = null; |
| 36 | + private String columnNames = null; |
| 37 | + |
| 38 | + public void populateConfiguration(String[] args, Configuration conf) { |
| 39 | + for (int i = 0; i < args.length - 1; i++) { |
| 40 | + if ("-jdbcDriver".equals(args[i])) { |
| 41 | + conf.set(DBConfiguration.DRIVER_CLASS_PROPERTY, args[++i]); |
| 42 | + } else if ("-jdbcUrl".equals(args[i])) { |
| 43 | + conf.set(DBConfiguration.URL_PROPERTY, args[++i]); |
| 44 | + } else if ("-jdbcUsername".equals(args[i])) { |
| 45 | + conf.set(DBConfiguration.USERNAME_PROPERTY, args[++i]); |
| 46 | + } else if ("-jdbcPassword".equals(args[i])) { |
| 47 | + conf.set(DBConfiguration.PASSWORD_PROPERTY, args[++i]); |
| 48 | + } else if ("-delimiter".equals(args[i])) { |
| 49 | + conf.set(HIHOConf.INPUT_OUTPUT_DELIMITER, args[++i]); |
| 50 | + } else if ("-numberOfMappers".equals(args[i])) { |
| 51 | + conf.set(HIHOConf.NUMBER_MAPPERS, args[++i]); |
| 52 | + } else if ("-tableName".equals(args[i])) { |
| 53 | + tableName = args[++i]; |
| 54 | + } else if ("-columnNames".equals(args[i])) { |
| 55 | + columnNames = args[++i]; |
| 56 | + } else if ("-inputPath".equals(args[i])) { |
| 57 | + inputPath = args[++i]; |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + public void checkMandatoryConfs(Configuration conf) throws HIHOException { |
| 63 | + if (conf.get(DBConfiguration.DRIVER_CLASS_PROPERTY) == null) { |
| 64 | + throw new HIHOException( |
| 65 | + "JDBC driver configuration is not specified,please specify JDBC driver class."); |
| 66 | + } |
| 67 | + if (conf.get(DBConfiguration.URL_PROPERTY) == null) { |
| 68 | + throw new HIHOException( |
| 69 | + "JDBC url path configuration is empty,please specify JDBC url path."); |
| 70 | + } |
| 71 | + if (!conf.get(DBConfiguration.DRIVER_CLASS_PROPERTY).contains("hsqldb")) { |
| 72 | + if (conf.get(DBConfiguration.USERNAME_PROPERTY) == null) { |
| 73 | + throw new HIHOException( |
| 74 | + "JDBC user name configuration is empty,please specify JDBC user name."); |
| 75 | + } |
| 76 | + if (conf.get(DBConfiguration.PASSWORD_PROPERTY) == null) { |
| 77 | + throw new HIHOException( |
| 78 | + "JDBC password configuration is empty,please specify JDBC password."); |
| 79 | + } |
| 80 | + } |
| 81 | + if (conf.get(HIHOConf.INPUT_OUTPUT_DELIMITER) == null) { |
| 82 | + throw new HIHOException( |
| 83 | + "The provided delimiter is empty, please specify delimiter."); |
| 84 | + } |
| 85 | + if (conf.get(HIHOConf.NUMBER_MAPPERS) == null) { |
| 86 | + throw new HIHOException( |
| 87 | + "The provided number of mappers is empty, please specify number of mappers."); |
| 88 | + } |
| 89 | + if (inputPath == null) { |
| 90 | + throw new HIHOException( |
| 91 | + "The provided input path is empty, please specify inputPath."); |
| 92 | + } |
| 93 | + if (tableName == null) { |
| 94 | + throw new HIHOException( |
| 95 | + "The provided table name is empty, please specify tableName."); |
| 96 | + } |
| 97 | + if (columnNames == null) { |
| 98 | + throw new HIHOException( |
| 99 | + "The provided column name is empty, please specify columnName."); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + public int run(String[] args) throws Exception { |
| 104 | + Configuration conf = getConf(); |
| 105 | + populateConfiguration(args, conf); |
| 106 | + try { |
| 107 | + checkMandatoryConfs(conf); |
| 108 | + } catch (HIHOException e1) { |
| 109 | + e1.printStackTrace(); |
| 110 | + throw new Exception(e1); |
| 111 | + } |
| 112 | + Job job = new Job(conf); |
| 113 | + job.getConfiguration().setInt(MRJobConfig.NUM_MAPS, |
| 114 | + conf.getInt(HIHOConf.NUMBER_MAPPERS, 1)); |
| 115 | + job.setJobName("HihoDBExport"); |
| 116 | + |
| 117 | + job.setMapperClass(GenericDBLoadDataMapper.class); |
| 118 | + job.setJarByClass(ExportToDB.class); |
| 119 | + job.setNumReduceTasks(0); |
| 120 | + job.setInputFormatClass(TextInputFormat.class); |
| 121 | + TextInputFormat.addInputPath(job, new Path(inputPath)); |
| 122 | + GenericDBOutputFormat.setOutput(job, tableName, columnNames); |
| 123 | + |
| 124 | + int ret = 0; |
| 125 | + try { |
| 126 | + ret = job.waitForCompletion(true) ? 0 : 1; |
| 127 | + } catch (Exception e) { |
| 128 | + e.printStackTrace(); |
| 129 | + } |
| 130 | + return ret; |
| 131 | + |
| 132 | + } |
| 133 | + |
| 134 | + public static void main(String[] args) throws Exception { |
| 135 | + int res = ToolRunner.run(new Configuration(), new ExportToDB(), args); |
| 136 | + System.exit(res); |
| 137 | + } |
| 138 | + |
| 139 | +} |
0 commit comments