Java Code Examples for org.apache.kudu.client.KuduScanner#hasMoreRows()

The following examples show how to use org.apache.kudu.client.KuduScanner#hasMoreRows() . 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: KuduOutput.java    From envelope with Apache License 2.0 6 votes vote down vote up
@Override
public Iterable<Row> getExistingForFilters(Iterable<Row> filters) throws Exception {
  List<Row> existingForFilters = Lists.newArrayList();

  if (!filters.iterator().hasNext()) {
    return existingForFilters;
  }

  KuduTable table = getConnection().getTable(config.getString(TABLE_CONFIG_NAME));
  KuduScanner scanner = scannerForFilters(filters, table);

  long startTime = System.nanoTime();
  while (scanner.hasMoreRows()) {
    for (RowResult rowResult : scanner.nextRows()) {
      Row existing = resultAsRow(rowResult, table);

      existingForFilters.add(existing);
    }
  }
  long endTime = System.nanoTime();
  if (hasAccumulators()) {
    accumulators.getDoubleAccumulators().get(ACCUMULATOR_SECONDS_SCANNING).add((endTime - startTime) / 1000.0 / 1000.0 / 1000.0);
  }

  return existingForFilters;
}
 
Example 2
Source File: KuduInputOperatorCommons.java    From attic-apex-malhar with Apache License 2.0 6 votes vote down vote up
public long countNumRowsInTable() throws Exception
{
  List<String> allProjectedCols = new ArrayList<>(
      unitTestStepwiseScanInputOperator.getKuduColNameToSchemaMapping().keySet());
  KuduScanner scanner = kuduClient.newScannerBuilder(kuduTable)
      .setProjectedColumnNames(allProjectedCols)
      .build();
  long counter = 0;
  while (scanner.hasMoreRows()) {
    RowResultIterator rowResultItr = scanner.nextRows();
    while (rowResultItr.hasNext()) {
      RowResult thisRow = rowResultItr.next();
      counter += 1;
    }
  }
  return counter;
}
 
Example 3
Source File: KuduOperations.java    From geowave with Apache License 2.0 6 votes vote down vote up
public List<Delete> getDeletions(
    final KuduTable table,
    final List<KuduPredicate> predicates,
    final Function<RowResult, PersistentKuduRow> adapter) throws KuduException {
  // TODO: Kudu Java API does not support deleting with predicates, so we first perform a scan and
  // then perform individual row deletions with the full primary key. This is inefficient, because
  // we need to read in entire rows in order to perform deletions.
  final KuduScannerBuilder scannerBuilder = getScannerBuilder(table);
  for (final KuduPredicate pred : predicates) {
    scannerBuilder.addPredicate(pred);
  }
  final KuduScanner scanner = scannerBuilder.build();
  final List<RowResultIterator> allResults = new ArrayList<>();
  while (scanner.hasMoreRows()) {
    allResults.add(scanner.nextRows());
  }
  final Iterator<Delete> deletions =
      Streams.stream(Iterators.concat(allResults.iterator())).map(result -> {
        final PersistentKuduRow row = adapter.apply(result);
        final Delete delete = table.newDelete();
        row.populatePartialRowPrimaryKey(delete.getRow());
        return delete;
      }).iterator();
  return Lists.newArrayList(deletions);
}
 
Example 4
Source File: KuduTemplate.java    From canal with Apache License 2.0 6 votes vote down vote up
/**
 * 统计kudu表数据
 *
 * @param tableName
 * @return
 */
public long countRow(String tableName) {
    this.checkClient();
    long rowCount = 0L;
    try {
        KuduTable kuduTable = kuduClient.openTable(tableName);
        // 创建scanner扫描
        KuduScanner scanner = kuduClient.newScannerBuilder(kuduTable).build();
        // 遍历数据
        while (scanner.hasMoreRows()) {
            while (scanner.nextRows().hasNext()) {
                rowCount++;
            }
        }
    } catch (KuduException e) {
        e.printStackTrace();
    }
    return rowCount;
}
 
Example 5
Source File: KuduInputOperatorCommons.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
public void truncateTable() throws Exception
{
  AbstractKuduPartitionScanner<UnitTestTablePojo,InputOperatorControlTuple> scannerForDeletingRows =
      unitTestStepwiseScanInputOperator.getScanner();
  List<KuduScanToken> scansForAllTablets = unitTestStepwiseScanInputOperator
      .getPartitioner().getKuduScanTokensForSelectAllColumns();
  ApexKuduConnection aCurrentConnection = scannerForDeletingRows.getConnectionPoolForThreads().get(0);
  KuduSession aSessionForDeletes = aCurrentConnection.getKuduClient().newSession();
  KuduTable currentTable = aCurrentConnection.getKuduTable();
  for ( KuduScanToken aTabletScanToken : scansForAllTablets) {
    KuduScanner aScanner = aTabletScanToken.intoScanner(aCurrentConnection.getKuduClient());
    while ( aScanner.hasMoreRows()) {
      RowResultIterator itrForRows = aScanner.nextRows();
      while ( itrForRows.hasNext()) {
        RowResult aRow = itrForRows.next();
        int intRowKey = aRow.getInt("introwkey");
        String stringRowKey = aRow.getString("stringrowkey");
        long timestampRowKey = aRow.getLong("timestamprowkey");
        Delete aDeleteOp = currentTable.newDelete();
        aDeleteOp.getRow().addInt("introwkey",intRowKey);
        aDeleteOp.getRow().addString("stringrowkey", stringRowKey);
        aDeleteOp.getRow().addLong("timestamprowkey",timestampRowKey);
        aSessionForDeletes.apply(aDeleteOp);
      }
    }
  }
  aSessionForDeletes.close();
  Thread.sleep(2000); // Sleep to allow for scans to complete
}
 
Example 6
Source File: KuduScanCustomizer.java    From syndesis with Apache License 2.0 4 votes vote down vote up
private static void processBody(Exchange exchange) throws KuduException, JsonProcessingException {
    final Message in = exchange.getIn();
    final KuduScanner scanner = in.getBody(KuduScanner.class);

    final List<String> answer = new ArrayList<>();
    while(scanner.hasMoreRows()) {
        RowResultIterator results = scanner.nextRows();

        while (results.hasNext()) {
            Map<String, Object> row = new HashMap<String, Object>();
            RowResult result = results.next();

            for (int i = 0; i < result.getSchema().getColumnCount(); i++) {
                String key = result.getSchema().getColumnByIndex(i).getName();
                Type type = result.getColumnType(i);

                switch (type.getName()) {
                    case "string":
                        row.put(key, result.getString(i));
                        break;
                    case "bool":
                        row.put(key, result.getBoolean(i));
                        break;
                    case "int8":
                    case "int16":
                    case "int32":
                        row.put(key, result.getInt(i));
                        break;
                    case "int64":
                        row.put(key, result.getLong(i));
                        break;
                    case "double":
                        row.put(key, result.getDouble(i));
                        break;
                    case "float":
                        row.put(key, result.getFloat(i));
                        break;
                    default:
                        throw new SyndesisServerException("The column schema type " + type.getName()
                                + " for column " + key
                                + " is not supported at the moment");
                }
            }
            answer.add(KuduSupport.toJSONBean(row));
        }
    }

    in.setBody(answer);
}
 
Example 7
Source File: KuduPartitionScannerCallable.java    From attic-apex-malhar with Apache License 2.0 4 votes vote down vote up
@Override
public Long call() throws Exception
{
  long numRowsScanned = 0;
  KuduScanner aPartitionSpecificScanner = KuduScanToken.deserializeIntoScanner(
      kuduPartitionScanAssignmentMeta.getSerializedKuduScanToken(), kuduClientHandle);
  LOG.info("Scanning the following tablet " + KuduScanToken.stringifySerializedToken(kuduPartitionScanAssignmentMeta
      .getSerializedKuduScanToken(), kuduClientHandle));
  KuduRecordWithMeta<T> beginScanRecord = new KuduRecordWithMeta<>();
  beginScanRecord.setBeginScanMarker(true);
  beginScanRecord.setTabletMetadata(kuduPartitionScanAssignmentMeta);
  bufferForTransmittingRecords.add(beginScanRecord); // Add a record entry that denotes the end of this scan.
  while ( aPartitionSpecificScanner.hasMoreRows()) {
    LOG.debug("Number of columns being returned for this read " +
        aPartitionSpecificScanner.getProjectionSchema().getColumnCount());
    RowResultIterator resultIterator = aPartitionSpecificScanner.nextRows();
    if (resultIterator == null) {
      break;
    } else {
      while (resultIterator.hasNext()) {
        KuduRecordWithMeta<T> recordWithMeta = new KuduRecordWithMeta<>();
        RowResult aRow = resultIterator.next();
        recordWithMeta.setPositionInScan(numRowsScanned);
        T payload = clazzForResultObject.newInstance();
        recordWithMeta.setThePayload(payload);
        recordWithMeta.setEndOfScanMarker(false);
        recordWithMeta.setTabletMetadata(kuduPartitionScanAssignmentMeta);
        setValuesInPOJO(aRow,payload);
        bufferForTransmittingRecords.add(recordWithMeta);
        numRowsScanned += 1;
      }
    }
  }
  aPartitionSpecificScanner.close();
  KuduRecordWithMeta<T> endScanRecord = new KuduRecordWithMeta<>();
  endScanRecord.setEndOfScanMarker(true);
  endScanRecord.setTabletMetadata(kuduPartitionScanAssignmentMeta);
  bufferForTransmittingRecords.add(endScanRecord); // Add a record entry that denotes the end of this scan.
  LOG.info(" Scanned a total of " + numRowsScanned + " for this scanner thread @tablet " +
      KuduScanToken.stringifySerializedToken(kuduPartitionScanAssignmentMeta.getSerializedKuduScanToken(),
      kuduClientHandle));
  return numRowsScanned;
}