5
0
mirror of https://github.com/apache/sqoop.git synced 2025-05-06 10:51:11 +08:00

SQOOP-2092: Support for LONG in MInputType

(Veena Basavaraj via Jarek Jarcec Cecho)
This commit is contained in:
Jarek Jarcec Cecho 2015-02-16 07:15:09 -08:00
parent 01bae79d2b
commit 88588a8b45
14 changed files with 229 additions and 11 deletions

View File

@ -29,6 +29,7 @@
import org.apache.sqoop.model.MInput;
import org.apache.sqoop.model.MInputType;
import org.apache.sqoop.model.MIntegerInput;
import org.apache.sqoop.model.MLongInput;
import org.apache.sqoop.model.MMapInput;
import org.apache.sqoop.model.MStringInput;
import org.json.simple.JSONArray;
@ -166,6 +167,10 @@ static MConfig restoreConfig(JSONObject config) {
mInput = new MIntegerInput(name, sensitive.booleanValue(), editable, overrides);
break;
}
case LONG: {
mInput = new MLongInput(name, sensitive.booleanValue(), editable, overrides);
break;
}
case BOOLEAN: {
mInput = new MBooleanInput(name, sensitive.booleanValue(), editable, overrides);
break;

View File

@ -158,6 +158,8 @@ private static MConfig toConfig(String configName, Class klass, Object object) {
input = new MMapInput(inputName, sensitive, editable, overrides);
} else if (type == Integer.class) {
input = new MIntegerInput(inputName, sensitive, editable, overrides);
} else if (type == Long.class) {
input = new MLongInput(inputName, sensitive, editable, overrides);
} else if (type == Boolean.class) {
input = new MBooleanInput(inputName, sensitive, editable, overrides);
} else if (type.isEnum()) {
@ -439,7 +441,7 @@ public static String toJson(Object configuration) {
map.put(key, ((Map) value).get(key));
}
jsonConfig.put(inputName, map);
} else if(type == Integer.class) {
} else if(type == Integer.class || type == Long.class) {
jsonConfig.put(inputName, value);
} else if(type.isEnum()) {
jsonConfig.put(inputName, value.toString());
@ -532,6 +534,8 @@ public static void fillValues(String json, Object configuration) {
inputField.set(configValue, map);
} else if(type == Integer.class) {
inputField.set(configValue, ((Long)jsonInputs.get(inputName)).intValue());
} else if(type == Long.class) {
inputField.set(configValue, ((Long)jsonInputs.get(inputName)).longValue());
} else if(type.isEnum()) {
inputField.set(configValue, Enum.valueOf((Class<? extends Enum>) inputField.getType(), (String) jsonInputs.get(inputName)));
} else if(type == Boolean.class) {

View File

@ -86,6 +86,10 @@ public MIntegerInput getIntegerInput(String inputName) {
return (MIntegerInput)getInput(inputName);
}
public MLongInput getLongInput(String inputName) {
return (MLongInput)getInput(inputName);
}
public MBooleanInput getBooleanInput(String inputName) {
return (MBooleanInput)getInput(inputName);
}

View File

@ -77,6 +77,10 @@ public MIntegerInput getIntegerInput(String name) {
return (MIntegerInput) getInput(name);
}
public MLongInput getLongInput(String name) {
return (MLongInput) getInput(name);
}
public MMapInput getMapInput(String name) {
return (MMapInput) getInput(name);
}

View File

@ -45,5 +45,8 @@ public enum MInputType {
/** String based input that can contain only predefined values **/
ENUM,
/** Long input type */
LONG,
;
}

View File

@ -0,0 +1,96 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.sqoop.model;
import org.apache.sqoop.classification.InterfaceAudience;
import org.apache.sqoop.classification.InterfaceStability;
/**
* Long user input.
*
*/
@InterfaceAudience.Public
@InterfaceStability.Unstable
public class MLongInput extends MInput<Long> {
public MLongInput(String name, boolean sensitive, InputEditable editable, String overrides) {
super(name, sensitive, editable, overrides);
}
@Override
public String getUrlSafeValueString() {
if(isEmpty()) {
return "";
}
return getValue().toString();
}
@Override
public void restoreFromUrlSafeValueString(String valueString) {
if(valueString.isEmpty()) {
setEmpty();
}
setValue(Long.valueOf(valueString));
}
@Override
public MInputType getType() {
return MInputType.LONG;
}
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if (!(other instanceof MLongInput)) {
return false;
}
MLongInput i = (MLongInput) other;
return getName().equals(i.getName());
}
@Override
public int hashCode() {
return 23 + 31 * getName().hashCode();
}
@Override
public boolean isEmpty() {
return getValue() == null;
}
@Override
public void setEmpty() {
setValue(null);
}
@Override
public MLongInput clone(boolean cloneWithValue) {
MLongInput copy = new MLongInput(getName(), isSensitive(), getEditable(), getOverrides());
copy.setPersistenceId(getPersistenceId());
if(cloneWithValue) {
copy.setValue(this.getValue());
}
return copy;
}
}

View File

@ -35,6 +35,7 @@
import org.apache.sqoop.model.MEnumInput;
import org.apache.sqoop.model.MInput;
import org.apache.sqoop.model.MIntegerInput;
import org.apache.sqoop.model.MLongInput;
import org.apache.sqoop.model.MMapInput;
import org.apache.sqoop.model.MStringInput;
import org.json.simple.JSONObject;
@ -136,7 +137,7 @@ protected MConfig getMapConfig() {
*/
protected MConfig getConfig() {
List<MInput<?>> inputs;
MInput input;
MInput<?> input;
inputs = new LinkedList<MInput<?>>();
@ -149,6 +150,9 @@ protected MConfig getConfig() {
input = new MIntegerInput("Integer", false, InputEditable.ANY, StringUtils.EMPTY);
inputs.add(input);
input = new MLongInput("Long", false, InputEditable.ANY, StringUtils.EMPTY);
inputs.add(input);
input = new MBooleanInput("Boolean", false, InputEditable.ANY, StringUtils.EMPTY);
inputs.add(input);

View File

@ -175,7 +175,7 @@ public void testJson() {
TestConfiguration config = new TestConfiguration();
config.aConfig.a1 = "A";
config.bConfig.b2 = "B";
config.cConfig.intValue = 4;
config.cConfig.longValue = 4L;
config.cConfig.map.put("C", "D");
config.cConfig.enumeration = Enumeration.X;
@ -197,7 +197,7 @@ public void testJson() {
assertNull(targetConfig.bConfig.b1);
AssertJUnit.assertEquals("B", targetConfig.bConfig.b2);
AssertJUnit.assertEquals((Integer) 4, targetConfig.cConfig.intValue);
AssertJUnit.assertEquals((Long) 4L, targetConfig.cConfig.longValue);
AssertJUnit.assertEquals(1, targetConfig.cConfig.map.size());
AssertJUnit.assertTrue(targetConfig.cConfig.map.containsKey("C"));
AssertJUnit.assertEquals("D", targetConfig.cConfig.map.get("C"));
@ -231,7 +231,7 @@ protected List<MConfig> getConfigs() {
// Config C
inputs = new LinkedList<MInput<?>>();
inputs.add(new MIntegerInput("cConfig.intValue", false, InputEditable.ANY, StringUtils.EMPTY));
inputs.add(new MLongInput("cConfig.longValue", false, InputEditable.ANY, StringUtils.EMPTY));
inputs.add(new MMapInput("cConfig.map", false, InputEditable.ANY, StringUtils.EMPTY));
inputs.add(new MEnumInput("cConfig.enumeration", false, InputEditable.ANY, StringUtils.EMPTY,
new String[] { "X", "Y" }));
@ -411,7 +411,7 @@ public static class BConfig {
@ConfigClass
public static class CConfig {
@Input
Integer intValue;
Long longValue;
@Input
Map<String, String> map;
@Input

View File

@ -37,9 +37,12 @@ public class TestMAccountableEntity {
@Test
public void testInitialization() {
List<MConfig> configs = new ArrayList<MConfig>();
MIntegerInput input = new MIntegerInput("INTEGER-INPUT", false, InputEditable.ANY, StringUtils.EMPTY);
MIntegerInput intInput = new MIntegerInput("INTEGER-INPUT", false, InputEditable.ANY, StringUtils.EMPTY);
MLongInput longInput = new MLongInput("LONG-INPUT", false, InputEditable.ANY, StringUtils.EMPTY);
List<MInput<?>> list = new ArrayList<MInput<?>>();
list.add(input);
list.add(intInput);
list.add(longInput);
MConfig config = new MConfig("CONFIGNAME", list);
configs.add(config);
MAccountableEntity link = new MLink(123l, new MLinkConfig(configs));
@ -57,5 +60,8 @@ public void testInitialization() {
assertEquals(testLastUpdateDate, link.getLastUpdateDate());
assertEquals(false, link.getEnabled());
assertEquals("user", link.getLastUpdateUser());
assertEquals(1, ((MLink) link).getConnectorLinkConfig().getConfigs().size());
assertEquals(2, ((MLink) link).getConnectorLinkConfig().getConfigs().get(0).getInputs().size());
}
}

View File

@ -54,20 +54,21 @@ public void testEquals() {
List<MInput<?>> list1 = new ArrayList<MInput<?>>();
list1.add(input1);
list1.add(input2);
MConfig mform1 = new MConfig("config", list1);
MConfig mConfig1 = new MConfig("config", list1);
MInput<Integer> input3 = new MIntegerInput("sqoopsqoop1", false, InputEditable.ANY, StringUtils.EMPTY );
MInput<Integer> input4 = new MIntegerInput("sqoopsqoop2", false, InputEditable.ANY, StringUtils.EMPTY );
List<MInput<?>> list2 = new ArrayList<MInput<?>>();
list2.add(input3);
list2.add(input4);
MConfig mform2 = new MConfig("config", list2);
assertEquals(mform2, mform1);
MConfig mConfig2 = new MConfig("config", list2);
assertEquals(mConfig2, mConfig1);
}
@Test
public void testGetInputs() {
MIntegerInput intInput = new MIntegerInput("Config.A", false, InputEditable.ANY, StringUtils.EMPTY );
MLongInput longInput = new MLongInput("Config.A1", false, InputEditable.ANY, StringUtils.EMPTY );
MMapInput mapInput = new MMapInput("Config.B", false, InputEditable.ANY, StringUtils.EMPTY );
MStringInput stringInput = new MStringInput("Config.C", false, InputEditable.ANY,
StringUtils.EMPTY, (short) 3);
@ -76,12 +77,14 @@ public void testGetInputs() {
List<MInput<?>> inputs = new ArrayList<MInput<?>>();
inputs.add(intInput);
inputs.add(longInput);
inputs.add(mapInput);
inputs.add(stringInput);
inputs.add(enumInput);
MConfig config = new MConfig("Config", inputs);
assertEquals(intInput, config.getIntegerInput("Config.A"));
assertEquals(longInput, config.getLongInput("Config.A1"));
assertEquals(mapInput, config.getMapInput("Config.B"));
assertEquals(stringInput, config.getStringInput("Config.C"));
assertEquals(enumInput, config.getEnumInput("Config.D"));

View File

@ -107,6 +107,8 @@ private MFromConfig fromConfig() {
List<MConfig> configs = new ArrayList<MConfig>();
MIntegerInput input = new MIntegerInput("INTEGER-INPUT", false, InputEditable.ANY, StringUtils.EMPTY);
input.setValue(100);
MLongInput lInput = new MLongInput("LONG-INPUT", false, InputEditable.ANY, StringUtils.EMPTY);
lInput.setValue(100L);
MStringInput strInput = new MStringInput("STRING-INPUT",false, InputEditable.ANY, StringUtils.EMPTY, (short)20);
strInput.setValue("TEST-VALUE");
List<MInput<?>> list = new ArrayList<MInput<?>>();

View File

@ -26,6 +26,7 @@
import org.apache.sqoop.driver.Driver;
import org.apache.sqoop.error.code.CommonRepositoryError;
import org.apache.sqoop.model.InputEditable;
import org.apache.sqoop.model.MLongInput;
import org.apache.sqoop.model.SubmissionError;
import org.apache.sqoop.model.MBooleanInput;
import org.apache.sqoop.model.MConfig;
@ -2010,6 +2011,9 @@ private void loadDriverConfigs(List<MConfig> driverConfig,
case INTEGER:
input = new MIntegerInput(inputName, inputSensitivity, editableEnum, overrides);
break;
case LONG:
input = new MLongInput(inputName, inputSensitivity, editableEnum, overrides);
break;
case ENUM:
input = new MEnumInput(inputName, inputSensitivity, editableEnum, overrides, inputEnumValues.split(","));
break;
@ -2158,6 +2162,9 @@ public void loadConnectorConfigs(List<MConfig> linkConfig, List<MConfig> fromCon
case INTEGER:
input = new MIntegerInput(inputName, inputSensitivity, editableEnum, overrides);
break;
case LONG:
input = new MLongInput(inputName, inputSensitivity, editableEnum, overrides);
break;
case ENUM:
input = new MEnumInput(inputName, inputSensitivity, editableEnum, overrides,
inputEnumValues.split(","));

View File

@ -40,6 +40,7 @@
import org.apache.sqoop.model.MIntegerInput;
import org.apache.sqoop.model.MJob;
import org.apache.sqoop.model.MLink;
import org.apache.sqoop.model.MLongInput;
import org.apache.sqoop.model.MMapInput;
import org.apache.sqoop.model.MStringInput;
import org.apache.sqoop.shell.core.Constants;
@ -186,6 +187,9 @@ private static void displayConfig(MConfig config, ResourceBundle bundle) {
case INTEGER:
displayInputInteger((MIntegerInput) input);
break;
case LONG:
displayLongInteger((MLongInput) input);
break;
case BOOLEAN:
displayInputBoolean((MBooleanInput) input);
break;
@ -223,6 +227,15 @@ private static void displayInputInteger(MIntegerInput input) {
print(input.getValue());
}
/**
* Display content of Long input.
*
* @param input Long input
*/
private static void displayLongInteger(MLongInput input) {
print(input.getValue());
}
/**
* Display content of Boolean input.
*

View File

@ -29,6 +29,7 @@
import org.apache.sqoop.model.MConfig;
import org.apache.sqoop.model.MInput;
import org.apache.sqoop.model.MIntegerInput;
import org.apache.sqoop.model.MLongInput;
import org.apache.sqoop.model.MMapInput;
import org.apache.sqoop.model.MJob;
import org.apache.sqoop.model.MNamedElement;
@ -182,6 +183,8 @@ public static boolean fillInput(String prefix, MInput input, CommandLine line) t
return fillInputString(prefix, (MStringInput) input, line);
case INTEGER:
return fillInputInteger(prefix, (MIntegerInput) input, line);
case LONG:
return fillInputLong(prefix, (MLongInput) input, line);
case BOOLEAN:
return fillInputBoolean(prefix, (MBooleanInput) input, line);
case MAP:
@ -289,6 +292,33 @@ private static boolean fillInputInteger(String prefix,
return true;
}
/**
* Load long input from CLI option.
*
* @param prefix
* placed at the beginning of the CLI option key
* @param input
* Input that we should read or edit
* @param line
* CLI options container
* @return
* @throws IOException
*/
private static boolean fillInputLong(String prefix, MLongInput input, CommandLine line) throws IOException {
String opt = ConfigOptions.getOptionKey(prefix, input);
if (line.hasOption(opt)) {
try {
input.setValue(Long.valueOf(line.getOptionValue(ConfigOptions.getOptionKey(prefix, input))));
} catch (NumberFormatException ex) {
errorMessage(input, "Input is not a valid long ");
return false;
}
} else {
input.setEmpty();
}
return true;
}
/**
* Load string input from CLI option.
*
@ -459,6 +489,8 @@ static boolean fillInputWithBundle(MInput input, ConsoleReader reader, ResourceB
return fillInputStringWithBundle((MStringInput) input, reader, bundle);
case INTEGER:
return fillInputInteger((MIntegerInput) input, reader, bundle);
case LONG:
return fillInputLong((MLongInput) input, reader, bundle);
case BOOLEAN:
return fillInputBooleanWithBundle((MBooleanInput) input, reader, bundle);
case MAP:
@ -708,6 +740,41 @@ private static boolean fillInputInteger(MIntegerInput input,
return true;
}
private static boolean fillInputLong(MLongInput input, ConsoleReader reader, ResourceBundle bundle) throws IOException {
generatePrompt(reader, bundle, input);
if (!input.isEmpty() && !input.isSensitive()) {
reader.putString(input.getValue().toString());
}
// Get the data
String userTyped;
if (input.isSensitive()) {
userTyped = reader.readLine('*');
} else {
userTyped = reader.readLine();
}
if (userTyped == null) {
return false;
} else if (userTyped.isEmpty()) {
input.setEmpty();
} else {
Long value;
try {
value = Long.valueOf(userTyped);
input.setValue(value);
} catch (NumberFormatException ex) {
errorMessage("Input is not a valid long");
return fillInputLong(input, reader, bundle);
}
input.setValue(Long.valueOf(userTyped));
}
return true;
}
/**
* Load string input from the user.
*