Java Code Examples for com.datastax.driver.core.ResultSet#iterator()

The following examples show how to use com.datastax.driver.core.ResultSet#iterator() . 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: CassandraQuery.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
@Override
public void runPostQuery(Object result, XMLStreamWriter xmlWriter, InternalParamCollection params, int queryLevel)
        throws DataServiceFault {
    ResultSet rs = (ResultSet) result;
    if (this.hasResult()) {
        Iterator<Row> itr = rs.iterator();
        Row row;
        DataEntry dataEntry;
        ColumnDefinitions defs = rs.getColumnDefinitions();
        while (itr.hasNext()) {
            row = itr.next();
            dataEntry = this.getDataEntryFromRow(row, defs);
            this.writeResultEntry(xmlWriter, dataEntry, params, queryLevel);
        }
    }
}
 
Example 2
Source File: CassandraPersistReader.java    From streams with Apache License 2.0 6 votes vote down vote up
@Override
public void prepare(Object configurationObject) {
  try {
    connectToCassandra();
    client.start();
  } catch (Exception e) {
    LOGGER.error("Exception", e);
    return;
  }

  String selectStatement = getSelectStatement();
  ResultSet rs = client.client().execute(selectStatement);
  rowIterator = rs.iterator();

  if (!rowIterator.hasNext()) {
    throw new RuntimeException("Table" + config.getTable() + "is empty!");
  }

  persistQueue = constructQueue();

  executor = Executors.newSingleThreadExecutor();
}
 
Example 3
Source File: CassandraEntryIterator.java    From hugegraph with Apache License 2.0 6 votes vote down vote up
public CassandraEntryIterator(ResultSet results, Query query,
       BiFunction<BackendEntry, Row, BackendEntry> merger) {
    super(query);
    this.results = results;
    this.rows = results.iterator();
    this.remaining = results.getAvailableWithoutFetching();
    this.merger = merger;
    this.next = null;

    this.skipOffset();

    if (query.paging()) {
        E.checkState(this.remaining == query.limit() ||
                     results.isFullyFetched(),
                     "Unexpected fetched page size: %s", this.remaining);
    }
}
 
Example 4
Source File: NamespaceOverrideMapper.java    From hawkular-metrics with Apache License 2.0 6 votes vote down vote up
private Set<String> getTenants() {
    Set<String> tenants = new HashSet<>();
    ResultSet resultset = session.execute("SELECT * FROM system_schema.keyspaces WHERE keyspace_name = 'hawkular_metrics';");
    if (!resultset.iterator().hasNext()) {
        return tenants;
    }

    try {
        // An invalid query exception will occur if the table does not exists.
        // If the table does not exist, then no tenants have been stored yet so we just return the empty tenant set.
        ResultSet resultSet = session.execute("SELECT DISTINCT tenant_id,type from hawkular_metrics.metrics_idx;");

        Iterator<Row> ri = resultSet.iterator();
        while (ri.hasNext()) {
            Row row = ri.next();
            String tenant = row.getString("tenant_id");
            if (!tenant.startsWith("_") && !tenant.contains(":")) {
                tenants.add(tenant);
            }
        }
    } catch (InvalidQueryException iqe) {
        log.warn(iqe);
    }
    return tenants;
}
 
Example 5
Source File: CassandraUtil.java    From sunbird-lms-service with MIT License 6 votes vote down vote up
/**
 * @desc This method is used for creating response from the resultset i.e return map
 *     <String,Object> or map<columnName,columnValue>
 * @param results ResultSet
 * @return Response Response
 */
public static Response createResponse(ResultSet results) {
  Response response = new Response();
  List<Map<String, Object>> responseList = new ArrayList<>();
  Map<String, String> columnsMapping = fetchColumnsMapping(results);
  Iterator<Row> rowIterator = results.iterator();
  rowIterator.forEachRemaining(
      row -> {
        Map<String, Object> rowMap = new HashMap<>();
        columnsMapping
            .entrySet()
            .stream()
            .forEach(entry -> rowMap.put(entry.getKey(), row.getObject(entry.getValue())));
        responseList.add(rowMap);
      });
  ProjectLogger.log(responseList.toString());
  response.put(Constants.RESPONSE, responseList);
  return response;
}
 
Example 6
Source File: CassandraCqlMapState.java    From storm-cassandra-cql with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public List<T> multiGet(List<List<Object>> keys) {
    try {
        List<T> values = new ArrayList<T>();

        for (List<Object> rowKey : keys) {
            Statement statement = mapper.retrieve(rowKey);
            ResultSet results = session.execute(statement);
            // TODO: Better way to check for empty results besides accessing entire results list
            Iterator<Row> rowIter = results.iterator();
            Row row;
            if (results != null && rowIter.hasNext() && (row = rowIter.next()) != null) {
                if (rowIter.hasNext()) {
                    LOG.error("Found non-unique value for key [{}]", rowKey);
                } else {
                    values.add((T) mapper.getValue(row));
                }
            } else {
                values.add(null);
            }
        }

        _mreads.incrBy(values.size());
        LOG.debug("Retrieving the following keys: {} with values: {}", keys, values);
        return values;
    } catch (Exception e) {
        checkCassandraException(e);
        throw new IllegalStateException("Impossible to reach this code");
    }
}
 
Example 7
Source File: CassandraPersistReader.java    From streams with Apache License 2.0 5 votes vote down vote up
@Override
public StreamsResultSet readAll() {
  ResultSet rs = client.client().execute(getSelectStatement());
  Iterator<Row> rowsIterator = rs.iterator();

  while (rowsIterator.hasNext()) {
    Row row = rowsIterator.next();
    StreamsDatum datum = prepareDatum(row);
    write(datum);
  }

  return readCurrent();
}
 
Example 8
Source File: CassandraEnumerator.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a CassandraEnumerator.
 *
 * @param results Cassandra result set ({@link com.datastax.driver.core.ResultSet})
 * @param protoRowType The type of resulting rows
 */
CassandraEnumerator(ResultSet results, RelProtoDataType protoRowType) {
  this.iterator = results.iterator();
  this.current = null;

  final RelDataTypeFactory typeFactory =
      new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
  this.fieldTypes = protoRowType.apply(typeFactory).getFieldList();
}
 
Example 9
Source File: CqlRecordReader.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public RowIterator()
{
    AbstractType type = partitioner.getTokenValidator();
    ResultSet rs = session.execute(cqlQuery, type.compose(type.fromString(split.getStartToken())), type.compose(type.fromString(split.getEndToken())) );
    for (ColumnMetadata meta : cluster.getMetadata().getKeyspace(quote(keyspace)).getTable(quote(cfName)).getPartitionKey())
        partitionBoundColumns.put(meta.getName(), Boolean.TRUE);
    rows = rs.iterator();
}
 
Example 10
Source File: CustomerEnrichedInfoCassandraRepo.java    From examples with Apache License 2.0 5 votes vote down vote up
protected void load() throws ClassNotFoundException, SQLException
{
  Cluster cluster = Cluster.builder().addContactPoint(dataWarehouseConfig.getHost()).build();
  Session session = cluster.connect(dataWarehouseConfig.getDatabase());

  List<SingleRecord> customerInfoList = Lists.newArrayList();

  try {
    ResultSet rs = session.execute("select id, imsi, isdn, imei, operatorName, operatorCode, deviceBrand, deviceModel from "
        + dataWarehouseConfig.getDatabase() + "." + dataWarehouseConfig.getTableName());

    Map<String, String> nameValueMap = new HashMap<String, String>();

    Iterator<Row> rowIter = rs.iterator();
    while (!rs.isFullyFetched() && rowIter.hasNext()) {
      Row row = rowIter.next();
      nameValueMap.put("id", row.getString(0));
      nameValueMap.put("imsi", row.getString(1));
      nameValueMap.put("isdn", row.getString(2));
      nameValueMap.put("imei", row.getString(3));
      nameValueMap.put("operatorName", row.getString(4));
      nameValueMap.put("operatorCode", row.getString(5));
      nameValueMap.put("deviceBrand", row.getString(6));
      nameValueMap.put("deviceModel", row.getString(7));

      SingleRecord record = new SingleRecord(nameValueMap);
      customerInfoList.add(record);
    }

  } catch (Exception e) {
    e.printStackTrace();
  } finally {
    if (session != null) {
      session.close();
    }
  }

  customerInfoArray = customerInfoList.toArray(new SingleRecord[0]);
}
 
Example 11
Source File: ShadowUserProcessor.java    From sunbird-lms-service with MIT License 5 votes vote down vote up
private FutureCallback<ResultSet> getSyncCallback() {
  return new FutureCallback<ResultSet>() {
    @Override
    public void onSuccess(ResultSet result) {
      Map<String, String> columnMap = CassandraUtil.fetchColumnsMapping(result);
      try {
        Iterator<Row> resultIterator = result.iterator();
        while (resultIterator.hasNext()) {
          Row row = resultIterator.next();
          Map<String, Object> doc = syncDataForEachRow(row, columnMap);
          ShadowUser singleShadowUser = mapper.convertValue(doc, ShadowUser.class);
          processSingleShadowUser(singleShadowUser);
          ProjectLogger.log(
              "ShadowUserProcessor:getSyncCallback:SUCCESS:SYNC CALLBACK SUCCESSFULLY PROCESSED for Shadow user: "
                  + singleShadowUser.toString(),
              LoggerEnum.INFO.name());
        }
        ProjectLogger.log(
            "ShadowUserProcessor:getSyncCallback:SUCCESS:SYNC CALLBACK SUCCESSFULLY MIGRATED  ALL Shadow user",
            LoggerEnum.INFO.name());

      } catch (Exception e) {
        ProjectLogger.log(
            "ShadowUserProcessor:getSyncCallback:SUCCESS:ERROR OCCURRED WHILE GETTING SYNC CALLBACKS"
                + e,
            LoggerEnum.ERROR.name());
      }
    }

    @Override
    public void onFailure(Throwable t) {
      ProjectLogger.log(
          "ShadowUserProcessor:getSyncCallback:FAILURE:ERROR OCCURRED WHILE GETTING SYNC CALLBACKS"
              + t,
          LoggerEnum.ERROR.name());
    }
  };
}
 
Example 12
Source File: TestResultSetConversionStrategy.java    From camel-kafka-connector with Apache License 2.0 5 votes vote down vote up
@Override
public Object getBody(ResultSet resultSet) {
    List<String> ret = new ArrayList<>();

    Iterator<Row> iterator = resultSet.iterator();
    while (iterator.hasNext()) {
        Row row = iterator.next();

        String data = row.getString("text");
        ret.add(data);
        LOG.info("Retrieved data: {}", data);
    }

    return ret;
}
 
Example 13
Source File: TestDataDao.java    From camel-kafka-connector with Apache License 2.0 5 votes vote down vote up
public void getData(Consumer<String> consumer) {
    ResultSet rs = session.execute("select * from test_data");

    if (rs != null) {
        Iterator<Row> iterator = rs.iterator();
        while (iterator.hasNext()) {
            Row row = iterator.next();
            String data = row.getString("text");
            LOG.info("Retrieved data: {}", data);
            consumer.accept(data);
        }
    } else {
        LOG.warn("No records were returned");
    }
}
 
Example 14
Source File: CassandraDataHandler.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
@Override
public List<ODataEntry> readTableWithKeys(String tableName, ODataEntry keys) throws ODataServiceFault {
    List<ColumnMetadata> cassandraTableMetaData = this.session.getCluster().getMetadata().getKeyspace(this.keyspace)
                                                              .getTable(tableName).getColumns();
    List<String> pKeys = this.primaryKeys.get(tableName);
    String query = createReadSqlWithKeys(tableName, keys);
    List<Object> values = new ArrayList<>();
    for (String column : this.tableMetaData.get(tableName).keySet()) {
        if (keys.getNames().contains(column) && pKeys.contains(column)) {
            bindParams(column, keys.getValue(column), values, cassandraTableMetaData);
        }
    }
    PreparedStatement statement = this.preparedStatementMap.get(query);
    if (statement == null) {
        statement = this.session.prepare(query);
        this.preparedStatementMap.put(query, statement);
    }
    ResultSet resultSet = this.session.execute(statement.bind(values.toArray()));
    List<ODataEntry> entryList = new ArrayList<>();
    Iterator<Row> iterator = resultSet.iterator();
    ColumnDefinitions definitions = resultSet.getColumnDefinitions();
    while (iterator.hasNext()) {
        ODataEntry dataEntry = createDataEntryFromRow(tableName, iterator.next(), definitions);
        entryList.add(dataEntry);
    }
    return entryList;
}
 
Example 15
Source File: CassandraDataHandler.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
@Override
public List<ODataEntry> readTable(String tableName) throws ODataServiceFault {
    Statement statement = new SimpleStatement("Select * from " + this.keyspace + "." + tableName);
    ResultSet resultSet = this.session.execute(statement);
    Iterator<Row> iterator = resultSet.iterator();
    List<ODataEntry> entryList = new ArrayList<>();
    ColumnDefinitions columnDefinitions = resultSet.getColumnDefinitions();
    while (iterator.hasNext()) {
        ODataEntry dataEntry = createDataEntryFromRow(tableName, iterator.next(), columnDefinitions);
        entryList.add(dataEntry);
    }
    return entryList;
}
 
Example 16
Source File: CassandraDbProvider.java    From ats-framework with Apache License 2.0 4 votes vote down vote up
@Override
public DbRecordValuesList[] select(
                                    DbQuery dbQuery,
                                    DbReturnModes dbReturnMode ) throws DbException {

    connect();

    ArrayList<DbRecordValuesList> dbRecords = new ArrayList<DbRecordValuesList>();

    String sqlQuery = dbQuery.getQuery();
    if (allowFiltering) {
        sqlQuery += " ALLOW FILTERING";
    }

    if (log.isDebugEnabled()) {
        log.debug(sqlQuery);
    }

    ResultSet results = session.execute(sqlQuery);

    int currentRow = 0;
    Iterator<Row> it = results.iterator();
    while (it.hasNext()) {
        Row row = it.next();

        currentRow++;
        if (log.isDebugEnabled()) {
            log.debug("Result row number: " + currentRow);
        }

        DbRecordValuesList recordList = new DbRecordValuesList();

        for (Definition columnDefinition : row.getColumnDefinitions()) {
            DbColumn dbColumn = new DbColumn(columnDefinition.getTable(), columnDefinition.getName());
            dbColumn.setColumnType(columnDefinition.getType().getName().toString());

            Object value = extractObjectFromResultSet(row, columnDefinition);

            DbRecordValue recordValue = new DbRecordValue(dbColumn, value);
            recordList.add(recordValue);
        }
        dbRecords.add(recordList);
    }

    return dbRecords.toArray(new DbRecordValuesList[]{});
}
 
Example 17
Source File: CassandraTables.java    From hugegraph with Apache License 2.0 4 votes vote down vote up
protected void deleteEdgesByLabel(CassandraSessionPool.Session session,
                                  Id label) {
    // Edges in edges_in table will be deleted when direction is OUT
    if (this.direction == Directions.IN) {
        return;
    }

    final String OWNER_VERTEX = formatKey(HugeKeys.OWNER_VERTEX);
    final String SORT_VALUES = formatKey(HugeKeys.SORT_VALUES);
    final String OTHER_VERTEX = formatKey(HugeKeys.OTHER_VERTEX);

    // Query edges by label index
    Select select = QueryBuilder.select().from(this.labelIndexTable());
    select.where(formatEQ(HugeKeys.LABEL, label.asLong()));

    ResultSet rs;
    try {
        rs = session.execute(select);
    } catch (DriverException e) {
        throw new BackendException("Failed to query edges " +
                  "with label '%s' for deleting", e, label);
    }

    // Delete edges
    long count = 0L;
    for (Iterator<Row> it = rs.iterator(); it.hasNext();) {
        Row row = it.next();
        Object ownerVertex = row.getObject(OWNER_VERTEX);
        Object sortValues = row.getObject(SORT_VALUES);
        Object otherVertex = row.getObject(OTHER_VERTEX);

        // Delete OUT edges from edges_out table
        session.add(buildDelete(label, ownerVertex, Directions.OUT,
                                sortValues, otherVertex));
        // Delete IN edges from edges_in table
        session.add(buildDelete(label, otherVertex, Directions.IN,
                                sortValues, ownerVertex));

        count += 2L;
        if (count >= COMMIT_DELETE_BATCH) {
            session.commit();
            count = 0;
        }
    }
    if (count > 0L) {
        session.commit();
    }
}
 
Example 18
Source File: ScannerIterator.java    From scalardb with Apache License 2.0 4 votes vote down vote up
public ScannerIterator(ResultSet resultSet, TableMetadata metadata) {
  iterator = resultSet.iterator();
  this.metadata = metadata;
}