Skip to content

Commit

Permalink
PHOENIX-1523 Make it easy to provide a tab literal as separator for C…
Browse files Browse the repository at this point in the history
…SV imports(Sergey Soldatov)
  • Loading branch information
chrajeshbabu committed Mar 31, 2016
1 parent b988050 commit 9e767ff
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,38 @@ public void testBasicImport() throws Exception {
stmt.close();
}

@Test
public void testImportWithTabs() throws Exception {

Statement stmt = conn.createStatement();
stmt.execute("CREATE TABLE TABLE8 (ID INTEGER NOT NULL PRIMARY KEY, " +
"NAME1 VARCHAR, NAME2 VARCHAR)");

FileSystem fs = FileSystem.get(getUtility().getConfiguration());
FSDataOutputStream outputStream = fs.create(new Path("/tmp/input8.csv"));
PrintWriter printWriter = new PrintWriter(outputStream);
printWriter.println("1\tName 1a\tName 2a");
printWriter.println("2\tName 2a\tName 2b");
printWriter.close();

CsvBulkLoadTool csvBulkLoadTool = new CsvBulkLoadTool();
csvBulkLoadTool.setConf(getUtility().getConfiguration());
int exitCode = csvBulkLoadTool.run(new String[] {
"--input", "/tmp/input8.csv",
"--table", "table8",
"--zookeeper", zkQuorum,
"--delimiter", "\\t"});
assertEquals(0, exitCode);

ResultSet rs = stmt.executeQuery("SELECT id, name1, name2 FROM table8 ORDER BY id");
assertTrue(rs.next());
assertEquals(1, rs.getInt(1));
assertEquals("Name 1a", rs.getString(2));

rs.close();
stmt.close();
}

@Test
public void testFullOptionImport() throws Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.util.ToolRunner;
Expand Down Expand Up @@ -54,7 +55,7 @@ protected void configureOptions(CommandLine cmdLine, List<ColumnInfo> importColu

char delimiterChar = ',';
if (cmdLine.hasOption(DELIMITER_OPT.getOpt())) {
String delimString = cmdLine.getOptionValue(DELIMITER_OPT.getOpt());
String delimString = StringEscapeUtils.unescapeJava(cmdLine.getOptionValue(DELIMITER_OPT.getOpt()));
if (delimString.length() != 1) {
throw new IllegalArgumentException("Illegal delimiter character: " + delimString);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Mutation;
Expand Down Expand Up @@ -633,10 +634,11 @@ public static ExecutionCommand parseArgs(String[] args) {
}

private static char getCharacter(String s) {
if (s.length() > 1) {
throw new IllegalArgumentException("Invalid single character: '" + s + "'");
String unescaped = StringEscapeUtils.unescapeJava(s);
if (unescaped.length() > 1) {
throw new IllegalArgumentException("Invalid single character: '" + unescaped + "'");
}
return s.charAt(0);
return unescaped.charAt(0);
}

private static void usageError(String errorMsg, Options options) {
Expand Down

0 comments on commit 9e767ff

Please sign in to comment.