Search in sources :

Example 1 with DriverKeyspace

use of org.apache.hop.databases.cassandra.datastax.DriverKeyspace in project hop by apache.

the class DriverCqlRowHandlerTest method testNextOutputRowNoQuery.

@Test
public void testNextOutputRowNoQuery() throws Exception {
    DriverKeyspace keyspace = mock(DriverKeyspace.class);
    Session session = mock(Session.class);
    DriverCqlRowHandler rowHandler = new DriverCqlRowHandler(keyspace, session, true);
    IRowMeta rowMeta = new RowMeta();
    assertNull(rowHandler.getNextOutputRow(rowMeta, null));
}
Also used : DriverCqlRowHandler(org.apache.hop.databases.cassandra.datastax.DriverCqlRowHandler) DriverKeyspace(org.apache.hop.databases.cassandra.datastax.DriverKeyspace) IRowMeta(org.apache.hop.core.row.IRowMeta) RowMeta(org.apache.hop.core.row.RowMeta) IRowMeta(org.apache.hop.core.row.IRowMeta) Test(org.junit.Test)

Example 2 with DriverKeyspace

use of org.apache.hop.databases.cassandra.datastax.DriverKeyspace in project hop by apache.

the class DriverCqlRowHandlerTest method testQueryRows.

@Test
public void testQueryRows() throws Exception {
    List<Object[]> rowList = new ArrayList<>();
    rowList.add(new Object[] { 1L, "a", 0.2d });
    rowList.add(new Object[] { 2L, "b", 42d });
    DriverKeyspace keyspace = mock(DriverKeyspace.class);
    Session session = mock(Session.class);
    ResultSet rs = mock(ResultSet.class);
    mockColumnDefinitions(rs, DataType.cint(), DataType.text(), DataType.cdouble());
    when(session.execute(anyString())).thenReturn(rs);
    Iterator<Object[]> it = rowList.iterator();
    when(rs.isExhausted()).then(invoc -> {
        return !it.hasNext();
    });
    when(rs.one()).then(invocation -> {
        Object[] rowArr = it.next();
        Row row = mock(Row.class);
        when(row.getObject(anyInt())).then(invoc -> {
            return rowArr[(int) invoc.getArguments()[0]];
        });
        when(row.getLong(0)).thenReturn((long) rowArr[0]);
        when(row.getDouble(2)).thenReturn((double) rowArr[2]);
        return row;
    });
    DriverCqlRowHandler rowHandler = new DriverCqlRowHandler(keyspace, session, true);
    IRowMeta rowMeta = new RowMeta();
    rowMeta.addValueMeta(new ValueMetaInteger("a"));
    rowMeta.addValueMeta(new ValueMetaString("b"));
    rowMeta.addValueMeta(new ValueMetaNumber("c"));
    rowHandler.newRowQuery(mock(ITransform.class), "tab", "select * from tab", null, null, mock(ILogChannel.class));
    List<Object[]> resultRows = getNextOutputRows(rowHandler, rowMeta);
    assertEquals(2, resultRows.size());
    assertEquals(2L, resultRows.get(1)[0]);
}
Also used : DriverCqlRowHandler(org.apache.hop.databases.cassandra.datastax.DriverCqlRowHandler) ValueMetaString(org.apache.hop.core.row.value.ValueMetaString) DriverKeyspace(org.apache.hop.databases.cassandra.datastax.DriverKeyspace) ITransform(org.apache.hop.pipeline.transform.ITransform) IRowMeta(org.apache.hop.core.row.IRowMeta) RowMeta(org.apache.hop.core.row.RowMeta) ILogChannel(org.apache.hop.core.logging.ILogChannel) IRowMeta(org.apache.hop.core.row.IRowMeta) ArrayList(java.util.ArrayList) ValueMetaNumber(org.apache.hop.core.row.value.ValueMetaNumber) ValueMetaInteger(org.apache.hop.core.row.value.ValueMetaInteger) Test(org.junit.Test)

Example 3 with DriverKeyspace

use of org.apache.hop.databases.cassandra.datastax.DriverKeyspace in project hop by apache.

the class DriverCqlRowHandlerTest method testExpandCollection.

@Test
public void testExpandCollection() throws Exception {
    List<Object[]> rowList = new ArrayList<>();
    ArrayList<Long> numList = new ArrayList<>();
    numList.add(1L);
    numList.add(2L);
    numList.add(3L);
    rowList.add(new Object[] { 1L, numList });
    rowList.add(new Object[] { 2L, new ArrayList<Long>() });
    Iterator<Object[]> it = rowList.iterator();
    DriverKeyspace keyspace = mock(DriverKeyspace.class);
    Session session = mock(Session.class);
    ResultSet rs = mock(ResultSet.class);
    when(session.execute(anyString())).thenReturn(rs);
    when(rs.isExhausted()).then(invoc -> {
        return !it.hasNext();
    });
    when(rs.one()).then(invocation -> {
        Object[] rowArr = it.next();
        Row row = mock(Row.class);
        when(row.getObject(anyInt())).then(invoc -> {
            return rowArr[(int) invoc.getArguments()[0]];
        });
        when(row.getLong(0)).thenReturn((long) rowArr[0]);
        return row;
    });
    mockColumnDefinitions(rs, DataType.bigint(), DataType.list(DataType.bigint()));
    DriverCqlRowHandler rowHandler = new DriverCqlRowHandler(keyspace, session, true);
    IRowMeta rowMeta = new RowMeta();
    rowMeta.addValueMeta(new ValueMetaInteger("id"));
    rowMeta.addValueMeta(new ValueMetaNumber("nums"));
    rowHandler.newRowQuery(mock(ITransform.class), "tab", "select * from tab", null, null, mock(ILogChannel.class));
    List<Object[]> resultRows = getNextOutputRows(rowHandler, rowMeta);
    assertEquals(4, resultRows.size());
    assertEquals(1L, resultRows.get(0)[1]);
    assertEquals(2L, resultRows.get(3)[0]);
    assertNull(resultRows.get(3)[1]);
}
Also used : DriverCqlRowHandler(org.apache.hop.databases.cassandra.datastax.DriverCqlRowHandler) DriverKeyspace(org.apache.hop.databases.cassandra.datastax.DriverKeyspace) ITransform(org.apache.hop.pipeline.transform.ITransform) IRowMeta(org.apache.hop.core.row.IRowMeta) RowMeta(org.apache.hop.core.row.RowMeta) ILogChannel(org.apache.hop.core.logging.ILogChannel) IRowMeta(org.apache.hop.core.row.IRowMeta) ArrayList(java.util.ArrayList) ValueMetaNumber(org.apache.hop.core.row.value.ValueMetaNumber) ValueMetaInteger(org.apache.hop.core.row.value.ValueMetaInteger) Test(org.junit.Test)

Example 4 with DriverKeyspace

use of org.apache.hop.databases.cassandra.datastax.DriverKeyspace in project hop by apache.

the class DriverCqlRowHandlerTest method testBatchInsert.

@Test
public void testBatchInsert() throws Exception {
    DriverKeyspace keyspace = mock(DriverKeyspace.class);
    when(keyspace.getName()).thenReturn("ks");
    Session session = mock(Session.class);
    TableMetaData familyMeta = mock(TableMetaData.class);
    ArrayList<Object[]> batch = new ArrayList<>();
    batch.add(new Object[] { 1L, "a" });
    batch.add(new Object[] { 2L, "b" });
    RowMeta rowMeta = new RowMeta();
    rowMeta.addValueMeta(new ValueMetaInteger("id"));
    rowMeta.addValueMeta(new ValueMetaString("a spaced name"));
    when(familyMeta.getTableName()).thenReturn("tab tab");
    when(familyMeta.columnExistsInSchema(anyString())).thenReturn(true);
    DriverCqlRowHandler rowHandler = new DriverCqlRowHandler(keyspace, session, true);
    rowHandler.batchInsert(rowMeta, batch, familyMeta, null, true, null);
    verify(session, times(1)).execute(argThat(new ArgumentMatcher<Statement>() {

        @Override
        public boolean matches(Object argument) {
            Statement stmt = (Statement) argument;
            return stmt.toString().equals("BEGIN UNLOGGED BATCH INSERT INTO ks.\"tab tab\" (id,\"a spaced name\") " + "VALUES (1,'a');INSERT INTO ks.\"tab tab\" (id,\"a spaced name\") VALUES (2,'b');APPLY BATCH;");
        }
    }));
}
Also used : ValueMetaString(org.apache.hop.core.row.value.ValueMetaString) DriverCqlRowHandler(org.apache.hop.databases.cassandra.datastax.DriverCqlRowHandler) DriverKeyspace(org.apache.hop.databases.cassandra.datastax.DriverKeyspace) TableMetaData(org.apache.hop.databases.cassandra.datastax.TableMetaData) IRowMeta(org.apache.hop.core.row.IRowMeta) RowMeta(org.apache.hop.core.row.RowMeta) ArgumentMatcher(org.mockito.ArgumentMatcher) ArrayList(java.util.ArrayList) ValueMetaInteger(org.apache.hop.core.row.value.ValueMetaInteger) Test(org.junit.Test)

Example 5 with DriverKeyspace

use of org.apache.hop.databases.cassandra.datastax.DriverKeyspace in project hop by apache.

the class DriverCqlRowHandlerTest method testBatchInsertIgnoreColumns.

@Test
public void testBatchInsertIgnoreColumns() throws Exception {
    DriverKeyspace keyspace = mock(DriverKeyspace.class);
    when(keyspace.getName()).thenReturn("ks");
    Session session = mock(Session.class);
    TableMetaData familyMeta = mock(TableMetaData.class);
    when(familyMeta.getTableName()).thenReturn("tab");
    ArrayList<Object[]> batch = new ArrayList<>();
    batch.add(new Object[] { 1, 1L, 2L, 3L, 4L });
    batch.add(new Object[] { 2, 5L, 6L, 7L, 8L });
    RowMeta rowMeta = new RowMeta();
    rowMeta.addValueMeta(new ValueMetaInteger("nope"));
    rowMeta.addValueMeta(new ValueMetaInteger("there1"));
    rowMeta.addValueMeta(new ValueMetaInteger("not there"));
    rowMeta.addValueMeta(new ValueMetaInteger("there2"));
    rowMeta.addValueMeta(new ValueMetaInteger("also not there"));
    when(familyMeta.columnExistsInSchema(anyString())).then(args -> {
        return ((String) args.getArguments()[0]).startsWith("there");
    });
    DriverCqlRowHandler rowHandler = new DriverCqlRowHandler(keyspace, session, true);
    rowHandler.setUnloggedBatch(false);
    rowHandler.batchInsert(rowMeta, batch, familyMeta, "TWO", false, null);
    verify(session, times(1)).execute(argThat(new ArgumentMatcher<Statement>() {

        @Override
        public boolean matches(Object argument) {
            Statement stmt = (Statement) argument;
            return stmt.toString().equals("BEGIN BATCH INSERT INTO ks.tab (there1,there2) " + "VALUES (1,3);INSERT INTO ks.tab (there1,there2) VALUES (5,7);APPLY BATCH;") && stmt.getConsistencyLevel().equals(ConsistencyLevel.TWO);
        }
    }));
}
Also used : DriverCqlRowHandler(org.apache.hop.databases.cassandra.datastax.DriverCqlRowHandler) DriverKeyspace(org.apache.hop.databases.cassandra.datastax.DriverKeyspace) TableMetaData(org.apache.hop.databases.cassandra.datastax.TableMetaData) IRowMeta(org.apache.hop.core.row.IRowMeta) RowMeta(org.apache.hop.core.row.RowMeta) ArgumentMatcher(org.mockito.ArgumentMatcher) ArrayList(java.util.ArrayList) ValueMetaInteger(org.apache.hop.core.row.value.ValueMetaInteger) ValueMetaString(org.apache.hop.core.row.value.ValueMetaString) Test(org.junit.Test)

Aggregations

IRowMeta (org.apache.hop.core.row.IRowMeta)5 RowMeta (org.apache.hop.core.row.RowMeta)5 DriverCqlRowHandler (org.apache.hop.databases.cassandra.datastax.DriverCqlRowHandler)5 DriverKeyspace (org.apache.hop.databases.cassandra.datastax.DriverKeyspace)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 ValueMetaInteger (org.apache.hop.core.row.value.ValueMetaInteger)4 ValueMetaString (org.apache.hop.core.row.value.ValueMetaString)3 ILogChannel (org.apache.hop.core.logging.ILogChannel)2 ValueMetaNumber (org.apache.hop.core.row.value.ValueMetaNumber)2 TableMetaData (org.apache.hop.databases.cassandra.datastax.TableMetaData)2 ITransform (org.apache.hop.pipeline.transform.ITransform)2 ArgumentMatcher (org.mockito.ArgumentMatcher)2