Search in sources :

Example 1 with JdbcEntryData

use of org.apache.gobblin.converter.jdbc.JdbcEntryData in project incubator-gobblin by apache.

the class JdbcBufferedInserterTestBase method createJdbcEntry.

private JdbcEntryData createJdbcEntry(Collection<String> colNames, int colSize) {
    List<JdbcEntryDatum> datumList = new ArrayList<>();
    for (String colName : colNames) {
        JdbcEntryDatum datum = new JdbcEntryDatum(colName, RandomStringUtils.randomAlphabetic(colSize));
        datumList.add(datum);
    }
    return new JdbcEntryData(datumList);
}
Also used : ArrayList(java.util.ArrayList) JdbcEntryDatum(org.apache.gobblin.converter.jdbc.JdbcEntryDatum) JdbcEntryData(org.apache.gobblin.converter.jdbc.JdbcEntryData)

Example 2 with JdbcEntryData

use of org.apache.gobblin.converter.jdbc.JdbcEntryData in project incubator-gobblin by apache.

the class MySqlBufferedInserterTest method testMySqlBufferedInsert.

public void testMySqlBufferedInsert() throws SQLException {
    final int colNums = 20;
    final int batchSize = 10;
    final int entryCount = 107;
    final int colSize = 7;
    State state = new State();
    state.setProp(WRITER_JDBC_INSERT_BATCH_SIZE, Integer.toString(batchSize));
    JdbcBufferedInserter inserter = getJdbcBufferedInserter(state, conn);
    PreparedStatement pstmt = mock(PreparedStatement.class);
    when(conn.prepareStatement(anyString())).thenReturn(pstmt);
    List<JdbcEntryData> jdbcEntries = createJdbcEntries(colNums, colSize, entryCount);
    for (JdbcEntryData entry : jdbcEntries) {
        inserter.insert(db, table, entry);
    }
    inserter.flush();
    verify(conn, times(2)).prepareStatement(anyString());
    verify(pstmt, times(11)).clearParameters();
    verify(pstmt, times(11)).execute();
    verify(pstmt, times(colNums * entryCount)).setObject(anyInt(), anyObject());
    reset(pstmt);
}
Also used : State(org.apache.gobblin.configuration.State) PreparedStatement(java.sql.PreparedStatement) JdbcBufferedInserter(org.apache.gobblin.writer.commands.JdbcBufferedInserter) JdbcEntryData(org.apache.gobblin.converter.jdbc.JdbcEntryData)

Example 3 with JdbcEntryData

use of org.apache.gobblin.converter.jdbc.JdbcEntryData in project incubator-gobblin by apache.

the class MySqlBufferedInserterTest method testMySqlBufferedInsertParamLimit.

public void testMySqlBufferedInsertParamLimit() throws SQLException {
    final int colNums = 50;
    final int batchSize = 10;
    final int entryCount = 107;
    final int colSize = 3;
    final int maxParamSize = 500;
    State state = new State();
    state.setProp(WRITER_JDBC_INSERT_BATCH_SIZE, Integer.toString(batchSize));
    state.setProp(WRITER_JDBC_MAX_PARAM_SIZE, maxParamSize);
    MySqlBufferedInserter inserter = new MySqlBufferedInserter(state, conn);
    PreparedStatement pstmt = mock(PreparedStatement.class);
    when(conn.prepareStatement(anyString())).thenReturn(pstmt);
    List<JdbcEntryData> jdbcEntries = createJdbcEntries(colNums, colSize, entryCount);
    for (JdbcEntryData entry : jdbcEntries) {
        inserter.insert(db, table, entry);
    }
    inserter.flush();
    int expectedBatchSize = maxParamSize / colNums;
    int expectedExecuteCount = entryCount / expectedBatchSize + 1;
    verify(conn, times(2)).prepareStatement(anyString());
    verify(pstmt, times(expectedExecuteCount)).clearParameters();
    verify(pstmt, times(expectedExecuteCount)).execute();
    verify(pstmt, times(colNums * entryCount)).setObject(anyInt(), anyObject());
    reset(pstmt);
}
Also used : State(org.apache.gobblin.configuration.State) PreparedStatement(java.sql.PreparedStatement) MySqlBufferedInserter(org.apache.gobblin.writer.commands.MySqlBufferedInserter) JdbcEntryData(org.apache.gobblin.converter.jdbc.JdbcEntryData)

Example 4 with JdbcEntryData

use of org.apache.gobblin.converter.jdbc.JdbcEntryData in project incubator-gobblin by apache.

the class TeradataBufferedInserterTest method testTeradataBufferedInsert.

public void testTeradataBufferedInsert() throws SQLException {
    final int colNums = 20;
    final int batchSize = 10;
    final int entryCount = 107;
    final int colSize = 7;
    State state = new State();
    state.setProp(WRITER_JDBC_INSERT_BATCH_SIZE, Integer.toString(batchSize));
    JdbcBufferedInserter inserter = getJdbcBufferedInserter(state, conn);
    MockParameterMetaData mockMetadata = new MockParameterMetaData();
    mockMetadata.setParameterCount(2);
    mockMetadata.setParameterType(0, 12);
    mockMetadata.setParameterType(1, -5);
    PreparedStatement pstmt = Mockito.mock(PreparedStatement.class);
    when(pstmt.getParameterMetaData()).thenReturn(mockMetadata);
    when(pstmt.executeBatch()).thenReturn(new int[] { 1, 1, 1 });
    when(conn.prepareStatement(anyString())).thenReturn(pstmt);
    List<JdbcEntryData> jdbcEntries = createJdbcEntries(colNums, colSize, entryCount);
    for (JdbcEntryData entry : jdbcEntries) {
        inserter.insert(db, table, entry);
    }
    inserter.flush();
    verify(conn, times(2)).prepareStatement(anyString());
    verify(pstmt, times(107)).addBatch();
    verify(pstmt, times((int) Math.ceil((double) entryCount / batchSize))).executeBatch();
    verify(pstmt, times(entryCount)).clearParameters();
    verify(pstmt, times(colNums * entryCount)).setObject(anyInt(), anyObject());
    reset(pstmt);
}
Also used : State(org.apache.gobblin.configuration.State) PreparedStatement(java.sql.PreparedStatement) JdbcBufferedInserter(org.apache.gobblin.writer.commands.JdbcBufferedInserter) JdbcEntryData(org.apache.gobblin.converter.jdbc.JdbcEntryData) MockParameterMetaData(com.mockrunner.mock.jdbc.MockParameterMetaData)

Example 5 with JdbcEntryData

use of org.apache.gobblin.converter.jdbc.JdbcEntryData in project incubator-gobblin by apache.

the class MySqlBufferedInserter method insertBatch.

@Override
protected boolean insertBatch(PreparedStatement pstmt) throws SQLException {
    int i = 0;
    pstmt.clearParameters();
    for (JdbcEntryData pendingEntry : MySqlBufferedInserter.this.pendingInserts) {
        for (JdbcEntryDatum datum : pendingEntry) {
            pstmt.setObject(++i, datum.getVal());
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Executing SQL " + pstmt);
    }
    return pstmt.execute();
}
Also used : JdbcEntryDatum(org.apache.gobblin.converter.jdbc.JdbcEntryDatum) JdbcEntryData(org.apache.gobblin.converter.jdbc.JdbcEntryData)

Aggregations

JdbcEntryData (org.apache.gobblin.converter.jdbc.JdbcEntryData)6 PreparedStatement (java.sql.PreparedStatement)3 State (org.apache.gobblin.configuration.State)3 JdbcEntryDatum (org.apache.gobblin.converter.jdbc.JdbcEntryDatum)3 JdbcBufferedInserter (org.apache.gobblin.writer.commands.JdbcBufferedInserter)2 MockParameterMetaData (com.mockrunner.mock.jdbc.MockParameterMetaData)1 BatchUpdateException (java.sql.BatchUpdateException)1 ArrayList (java.util.ArrayList)1 MySqlBufferedInserter (org.apache.gobblin.writer.commands.MySqlBufferedInserter)1