Created
November 16, 2013 03:56
-
-
Save mrcsparker/7495739 to your computer and use it in GitHub Desktop.
Dump a Teradata database to MySQL. Also will work with SQLite with modifications.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedWriter; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.sql.*; | |
import java.text.DateFormat; | |
import java.text.SimpleDateFormat; | |
import java.util.Calendar; | |
import java.util.HashMap; | |
class TeradataDump { | |
private BufferedWriter _output; | |
private static String _host; | |
private static String _user; | |
private static String _pass; | |
private static String _catalog; | |
private static String _schema; | |
private static int _sampleSize; | |
private boolean _loadTableHeader; | |
private DatabaseMetaData _databaseMetaData; | |
private final HashMap<String, String> _indexes = new HashMap<String, String>(); | |
private Connection _conn; | |
private String url() { | |
return "jdbc:teradata://" + _host + "/TMODE=ANSI,CHARSET=UTF8"; | |
} | |
private String getValidColumnType(String columnType, long columnSize) { | |
if (columnType.equals("VARCHAR")) { | |
return "VARCHAR (" + columnSize + ")"; | |
} else if (columnType.equals("CHAR")) { | |
return "CHAR (" + columnSize + ")"; | |
} else { | |
return columnType; | |
} | |
} | |
private void connect() throws ClassNotFoundException, SQLException { | |
String url = url(); | |
Class.forName("com.teradata.jdbc.TeraDriver"); | |
_conn = DriverManager.getConnection(url, _user, _pass); | |
System.out.println("Connection established"); | |
} | |
private void loadTable(String table) throws IOException, SQLException { | |
_output.write("\n\n-- Table: " + table); | |
_output.write("\nCREATE TABLE " + table + "(\n"); | |
ResultSet rs = _databaseMetaData.getColumns(_catalog, _schema, table, "%"); | |
boolean firstLine = true; | |
while (rs.next()) { | |
if (firstLine) { | |
firstLine = false; | |
} else { | |
// If we're not the first line, then finish the previous line with a comma | |
_output.write(",\n"); | |
} | |
String columnName = rs.getString("COLUMN_NAME"); | |
String columnType = getValidColumnType(rs.getString("TYPE_NAME"), rs.getInt("COLUMN_SIZE")); | |
String nullable = rs.getString("IS_NULLABLE"); | |
String nullString = "NULL"; | |
if ("NO".equalsIgnoreCase(nullable)) { | |
nullString = "NOT NULL"; | |
} | |
// rs.getPrimaryKeys does not work with the Teradata version we have | |
// so we hack this to work. | |
String primaryKey = ""; | |
if (columnName.equals("id")) { | |
primaryKey = " PRIMARY KEY AUTO_INCREMENT"; | |
} | |
// Even bigger hack, which is why it is separate | |
if (columnName.equals("WEEK_ID") && table.equals("WEEK_CAL")) { | |
primaryKey = " PRIMARY KEY AUTO_INCREMENT"; | |
} | |
_output.write(" " + columnName + " " + columnType + " " + nullString + primaryKey); | |
} | |
rs.close(); | |
_output.write("\n);\n"); | |
// Now, create indexes | |
String tableIndexBase = "idx_" + _catalog + "_" + _schema + "_" + table + "_"; | |
rs = _databaseMetaData.getIndexInfo(_catalog, _schema, table, false, false); | |
while(rs.next()) { | |
String columnName = rs.getString("COLUMN_NAME"); | |
if (columnName == null || columnName.equals("id")) { | |
continue; | |
} | |
String indexName = tableIndexBase + columnName; | |
if (_indexes.containsKey(indexName)) { | |
continue; | |
} | |
_indexes.put(indexName, columnName); | |
_output.write("CREATE INDEX " + indexName + " ON " + table + " (" + columnName + ");\n"); | |
} | |
rs.close(); | |
} | |
private PreparedStatement buildSelect(String table) throws SQLException { | |
String fullName = _schema + "." + table; | |
if (_sampleSize == 0) { | |
return _conn.prepareStatement("SELECT * FROM " + fullName + ""); | |
} else { | |
return _conn.prepareStatement("SELECT * FROM " + fullName + " SAMPLE " + _sampleSize); | |
} | |
} | |
private void loadTableData(String table) throws SQLException, IOException { | |
PreparedStatement stmt = buildSelect(table); | |
ResultSet rs = stmt.executeQuery(); | |
ResultSetMetaData metaData = rs.getMetaData(); | |
int columnCount = metaData.getColumnCount(); | |
_output.write("\n\n-- Data for [" + table + "]\n"); | |
while (rs.next()) { | |
_output.write("INSERT INTO " + table + " VALUES ("); | |
for (int i = 0; i < columnCount; i++) { | |
if (i > 0) { | |
_output.write(", "); | |
} | |
Object value = rs.getObject(i + 1); | |
if (value == null) { | |
_output.write("NULL"); | |
} else { | |
String outputValue = value.toString(); | |
outputValue = outputValue.replaceAll("\'", "\'\'"); | |
_output.write("'" + outputValue + "'"); | |
} | |
} | |
_output.write(");\n"); | |
} | |
rs.close(); | |
stmt.close(); | |
} | |
private void createWriter() throws IOException { | |
_output = new BufferedWriter(new FileWriter(_catalog + "." + _schema + ".sql")); | |
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); | |
Calendar cal = Calendar.getInstance(); | |
_output.write("\n-- This file was generated by TeradataDump.java"); | |
_output.write("\n-- on " + dateFormat.format(cal.getTime())); | |
_output.newLine(); | |
} | |
private void closeWriter() throws IOException { | |
_output.newLine(); | |
_output.write("-- End"); | |
_output.close(); | |
} | |
public void run() { | |
try { | |
connect(); | |
createWriter(); | |
String[] types = {"TABLE"}; | |
_databaseMetaData = _conn.getMetaData(); | |
ResultSet rs = _databaseMetaData.getTables(_catalog, _schema, "%", types); | |
while (rs.next()) { | |
String table = rs.getString(3); | |
System.out.println("Name: " + table); | |
_output.write("\nSTART TRANSACTION;\n"); | |
if (_loadTableHeader) { | |
loadTable(table); | |
} | |
loadTableData(table); | |
_output.write("\nCOMMIT;\n"); | |
} | |
rs.close(); | |
closeWriter(); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} catch (ClassNotFoundException e) { | |
e.printStackTrace(); | |
} | |
} | |
public TeradataDump(String host, String user, String pass, String catalog, String schema, int sampleSize, boolean loadTableHeader) { | |
_host = host; | |
_user = user; | |
_pass = pass; | |
_catalog = catalog; | |
_schema = schema; | |
_sampleSize = sampleSize; | |
_loadTableHeader = loadTableHeader; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment