Search in sources :

Example 11 with Row

use of com.airbnb.spinaltap.mysql.mutation.schema.Row 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;
}
Also used : ColumnMetadata(com.airbnb.spinaltap.mysql.mutation.schema.ColumnMetadata) MysqlInsertMutation(com.airbnb.spinaltap.mysql.mutation.MysqlInsertMutation) ArrayList(java.util.ArrayList) Row(com.airbnb.spinaltap.mysql.mutation.schema.Row)

Example 12 with Row

use of com.airbnb.spinaltap.mysql.mutation.schema.Row 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());
}
Also used : ColumnMetadata(com.airbnb.spinaltap.mysql.mutation.schema.ColumnMetadata) Table(com.airbnb.spinaltap.mysql.mutation.schema.Table) Column(com.airbnb.spinaltap.mysql.mutation.schema.Column) Row(com.airbnb.spinaltap.mysql.mutation.schema.Row) Test(org.junit.Test)

Example 13 with Row

use of com.airbnb.spinaltap.mysql.mutation.schema.Row 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;
}
Also used : MysqlDeleteMutation(com.airbnb.spinaltap.mysql.mutation.MysqlDeleteMutation) ColumnMetadata(com.airbnb.spinaltap.mysql.mutation.schema.ColumnMetadata) Serializable(java.io.Serializable) MysqlMutationMetadata(com.airbnb.spinaltap.mysql.mutation.MysqlMutationMetadata) MysqlUpdateMutation(com.airbnb.spinaltap.mysql.mutation.MysqlUpdateMutation) MysqlMutation(com.airbnb.spinaltap.mysql.mutation.MysqlMutation) MysqlInsertMutation(com.airbnb.spinaltap.mysql.mutation.MysqlInsertMutation) Row(com.airbnb.spinaltap.mysql.mutation.schema.Row)

Example 14 with Row

use of com.airbnb.spinaltap.mysql.mutation.schema.Row 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));
}
Also used : MysqlDeleteMutation(com.airbnb.spinaltap.mysql.mutation.MysqlDeleteMutation) ColumnMetadata(com.airbnb.spinaltap.mysql.mutation.schema.ColumnMetadata) MysqlMutationMetadata(com.airbnb.spinaltap.mysql.mutation.MysqlMutationMetadata) Table(com.airbnb.spinaltap.mysql.mutation.schema.Table) MysqlUpdateMutation(com.airbnb.spinaltap.mysql.mutation.MysqlUpdateMutation) DataSource(com.airbnb.spinaltap.mysql.DataSource) MysqlMutation(com.airbnb.spinaltap.mysql.mutation.MysqlMutation) Column(com.airbnb.spinaltap.mysql.mutation.schema.Column) MysqlInsertMutation(com.airbnb.spinaltap.mysql.mutation.MysqlInsertMutation) BinlogFilePos(com.airbnb.spinaltap.mysql.BinlogFilePos) MysqlUpdateMutation(com.airbnb.spinaltap.mysql.mutation.MysqlUpdateMutation) Mutation(com.airbnb.jitney.event.spinaltap.v1.Mutation) MysqlMutation(com.airbnb.spinaltap.mysql.mutation.MysqlMutation) MysqlDeleteMutation(com.airbnb.spinaltap.mysql.mutation.MysqlDeleteMutation) MysqlInsertMutation(com.airbnb.spinaltap.mysql.mutation.MysqlInsertMutation) Row(com.airbnb.spinaltap.mysql.mutation.schema.Row)

Example 15 with Row

use of com.airbnb.spinaltap.mysql.mutation.schema.Row in project SpinalTap by airbnb.

the class MysqlMutationMapperTest method testDeleteMutation.

@Test
public void testDeleteMutation() throws Exception {
    Serializable[] change = new Serializable[4];
    change[0] = 12131L;
    change[1] = "test_user";
    change[2] = 25;
    change[3] = 0;
    Serializable[] change2 = new Serializable[4];
    change2[0] = 12334L;
    change2[1] = "test_user2";
    change2[2] = 12;
    change2[3] = 1;
    BinlogEvent event = new DeleteEvent(TABLE_ID, SERVER_ID, TIMESTAMP, BINLOG_FILE_POS, ImmutableList.of(change, change2));
    List<? extends Mutation> mutations = eventMapper.map(event);
    assertEquals(2, mutations.size());
    assertTrue(mutations.get(0) instanceof MysqlDeleteMutation);
    MysqlDeleteMutation mutation = (MysqlDeleteMutation) mutations.get(0);
    validateMetadata(mutation, 0);
    Row row = mutation.getEntity();
    assertEquals(12131L, row.getColumns().get("id").getValue());
    assertEquals("test_user", row.getColumns().get("name").getValue());
    assertEquals(25, row.getColumns().get("age").getValue());
    assertEquals(0, row.getColumns().get("sex").getValue());
    assertTrue(mutations.get(1) instanceof MysqlDeleteMutation);
    mutation = (MysqlDeleteMutation) mutations.get(1);
    validateMetadata(mutation, 1);
    row = mutation.getEntity();
    assertEquals(12334L, row.getColumns().get("id").getValue());
    assertEquals("test_user2", row.getColumns().get("name").getValue());
    assertEquals(12, row.getColumns().get("age").getValue());
    assertEquals(1, row.getColumns().get("sex").getValue());
}
Also used : DeleteEvent(com.airbnb.spinaltap.mysql.event.DeleteEvent) MysqlDeleteMutation(com.airbnb.spinaltap.mysql.mutation.MysqlDeleteMutation) Serializable(java.io.Serializable) BinlogEvent(com.airbnb.spinaltap.mysql.event.BinlogEvent) Row(com.airbnb.spinaltap.mysql.mutation.schema.Row) Test(org.junit.Test)

Aggregations

Row (com.airbnb.spinaltap.mysql.mutation.schema.Row)22 Test (org.junit.Test)17 ColumnMetadata (com.airbnb.spinaltap.mysql.mutation.schema.ColumnMetadata)9 Column (com.airbnb.spinaltap.mysql.mutation.schema.Column)8 Table (com.airbnb.spinaltap.mysql.mutation.schema.Table)7 MysqlInsertMutation (com.airbnb.spinaltap.mysql.mutation.MysqlInsertMutation)6 Serializable (java.io.Serializable)6 BinlogEvent (com.airbnb.spinaltap.mysql.event.BinlogEvent)5 MysqlDeleteMutation (com.airbnb.spinaltap.mysql.mutation.MysqlDeleteMutation)5 MysqlUpdateMutation (com.airbnb.spinaltap.mysql.mutation.MysqlUpdateMutation)4 UpdateEvent (com.airbnb.spinaltap.mysql.event.UpdateEvent)3 MysqlMutation (com.airbnb.spinaltap.mysql.mutation.MysqlMutation)3 MysqlMutationMetadata (com.airbnb.spinaltap.mysql.mutation.MysqlMutationMetadata)3 AbstractMap (java.util.AbstractMap)3 Map (java.util.Map)3 ArrayList (java.util.ArrayList)2 Mutation (com.airbnb.jitney.event.spinaltap.v1.Mutation)1 SourceState (com.airbnb.spinaltap.common.source.SourceState)1 BinlogFilePos (com.airbnb.spinaltap.mysql.BinlogFilePos)1 DataSource (com.airbnb.spinaltap.mysql.DataSource)1