use of com.airbnb.spinaltap.mysql.mutation.schema.ColumnMetadata in project SpinalTap by airbnb.
the class InsertMutationMapper method mapEvent.
@Override
protected List<MysqlInsertMutation> mapEvent(Table table, WriteEvent event) {
List<Serializable[]> rows = event.getRows();
List<MysqlInsertMutation> mutations = new ArrayList<>();
Collection<ColumnMetadata> cols = table.getColumns().values();
for (int position = 0; position < rows.size(); position++) {
mutations.add(new MysqlInsertMutation(createMetadata(table, event, position), new Row(table, zip(rows.get(position), cols))));
}
return mutations;
}
use of com.airbnb.spinaltap.mysql.mutation.schema.ColumnMetadata in project SpinalTap by airbnb.
the class MysqlMutationMapper method zip.
protected static ImmutableMap<String, Column> zip(Serializable[] row, Collection<ColumnMetadata> columns) {
if (row.length != columns.size()) {
log.error("Row length {} and column length {} don't match", row.length, columns.size());
}
ImmutableMap.Builder<String, Column> builder = ImmutableMap.builder();
Iterator<ColumnMetadata> columnIterator = columns.iterator();
for (int position = 0; position < row.length && columnIterator.hasNext(); position++) {
ColumnMetadata col = columnIterator.next();
builder.put(col.getName(), new Column(col, row[position]));
}
return builder.build();
}
use of com.airbnb.spinaltap.mysql.mutation.schema.ColumnMetadata in project SpinalTap by airbnb.
the class RowTest method testNoPrimaryKey.
@Test
public void testNoPrimaryKey() throws Exception {
Table table = new Table(TABLE_ID, TABLE_NAME, DB_NAME, ImmutableList.of(new ColumnMetadata(ID_COLUMN, ColumnDataType.LONGLONG, false, 0)), ImmutableList.of());
Row row = new Row(table, ImmutableMap.of(ID_COLUMN, new Column(table.getColumns().get(ID_COLUMN), 1)));
assertNull(row.getPrimaryKeyValue());
}
use of com.airbnb.spinaltap.mysql.mutation.schema.ColumnMetadata in project SpinalTap by airbnb.
the class UpdateMutationMapper method mapEvent.
@Override
protected List<MysqlMutation> mapEvent(Table table, UpdateEvent event) {
List<MysqlMutation> mutations = Lists.newArrayList();
Collection<ColumnMetadata> cols = table.getColumns().values();
List<Map.Entry<Serializable[], Serializable[]>> rows = event.getRows();
for (int position = 0; position < rows.size(); position++) {
MysqlMutationMetadata metadata = createMetadata(table, event, position);
Row previousRow = new Row(table, zip(rows.get(position).getKey(), cols));
Row newRow = new Row(table, zip(rows.get(position).getValue(), cols));
// to retain invariant that a mutation captures changes to a single PK
if (table.getPrimaryKey().isPresent() && !previousRow.getPrimaryKeyValue().equals(newRow.getPrimaryKeyValue())) {
mutations.add(new MysqlDeleteMutation(metadata, previousRow));
mutations.add(new MysqlInsertMutation(metadata, newRow));
} else {
mutations.add(new MysqlUpdateMutation(metadata, previousRow, newRow));
}
}
;
return mutations;
}
use of com.airbnb.spinaltap.mysql.mutation.schema.ColumnMetadata in project SpinalTap by airbnb.
the class KafkaDestinationTest method createMutation.
private Mutation createMutation(MutationType type) {
Mapper<com.airbnb.spinaltap.Mutation<?>, ? extends TBase<?, ?>> thriftMutationMapper = ThriftMutationMapper.create("spinaltap");
Table table = new Table(0L, TABLE, DATABASE, ImmutableList.of(new ColumnMetadata("id", ColumnDataType.LONGLONG, true, 0)), ImmutableList.of("id"));
MysqlMutationMetadata metadata = new MysqlMutationMetadata(new DataSource(HOSTNAME, 0, "service"), new BinlogFilePos(), table, 0L, 0L, 0L, null, null, 0L, 0);
Row row = new Row(table, ImmutableMap.of("id", new Column(new ColumnMetadata("id", ColumnDataType.LONGLONG, true, 0), 1L)));
MysqlMutation mutation;
switch(type) {
case INSERT:
mutation = new MysqlInsertMutation(metadata, row);
break;
case UPDATE:
mutation = new MysqlUpdateMutation(metadata, row, row);
break;
case DELETE:
mutation = new MysqlDeleteMutation(metadata, row);
break;
default:
mutation = null;
}
return (Mutation) (thriftMutationMapper.map(mutation));
}
Aggregations