예제 #1
0
  public int insertEntity(
      String entitySet,
      String entityPK,
      HashMap<String, ByteIterator> values,
      boolean insertImage) {
    // TODO Auto-generated method stub
    /* Insert into table */
    try {
      long mutator = client.mutator_open(namespace, entitySet, 0, 0);
      List<Cell> cells = new ArrayList<Cell>();
      Key key = null;
      Cell cell = null;

      for (HashMap.Entry<String, ByteIterator> entry : values.entrySet()) {
        key = new Key(entityPK, entry.getKey(), null, KeyFlag.INSERT);
        cell = new Cell(key);
        cell.setValue(entry.getValue().toArray());
        cells.add(cell);
      }

      client.mutator_set_cells(mutator, cells);
      client.mutator_flush(mutator);
      cells.clear();
      client.mutator_close(mutator);
    } catch (ClientException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      return -1;
    } catch (TException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      return -1;
    }
    return 0;
  }
예제 #2
0
  public int CreateFriendship(int friendid1, int friendid2) {
    // TODO Auto-generated method stub
    long mutator;
    try {
      mutator = client.mutator_open(namespace, ConfirmedFriendsTable, 0, 0);

      List<Cell> cells = new ArrayList<Cell>();
      Key key = null;
      Cell cell = null;

      key = new Key(friendid1 + "", ConfirmedFriendsTableCF[1], null, KeyFlag.INSERT);
      cell = new Cell(key);
      cell.setValue((friendid2 + "").getBytes("UTF-8"));
      cells.add(cell);

      key = new Key(friendid2 + "", ConfirmedFriendsTableCF[1], null, KeyFlag.INSERT);
      cell = new Cell(key);
      cell.setValue((friendid1 + "").getBytes("UTF-8"));
      cells.add(cell);

      client.mutator_set_cells(mutator, cells);
      client.mutator_flush(mutator);
      cells.clear();

    } catch (TException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      return -1;
    } catch (UnsupportedEncodingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      return -1;
    }
    return 0;
  }
  /**
   * Method to execute a Delete mutation operation on Hypertable. A delete is implemented by
   * Hypertable as a put that inserts tombstone values.
   *
   * @param tableName
   * @param delete
   * @throws ClientException
   * @throws TException
   */
  public void delete(String tableName, Delete delete) throws ClientException, TException {
    long mutator = client.mutator_open(ns, tableName, 0, 0);

    for (int i = 0; i < delete._getCells().size(); i++) {
      Cell c = delete._getCells().get(i);
      if (c.getKey().isSetRevision()) {
        int revs = (int) c.getKey().getRevision();
        System.out.println(revs);
        String cfam = c.getKey().getColumn_family();
        String cqual = c.getKey().getColumn_qualifier();
        c.getKey().setRevisionIsSet(false);
        List<Result> res =
            this.scan(
                tableName, new Scan(delete.getRowkey()).addColumn(cfam, cqual).setRevisions(revs));
        if (!res.isEmpty()) {
          System.out.println(res.get(0).getValueRevisions(cfam, cqual));
          for (Long ts : res.get(0).getTimestampRevisions(cfam, cqual))
            delete.deleteColumn(cfam, cqual, ts);
        }
        delete._getCells().remove(i--);
      }
    }

    try {
      if (delete._getCells().size() > 1) client.mutator_set_cells(mutator, delete._getCells());
      else if (delete._getCells().size() == 1)
        client.mutator_set_cell(mutator, delete._getCells().get(0));
    } finally {
      client.mutator_close(mutator);
    }
  }
  /**
   * Method to execute a Scan operation on Hypertable.
   *
   * @param tablename
   * @param scan
   * @return
   * @throws ClientException
   * @throws TException
   */
  public List<Result> scan(String tablename, Scan scan) throws ClientException, TException {
    long scanner = client.scanner_open(ns, tablename, scan._getScanSpec());
    HashMap<String, List<Cell>> buckets = new HashMap<String, List<Cell>>();
    try {
      List<Cell> cells = client.scanner_get_cells(scanner);

      while (cells.size() > 0) {
        for (Cell cell : cells) {
          String row = cell.getKey().getRow();
          if (buckets.get(row) == null) buckets.put(row, new LinkedList<Cell>());
          buckets.get(row).add(cell);
        }
        cells = client.scanner_get_cells(scanner);
      }
    } finally {
      client.scanner_close(scanner);
    }
    List<Result> reslist = new LinkedList<Result>();
    for (String row : buckets.keySet()) {
      reslist.add(new Result(row, buckets.get(row)));
    }
    return reslist;
  }