Search in sources :

Example 81 with ResultSet

use of com.datastax.driver.core.ResultSet in project apex-malhar by apache.

the class AbstractUpsertOutputOperatorCodecsTest method testForListPrepend.

@Test
public void testForListPrepend() throws Exception {
    User aUser = new User();
    String userId = "user" + System.currentTimeMillis();
    aUser.setUserid(userId);
    FullName fullName = new FullName("first1" + System.currentTimeMillis(), "last1" + System.currentTimeMillis());
    aUser.setUsername(fullName);
    List<Integer> topScores = new ArrayList<>();
    topScores.add(1);
    topScores.add(2);
    aUser.setTopScores(topScores);
    UpsertExecutionContext<User> originalEntry = new UpsertExecutionContext<>();
    originalEntry.setPayload(aUser);
    UpsertExecutionContext<User> subsequentUpdateForTopScores = new UpsertExecutionContext<>();
    subsequentUpdateForTopScores.setListPlacementStyle(UpsertExecutionContext.ListPlacementStyle.PREPEND_TO_EXISTING_LIST);
    subsequentUpdateForTopScores.setCollectionMutationStyle(UpsertExecutionContext.CollectionMutationStyle.ADD_TO_EXISTING_COLLECTION);
    subsequentUpdateForTopScores.setNullHandlingMutationStyle(UpsertExecutionContext.NullHandlingMutationStyle.IGNORE_NULL_COLUMNS);
    User oldUser = new User();
    oldUser.setUserid(userId);
    List<Integer> topScoresAppended = new ArrayList<>();
    topScoresAppended.add(3);
    oldUser.setTopScores(topScoresAppended);
    subsequentUpdateForTopScores.setPayload(oldUser);
    userUpsertOperator.beginWindow(2);
    userUpsertOperator.input.process(originalEntry);
    userUpsertOperator.input.process(subsequentUpdateForTopScores);
    userUpsertOperator.endWindow();
    ResultSet results = userUpsertOperator.session.execute("SELECT * FROM unittests.users WHERE userid = '" + userId + "'");
    List<Row> rows = results.all();
    Row userRow = rows.get(0);
    List<Integer> topScoresEntry = userRow.getList("top_scores", Integer.class);
    assertEquals(3, topScoresEntry.size());
    assertEquals("" + 3, "" + topScoresEntry.get(0));
}
Also used : ArrayList(java.util.ArrayList) ResultSet(com.datastax.driver.core.ResultSet) Row(com.datastax.driver.core.Row) Test(org.junit.Test)

Example 82 with ResultSet

use of com.datastax.driver.core.ResultSet in project apex-malhar by apache.

the class AbstractUpsertOutputOperatorCodecsTest method testForSingleRowInsertWithTTL.

@Test
public void testForSingleRowInsertWithTTL() throws Exception {
    User aUser = new User();
    aUser.setUserid("userWithTTL" + System.currentTimeMillis());
    FullName fullName = new FullName("firstname" + System.currentTimeMillis(), "lasName" + System.currentTimeMillis());
    aUser.setUsername(fullName);
    Address address = new Address("city1", "Street1", 12, null);
    aUser.setCurrentaddress(address);
    UpsertExecutionContext<User> anUpdate = new UpsertExecutionContext<>();
    anUpdate.setOverridingTTL(5000);
    anUpdate.setPayload(aUser);
    userUpsertOperator.beginWindow(7);
    userUpsertOperator.input.process(anUpdate);
    userUpsertOperator.endWindow();
    ResultSet results = userUpsertOperator.session.execute("SELECT * FROM unittests.users WHERE userid = '" + aUser.getUserid() + "'");
    List<Row> rows = results.all();
    assertEquals(rows.size(), 1);
    assertTrue(results.isExhausted());
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) Row(com.datastax.driver.core.Row) Test(org.junit.Test)

Example 83 with ResultSet

use of com.datastax.driver.core.ResultSet in project apex-malhar by apache.

the class AbstractUpsertOutputOperatorCodecsTest method testForListPrependAndExplicitNullForSomeColumns.

@Test
public void testForListPrependAndExplicitNullForSomeColumns() throws Exception {
    User aUser = new User();
    String userId = "user" + System.currentTimeMillis();
    aUser.setUserid(userId);
    FullName fullName = new FullName("first24" + System.currentTimeMillis(), "last" + System.currentTimeMillis());
    aUser.setUsername(fullName);
    List<Integer> topScores = new ArrayList<>();
    topScores.add(1);
    topScores.add(2);
    aUser.setTopScores(topScores);
    UpsertExecutionContext<User> originalEntry = new UpsertExecutionContext<>();
    originalEntry.setPayload(aUser);
    UpsertExecutionContext<User> subsequentUpdateForTopScores = new UpsertExecutionContext<>();
    subsequentUpdateForTopScores.setListPlacementStyle(UpsertExecutionContext.ListPlacementStyle.PREPEND_TO_EXISTING_LIST);
    subsequentUpdateForTopScores.setCollectionMutationStyle(UpsertExecutionContext.CollectionMutationStyle.ADD_TO_EXISTING_COLLECTION);
    subsequentUpdateForTopScores.setNullHandlingMutationStyle(UpsertExecutionContext.NullHandlingMutationStyle.SET_NULL_COLUMNS);
    User oldUser = new User();
    oldUser.setUserid(userId);
    List<Integer> topScoresAppended = new ArrayList<>();
    topScoresAppended.add(3);
    oldUser.setTopScores(topScoresAppended);
    subsequentUpdateForTopScores.setPayload(oldUser);
    userUpsertOperator.beginWindow(6);
    userUpsertOperator.input.process(originalEntry);
    userUpsertOperator.input.process(subsequentUpdateForTopScores);
    userUpsertOperator.endWindow();
    ResultSet results = userUpsertOperator.session.execute("SELECT * FROM unittests.users WHERE userid = '" + userId + "'");
    List<Row> rows = results.all();
    Row userRow = rows.get(0);
    FullName name = userRow.get("username", FullName.class);
    assertEquals(null, name);
}
Also used : ArrayList(java.util.ArrayList) ResultSet(com.datastax.driver.core.ResultSet) Row(com.datastax.driver.core.Row) Test(org.junit.Test)

Example 84 with ResultSet

use of com.datastax.driver.core.ResultSet in project apex-malhar by apache.

the class AbstractUpsertOutputOperatorCodecsTest method testForCollectionRemovalAndIfExists.

@Test
public void testForCollectionRemovalAndIfExists() throws Exception {
    User aUser = new User();
    String userId = "user" + System.currentTimeMillis();
    aUser.setUserid(userId);
    FullName fullName = new FullName("first12" + System.currentTimeMillis(), "last12" + System.currentTimeMillis());
    aUser.setUsername(fullName);
    Set<String> emails = new HashSet<>();
    emails.add(new String("1"));
    emails.add(new String("2"));
    aUser.setEmails(emails);
    UpsertExecutionContext<User> originalEntry = new UpsertExecutionContext<>();
    originalEntry.setPayload(aUser);
    UpsertExecutionContext<User> subsequentUpdateForEmails = new UpsertExecutionContext<>();
    subsequentUpdateForEmails.setCollectionMutationStyle(UpsertExecutionContext.CollectionMutationStyle.REMOVE_FROM_EXISTING_COLLECTION);
    subsequentUpdateForEmails.setNullHandlingMutationStyle(UpsertExecutionContext.NullHandlingMutationStyle.IGNORE_NULL_COLUMNS);
    subsequentUpdateForEmails.setUpdateOnlyIfPrimaryKeyExists(true);
    User oldUser = new User();
    // overriding with a non-existent user id
    oldUser.setUserid(userId + System.currentTimeMillis());
    Set<String> updatedEmails = new HashSet<>();
    updatedEmails.add(new String("1"));
    oldUser.setEmails(updatedEmails);
    subsequentUpdateForEmails.setPayload(oldUser);
    userUpsertOperator.beginWindow(4);
    userUpsertOperator.input.process(originalEntry);
    userUpsertOperator.input.process(subsequentUpdateForEmails);
    userUpsertOperator.endWindow();
    ResultSet results = userUpsertOperator.session.execute("SELECT * FROM unittests.users WHERE userid = '" + userId + "'");
    List<Row> rows = results.all();
    Row userRow = rows.get(0);
    Set<String> existingEmailsEntry = userRow.getSet("emails", String.class);
    assertEquals(2, existingEmailsEntry.size());
    assertEquals("" + 1, "" + existingEmailsEntry.iterator().next());
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) Row(com.datastax.driver.core.Row) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 85 with ResultSet

use of com.datastax.driver.core.ResultSet in project FlareBot by FlareBot.

the class FlareBot method migrations.

public void migrations() {
    CassandraController.runTask(session -> {
        session.execute("CREATE TABLE IF NOT EXISTS flarebot.announces (" + "guild_id varchar PRIMARY KEY," + "channel_id varchar)");
        ResultSet set = session.execute("SELECT * FROM flarebot.announces");
        Row row;
        while ((row = set.one()) != null) {
            FlareBotManager.instance().getGuild(row.getString(0)).setMusicAnnounceChannelId(row.getString(1));
        }
        session.execute("CREATE TABLE IF NOT EXISTS flarebot.prefixes (" + "guild_id varchar PRIMARY KEY, " + "prefix varchar" + ")");
        set = session.execute("SELECT * FROM flarebot.prefixes;");
        while ((row = set.one()) != null) {
            FlareBotManager.instance().getGuild(row.getString(0)).setPrefix(row.getString(1).charAt(0));
        }
    });
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) Row(com.datastax.driver.core.Row)

Aggregations

ResultSet (com.datastax.driver.core.ResultSet)95 Row (com.datastax.driver.core.Row)61 Test (org.junit.Test)43 ArrayList (java.util.ArrayList)17 BoundStatement (com.datastax.driver.core.BoundStatement)13 Session (com.datastax.driver.core.Session)12 Cluster (com.datastax.driver.core.Cluster)10 Statement (com.datastax.driver.core.Statement)8 List (java.util.List)8 PreparedStatement (com.datastax.driver.core.PreparedStatement)7 ByteBuffer (java.nio.ByteBuffer)5 StaticBuffer (org.janusgraph.diskstorage.StaticBuffer)5 BatchStatement (com.datastax.driver.core.BatchStatement)4 ResultSetFuture (com.datastax.driver.core.ResultSetFuture)4 Select (com.datastax.driver.core.querybuilder.Select)4 Tuple (io.vavr.Tuple)4 IOException (java.io.IOException)4 HashSet (java.util.HashSet)4 TypeHint (org.apache.flink.api.common.typeinfo.TypeHint)4 Entry (org.janusgraph.diskstorage.Entry)4