本文整理汇总了Java中com.github.shyiko.mysql.binlog.event.TableMapEventData.getTable方法的典型用法代码示例。如果您正苦于以下问题:Java TableMapEventData.getTable方法的具体用法?Java TableMapEventData.getTable怎么用?Java TableMapEventData.getTable使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.github.shyiko.mysql.binlog.event.TableMapEventData
的用法示例。
在下文中一共展示了TableMapEventData.getTable方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: handleDeleteRows
import com.github.shyiko.mysql.binlog.event.TableMapEventData; //导入方法依赖的package包/类
private void handleDeleteRows(DeleteRowsEventData data, List<DataChange> dataChanges) throws JsonProcessingException {
System.out.println(data);
TableMapEventData tableData = tableIdToDataMap.get(data.getTableId());
Pair<String, String> tableUniqueName = new ImmutablePair<String, String>(tableData.getDatabase(),
tableData.getTable());
TableMetadata tableMetadata = databaseNameToTableMetadata.get(tableUniqueName);
// XXX Error handling if tableMetadata == null
ArrayList<String> columnNames = tableMetadata.getColumnNames();
List<Integer> primaryKeyIndices = tableMetadata.getPrimaryKeyIndices();
List<Serializable[]> rows = data.getRows();
for (Serializable[] row: rows) {
Map<String, Serializable> primaryKeyMap = new HashMap<String, Serializable>();
for (int index : primaryKeyIndices) {
String columnName = columnNames.get(index);
Serializable value = row[index];
primaryKeyMap.put(columnName, value);
}
DataChange change = new DataChange();
change.setPayload(null);
change.setPrimaryKey(primaryKeyMap);
change.setTableName(tableUniqueName);
dataChanges.add(change);
}
}
示例2: handleWriteRows
import com.github.shyiko.mysql.binlog.event.TableMapEventData; //导入方法依赖的package包/类
private void handleWriteRows(WriteRowsEventData data, List<DataChange> dataChanges) throws JsonProcessingException {
TableMapEventData tableData = tableIdToDataMap.get(data.getTableId());
Pair<String, String> tableUniqueName = new ImmutablePair<String, String>(tableData.getDatabase(),
tableData.getTable());
TableMetadata tableMetadata = databaseNameToTableMetadata.get(tableUniqueName);
// XXX Error handling if tableMetadata == null
ArrayList<String> columnNames = tableMetadata.getColumnNames();
List<Integer> primaryKeyIndices = tableMetadata.getPrimaryKeyIndices();
List<Serializable[]> rows = data.getRows();
for (Serializable[] row: rows) {
Map<String,Serializable> map = new HashMap<String, Serializable>();
// System.out.println(row);
for (int i = 0; i < row.length; i++) {
String column = columnNames.get(i);
map.put(column, row[i]);
}
Map<String, Serializable> primaryKeyMap = new HashMap<String, Serializable>();
for (int index : primaryKeyIndices) {
String columnName = columnNames.get(index);
Serializable value = row[index];
primaryKeyMap.put(columnName, value);
}
DataChange change = new DataChange();
change.setPayload(map);
change.setPrimaryKey(primaryKeyMap);
change.setTableName(tableUniqueName);
dataChanges.add(change);
}
}
示例3: handleTableMap
import com.github.shyiko.mysql.binlog.event.TableMapEventData; //导入方法依赖的package包/类
private void handleTableMap(TableMapEventData data) throws SQLException {
Pair<String, String> tableUniqueName = new ImmutablePair<String, String>(data.getDatabase(), data.getTable());
if (databaseNameToTableIdMap.containsKey(tableUniqueName)) {
// delete the id->info mapping
System.out.println("Removing old db info!");
long oldTableId = databaseNameToTableIdMap.get(tableUniqueName);
tableIdToDataMap.remove(oldTableId);
}
tableIdToDataMap.put(data.getTableId(), data);
databaseNameToTableIdMap.put(tableUniqueName, data.getTableId());
if (databaseNameToTableMetadata.containsKey(tableUniqueName)) {
return;
}
// lookup the data
TableMetadata tableMetadata = new TableMetadata();
String dbName = data.getDatabase();
String tableName = data.getTable();
// XXX I should close this connection!
Connection connection;
connection = DriverManager.getConnection(
"jdbc:mysql://" + hostname + ":" + port + "/" + dbName, // notice the dbname is here. mysql's jdbc driver requires it.
username,
password);
DatabaseMetaData databaseMetaData = connection.getMetaData();
// get column names
ResultSet rs = databaseMetaData.getColumns(null, null, tableName, null);
// what about failures to connect to database? Need to make sure that we catch the case where we add an empty list, and then fail (meaning, no column names)
ArrayList<String> columnNames = new ArrayList<String>();
while (rs.next()) {
String columnName = rs.getString("COLUMN_NAME");
// 1 based, but we don't care. We just add them in order. This
// assumes that the columns come in ORDINAL_POSITION order, but the
// docs for getColumns() says they do.
// String position = rs.getString("ORDINAL_POSITION");
columnNames.add(columnName);
}
tableMetadata.setColumnNames(columnNames);
// get primary keys
ResultSet rs2 = databaseMetaData.getPrimaryKeys(null, null, tableName);
List<String> primaryKeyColumns = new ArrayList<String>();
while (rs2.next()) {
primaryKeyColumns.add(rs2.getString("COLUMN_NAME"));
}
if (primaryKeyColumns.isEmpty()) {
throw new RuntimeException(String.format("Table %s.%s does not have a primary key!", data.getDatabase(), data.getTable()));
}
tableMetadata.setPrimaryKeys(primaryKeyColumns);
System.out.println(tableMetadata);
// Only upon successful lookup, with no sql errors, do we save the data.
databaseNameToTableMetadata.put(tableUniqueName, tableMetadata);
}