Exemple #1
0
  /**
   * Read a record from the database. Each field/value pair from the result will be stored in a
   * HashMap.
   *
   * @param table The name of the table
   * @param key The record key of the record to read.
   * @param fields The list of fields to read, or null for all of them
   * @param result A HashMap of field/value pairs for the result
   * @return Zero on success, a non-zero error code on error
   */
  public Status read(
      String table, String key, Set<String> fields, HashMap<String, ByteIterator> result) {
    // if this is a "new" table, init HTable object. Else, use existing one
    if (!tableName.equals(table)) {
      currentTable = null;
      try {
        getHTable(table);
        tableName = table;
      } catch (IOException e) {
        System.err.println("Error accessing HBase table: " + e);
        return Status.ERROR;
      }
    }

    Result r = null;
    try {
      if (debug) {
        System.out.println("Doing read from HBase columnfamily " + columnFamily);
        System.out.println("Doing read for key: " + key);
      }
      Get g = new Get(Bytes.toBytes(key));
      if (fields == null) {
        g.addFamily(columnFamilyBytes);
      } else {
        for (String field : fields) {
          g.addColumn(columnFamilyBytes, Bytes.toBytes(field));
        }
      }
      r = currentTable.get(g);
    } catch (IOException e) {
      if (debug) {
        System.err.println("Error doing get: " + e);
      }
      return Status.ERROR;
    } catch (ConcurrentModificationException e) {
      // do nothing for now...need to understand HBase concurrency model better
      return Status.ERROR;
    }

    if (r.isEmpty()) {
      return Status.NOT_FOUND;
    }

    while (r.advance()) {
      final Cell c = r.current();
      result.put(
          Bytes.toString(CellUtil.cloneQualifier(c)),
          new ByteArrayByteIterator(CellUtil.cloneValue(c)));
      if (debug) {
        System.out.println(
            "Result for field: "
                + Bytes.toString(CellUtil.cloneQualifier(c))
                + " is: "
                + Bytes.toString(CellUtil.cloneValue(c)));
      }
    }
    return Status.OK;
  }
Exemple #2
0
  /**
   * Perform a range scan for a set of records in the database. Each field/value pair from the
   * result will be stored in a HashMap.
   *
   * @param table The name of the table
   * @param startkey The record key of the first record to read.
   * @param recordcount The number of records to read
   * @param fields The list of fields to read, or null for all of them
   * @param result A Vector of HashMaps, where each HashMap is a set field/value pairs for one
   *     record
   * @return Zero on success, a non-zero error code on error
   */
  @Override
  public Status scan(
      String table,
      String startkey,
      int recordcount,
      Set<String> fields,
      Vector<HashMap<String, ByteIterator>> result) {
    // if this is a "new" table, init HTable object. Else, use existing one
    if (!tableName.equals(table)) {
      currentTable = null;
      try {
        getHTable(table);
        tableName = table;
      } catch (IOException e) {
        System.err.println("Error accessing HBase table: " + e);
        return Status.ERROR;
      }
    }

    Scan s = new Scan(Bytes.toBytes(startkey));
    // HBase has no record limit. Here, assume recordcount is small enough to
    // bring back in one call.
    // We get back recordcount records
    s.setCaching(recordcount);
    if (this.usePageFilter) {
      s.setFilter(new PageFilter(recordcount));
    }

    // add specified fields or else all fields
    if (fields == null) {
      s.addFamily(columnFamilyBytes);
    } else {
      for (String field : fields) {
        s.addColumn(columnFamilyBytes, Bytes.toBytes(field));
      }
    }

    // get results
    ResultScanner scanner = null;
    try {
      scanner = currentTable.getScanner(s);
      int numResults = 0;
      for (Result rr = scanner.next(); rr != null; rr = scanner.next()) {
        // get row key
        String key = Bytes.toString(rr.getRow());

        if (debug) {
          System.out.println("Got scan result for key: " + key);
        }

        HashMap<String, ByteIterator> rowResult = new HashMap<String, ByteIterator>();

        while (rr.advance()) {
          final Cell cell = rr.current();
          rowResult.put(
              Bytes.toString(CellUtil.cloneQualifier(cell)),
              new ByteArrayByteIterator(CellUtil.cloneValue(cell)));
        }

        // add rowResult to result vector
        result.add(rowResult);
        numResults++;

        // PageFilter does not guarantee that the number of results is <=
        // pageSize, so this
        // break is required.
        if (numResults >= recordcount) { // if hit recordcount, bail out
          break;
        }
      } // done with row
    } catch (IOException e) {
      if (debug) {
        System.out.println("Error in getting/parsing scan result: " + e);
      }
      return Status.ERROR;
    } finally {
      if (scanner != null) {
        scanner.close();
      }
    }

    return Status.OK;
  }