Java Code Examples for com.datastax.driver.core.Row#get()

The following examples show how to use com.datastax.driver.core.Row#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CassandraKeyUtils.java    From DataflowTemplates with Apache License 2.0 6 votes vote down vote up
/**
 * This method returns a sorted map containing the columns that make up the (compound) primary key
 * in cassandra.
 *
 * <p>Each key in return value contains a name of a column in the cassandra key. The value for
 * each corresponding key in the map denotes the order in the Cassandra key.
 *
 * <p>Example, given the table:
 *
 * <p>create table mytable ( key_part_one int, key_part_two int, data boolean, PRIMARY
 * KEY(key_part_one, key_part_two) );
 *
 * <p>The method would return the following map:
 *
 * <p>"key_part_one", 0 "key_part_two", 1
 *
 * @param session the session to the Cassandra cluster.
 * @param keyspace they keyspace to query
 * @param table the table to query
 * @return see method description.
 */
static Map<String, Integer> primaryKeyOrder(Session session, String keyspace, String table) {
  HashMap<String, Integer> returnValue = new HashMap<>();
  Statement select = primarykeyCQL(keyspace, table);
  ResultSet rs = session.execute(select.toString());

  TreeMap<String, String> sortedMap = new TreeMap<>();
  for (Row row : rs) {
    String columnName = row.get(COLUMN_NAME_COLUMN, String.class);
    String kind = row.get(KIND_COLUMN, String.class);
    String position = row.get(POSITION_COLUMN, Integer.class).toString();
    if (kind.equals("clustering")) {
      sortedMap.put("clustering_" + position, columnName);
    } else {
      sortedMap.put(position, columnName);
    }
  }

  List<String> sortedKeyset = new ArrayList<>(sortedMap.keySet());
  for (int i = 0; i < sortedKeyset.size(); i++) {
    returnValue.put(sortedMap.get(sortedKeyset.get(i)), i);
  }

  return returnValue;
}
 
Example 2
Source File: CassandraLoader.java    From swblocks-decisiontree with Apache License 2.0 6 votes vote down vote up
private Set<ValueGroupChange> getValueGroupChange(final Row row) {
    final String type = row.getString("vgchangetype");
    if (type != null && Type.valueOf(type) != Type.NONE) {
        final ValueGroup group =
                new ValueGroup(row.getUUID("vgid"), row.getString("vgname"),
                        row.getList("vgdrivers", String.class),
                        new DateRange(row.get("vgstart", Instant.class), row.get("vgend", Instant.class)));

        final String driver = row.getString("vgdrivername");
        if (driver != null && !driver.isEmpty()) {
            group.setNewRuleData(driver, row.getList("vgrulecodes", UUID.class));
        }

        return Collections.singleton(new ValueGroupChange(Type.valueOf(type), group));
    }

    return Collections.emptySet();
}
 
Example 3
Source File: CassandraBaseTimeseriesDao.java    From iotplatform with Apache License 2.0 6 votes vote down vote up
public static KvEntry toKvEntry(Row row, String key) {
    KvEntry kvEntry = null;
    String strV = row.get(ModelConstants.STRING_VALUE_COLUMN, String.class);
    if (strV != null) {
        kvEntry = new StringDataEntry(key, strV);
    } else {
        Long longV = row.get(ModelConstants.LONG_VALUE_COLUMN, Long.class);
        if (longV != null) {
            kvEntry = new LongDataEntry(key, longV);
        } else {
            Double doubleV = row.get(ModelConstants.DOUBLE_VALUE_COLUMN, Double.class);
            if (doubleV != null) {
                kvEntry = new DoubleDataEntry(key, doubleV);
            } else {
                Boolean boolV = row.get(ModelConstants.BOOLEAN_VALUE_COLUMN, Boolean.class);
                if (boolV != null) {
                    kvEntry = new BooleanDataEntry(key, boolV);
                } else {
                    log.warn("All values in key-value row are nullable ");
                }
            }
        }
    }
    return kvEntry;
}
 
Example 4
Source File: AdaptiveResultSetTest.java    From emodb with Apache License 2.0 6 votes vote down vote up
private String verifyNextRow(ResultSet rs) {
    String rowid = null;
    for (int col=0; col < 137; col++) {
        Row row = rs.one();
        assertNotNull(row);

        if (rowid == null) {
            rowid = row.get("rowid", TypeCodec.varchar());
        } else {
            assertEquals(row.get("rowid", TypeCodec.varchar()), rowid);
        }

        assertEquals((long) row.get("col", TypeCodec.bigint()), col);
        assertEquals( row.get("data", TypeCodec.blob()).asIntBuffer().get(), col);
    }

    return rowid;
}
 
Example 5
Source File: CassandraLoader.java    From swblocks-decisiontree with Apache License 2.0 5 votes vote down vote up
public Result<ChangeSet> getChange(final String changeSetName) {
    try {
        final ResultSet resultSet = this.session.execute(CQL_GET_ACTIVE_CHANGE, changeSetName);
        EhSupport.ensure(!resultSet.isExhausted(), "ChangeSet %s does not exist in %s.", changeSetName,
                this.keyspace);
        UUID changeSetId = null;
        final Map<Change, List<Change>> changes = new HashMap<>();

        for (final Row row : resultSet) {
            changeSetId = row.getUUID("id");

            final UUID changeid = row.getUUID("changeid");
            final String rulesetname = row.getString("rulesetname");
            final Instant activationTime = row.get("activationtime", Instant.class);
            final DateRange changeRange = new DateRange(row.get("start", Instant.class),
                    row.get("end", Instant.class));
            final Change newchange = new Change(changeid, rulesetname, activationTime, changeRange,
                    getAuditForChange(row), getRuleChangeForChange(row), getValueGroupChange(row));

            final List<Change> internalChanges = changes.computeIfAbsent(newchange, value -> new ArrayList<>());
            internalChanges.add(newchange);
        }
        return Result.success(new ChangeSet(changeSetId, changeSetName, mergeChangesIntoSet(changes)));
    } catch (final Exception exception) {
        return Result.failure(() -> exception);
    }
}
 
Example 6
Source File: CassandraBaseAttributesDao.java    From iotplatform with Apache License 2.0 5 votes vote down vote up
private AttributeKvEntry convertResultToAttributesKvEntry(String key, Row row) {
    AttributeKvEntry attributeEntry = null;
    if (row != null) {
        long lastUpdateTs = row.get(LAST_UPDATE_TS_COLUMN, Long.class);
        attributeEntry = new BaseAttributeKvEntry(CassandraBaseTimeseriesDao.toKvEntry(row, key), lastUpdateTs);
    }
    return attributeEntry;
}
 
Example 7
Source File: AbstractUpsertOutputOperatorCodecsTest.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 8
Source File: AggregateDaoImpl.java    From glowroot with Apache License 2.0 5 votes vote down vote up
private static double getNextThreadStat(Row row, int columnIndex) {
    Double threadStat = row.get(columnIndex, Double.class);
    if (threadStat == null) {
        // old data stored prior to 0.10.9
        return NotAvailableAware.NA;
    } else {
        return threadStat;
    }
}
 
Example 9
Source File: CassandraMetadataReader.java    From geowave with Apache License 2.0 5 votes vote down vote up
private byte[] getVisibility(final Row result) {
  if (MetadataType.STATS.equals(metadataType)) {
    final ByteBuffer buf = result.get(CassandraMetadataWriter.VISIBILITY_KEY, ByteBuffer.class);
    if (buf != null) {
      return buf.array();
    }
  }
  return null;
}
 
Example 10
Source File: CassandraLoader.java    From swblocks-decisiontree with Apache License 2.0 4 votes vote down vote up
private Audit getAuditForChange(final Row row) {
    return new Audit(row.getString("initiator"),
            row.get("initiatortime", Instant.class), row.getString("approver"),
            row.get("approvertime", Instant.class));
}