*/
public static void main(String [] args) {
if (args.length < 2) {
usageError();
}
PhoenixConnection conn = null;
try {
String tableName = null;
List<String> columns = null;
boolean isStrict = false;
List<String> delimiter = new ArrayList<String>();
int i = 0;
for (; i < args.length; i++) {
if (TABLE_OPTION.equals(args[i])) {
if (++i == args.length || tableName != null) {
usageError();
}
tableName = args[i];
} else if (HEADER_OPTION.equals(args[i])) {
if (++i >= args.length || columns != null) {
usageError();
}
String header = args[i];
if (HEADER_IN_LINE.equals(header)) {
columns = Collections.emptyList();
} else {
columns = Lists.newArrayList();
StringTokenizer tokenizer = new StringTokenizer(header,",");
while(tokenizer.hasMoreTokens()) {
columns.add(tokenizer.nextToken());
}
}
} else if (STRICT_OPTION.equals(args[i])) {
isStrict = true;
} else if (CSV_OPTION.equals(args[i])) {
for(int j=0; j < 3; j++) {
if(args[++i].length()==1){
delimiter.add(args[i]);
} else {
usageError();
}
}
} else {
break;
}
}
if (i == args.length) {
usageError();
}
Properties props = new Properties();
String connectionUrl = JDBC_PROTOCOL + JDBC_PROTOCOL_SEPARATOR + args[i++];
conn = DriverManager.getConnection(connectionUrl, props).unwrap(PhoenixConnection.class);
for (; i < args.length; i++) {
String fileName = args[i];
if (fileName.endsWith(SQL_FILE_EXT)) {
PhoenixRuntime.executeStatements(conn, new FileReader(args[i]), Collections.emptyList());
} else if (fileName.endsWith(CSV_FILE_EXT)) {
if (tableName == null) {
tableName = fileName.substring(fileName.lastIndexOf(File.separatorChar) + 1, fileName.length()-CSV_FILE_EXT.length());
}
CSVLoader csvLoader = new CSVLoader(conn, tableName, columns, isStrict, delimiter);
csvLoader.upsert(fileName);
} else {
usageError();
}
Long scn = conn.getSCN();
// If specifying SCN, increment it between processing files to allow
// for later files to see earlier files tables.
if (scn != null) {
scn++;
props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, scn.toString());
conn.close();
conn = DriverManager.getConnection(connectionUrl, props).unwrap(PhoenixConnection.class);
}
}
} catch (Throwable t) {
t.printStackTrace();
} finally {
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
//going to shut jvm down anyway. So might as well feast on it.
}
}
System.exit(0);