@Override
  protected List<RawResource> getAllResources(String rangeStart, String rangeEnd)
      throws IOException {
    byte[] startRow = Bytes.toBytes(rangeStart);
    byte[] endRow = plusZero(Bytes.toBytes(rangeEnd));

    Scan scan = new Scan(startRow, endRow);
    scan.addColumn(B_FAMILY, B_COLUMN_TS);
    scan.addColumn(B_FAMILY, B_COLUMN);

    HTableInterface table = getConnection().getTable(getAllInOneTableName());
    List<RawResource> result = Lists.newArrayList();
    try {
      ResultScanner scanner = table.getScanner(scan);
      for (Result r : scanner) {
        result.add(new RawResource(getInputStream(Bytes.toString(r.getRow()), r), getTimestamp(r)));
      }
    } catch (IOException e) {
      for (RawResource rawResource : result) {
        IOUtils.closeQuietly(rawResource.resource);
      }
      throw e;
    } finally {
      IOUtils.closeQuietly(table);
    }
    return result;
  }
 public static void measureReadingHbase(HTable table, byte[] key, PrintWriter out) {
   try {
     long start1 = System.nanoTime();
     Get g = new Get(key);
     Result r = table.get(g);
     long start2 = System.nanoTime();
     byte[] value = r.getValue(Import.family, Import.qualifier);
     if (value != null) {
       long start3 = System.nanoTime();
       BufferedOutputStream outToNull = new BufferedOutputStream(new ByteArrayOutputStream());
       outToNull.write(value);
       outToNull.close();
       long stop = System.nanoTime(); // stop
       synchronized (out) {
         out.println(
             "NAN, "
                 + (double) (stop - start1) / 1000000.
                 + ", "
                 + (double) (start2 - start1) / 1000000.
                 + ", "
                 + (double) (start3 - start2) / 1000000.
                 + ", "
                 + (double) (stop - start3) / 1000000.); // +
         // " "
         // +
         threadCount++;
       }
     } else {
       System.out.println("No image is extracted with name key " + new String(key));
     }
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
Exemple #3
0
 private HRegionInfo nextRegion() throws IOException {
   try {
     Result results = getMetaRow();
     if (results == null) {
       return null;
     }
     byte[] regionInfoValue =
         results.getValue(HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER);
     if (regionInfoValue == null || regionInfoValue.length == 0) {
       throw new NoSuchElementException(
           "meta region entry missing "
               + Bytes.toString(HConstants.CATALOG_FAMILY)
               + ":"
               + Bytes.toString(HConstants.REGIONINFO_QUALIFIER));
     }
     HRegionInfo region = Writables.getHRegionInfo(regionInfoValue);
     if (!Bytes.equals(region.getTableName(), this.tableName)) {
       return null;
     }
     return region;
   } catch (IOException e) {
     e = RemoteExceptionHandler.checkIOException(e);
     LOG.error("meta scanner error", e);
     metaScanner.close();
     throw e;
   }
 }
    public static List<Delete> GetDeleteEventsBetween(
        Table VTEvent_Table, String imo_str, long first_timestamp, long last_timestamp)
        throws IOException {
      // scan
      // 'cdb_vessel:vessel_event',{FILTER=>"(PrefixFilter('0000003162')"}
      Scan GetEventsBetween = new Scan();
      GetEventsBetween.setStartRow(
              Bytes.toBytes(imo_str + LpadNum(Long.MAX_VALUE - last_timestamp, 19) + "0000000000"))
          .setStopRow(
              Bytes.toBytes(
                  imo_str + LpadNum(Long.MAX_VALUE - first_timestamp + 1, 19) + "9999999999"))
          .addColumn(details, exittime);
      GetEventsBetween.setCaching(100);

      Filter ExistTimeValuefilter =
          new ValueFilter(
              CompareFilter.CompareOp.LESS_OR_EQUAL,
              new BinaryComparator(
                  Bytes.toBytes(new DateTime(last_timestamp).toString(rawformatter))));
      GetEventsBetween.setFilter(ExistTimeValuefilter);

      ResultScanner Result_ExistingEvents = VTEvent_Table.getScanner(GetEventsBetween);
      List<Delete> deletes = new ArrayList<Delete>();

      for (Result res : Result_ExistingEvents) {
        deletes.add(new Delete(res.getRow()));
      }

      Result_ExistingEvents.close();
      return deletes;
    }
Exemple #5
0
  public String[] getTags(String objectType, String objectId) throws IOException {
    List<String> ret = new ArrayList<String>();
    String rowKey = objectType + "_" + objectId;
    Scan s = new Scan(rowKey.getBytes(), rowKey.getBytes());
    s.setMaxVersions(1);
    ResultScanner scanner = htable.getScanner(s);
    try {
      for (Result rr = scanner.next(); rr != null; rr = scanner.next()) {

        String localObjectType =
            new String(rr.getValue("tags".getBytes(), "OBJECTTYPE".getBytes()));
        String localObjectId = new String(rr.getValue("tags".getBytes(), "OBJECTID".getBytes()));
        NavigableMap<byte[], byte[]> map = rr.getFamilyMap("tags".getBytes());
        Iterator<Entry<byte[], byte[]>> it = map.entrySet().iterator();
        while (it.hasNext()) {
          Entry<byte[], byte[]> entry = it.next();
          String key = new String(entry.getKey());
          if (!key.startsWith("OBJECT")) {
            int val = Bytes.toInt(entry.getValue());
            if (val > 0) ret.add(key);
          }
        }
      }
    } finally {
      scanner.close();
    }
    return ret.toArray(new String[] {});
  }
 /* (non-Javadoc)
  * @see com.hazelcast.core.MapLoader#loadAllKeys()
  */
 @Override
 public Set<String> loadAllKeys() {
   Set<String> keySet = null;
   if (allowLoadAll) {
     keySet = new HashSet<String>();
     HTableInterface hti = null;
     try {
       hti = pool.getTable(tableName);
       Scan s = new Scan();
       s.addColumn(family, qualifier);
       ResultScanner rs = hti.getScanner(s);
       Result r = null;
       while ((r = rs.next()) != null) {
         String k = new String(r.getRow());
         keySet.add(k);
       }
     } catch (IOException e) {
       LOG.error("IOException while loading all keys", e);
     } finally {
       if (hti != null) {
         pool.putTable(hti);
       }
     }
   }
   return keySet;
 }
  private HashMap<String, String[][]> getObjectList(
      ResultScanner resultScan, byte[][] family, List<byte[]> rowKeyList) {
    String tableFamilies; // = new table;
    String data[][];
    String rowKey;
    HashMap<String, String[][]> tableDataList = new HashMap<String, String[][]>();

    long k = 0;
    for (Result rows : resultScan) {
      System.out.println("Rows passed : " + (++k));
      for (int i = 0; i < family.length; i++) {

        tableFamilies = Bytes.toString(family[i]);
        rowKey = Bytes.toString(rows.getRow());

        rowKeyList.add(rows.getRow());

        if (tblMngr.getDataFiller(rows, tableFamilies)) {
          data = tblMngr.getResultMap();
          tableFamilies = tableFamilies + "," + rowKey;
          tableDataList.put(tableFamilies, data);
        }
      }
    }
    return tableDataList;
  }
  // Retrieves the row from the table and check if it exists and has not been flagged as deleted
  protected Map<RecordId, Result> getRows(List<RecordId> recordIds, List<FieldType> fields)
      throws RecordException {
    Map<RecordId, Result> results = new HashMap<RecordId, Result>();

    try {
      List<Get> gets = new ArrayList<Get>();
      for (RecordId recordId : recordIds) {
        Get get = new Get(recordId.toBytes());
        // Add the columns for the fields to get
        addFieldsToGet(get, fields);
        get.setMaxVersions(1); // Only retrieve the most recent version of each field
        gets.add(get);
      }

      // Retrieve the data from the repository
      int i = 0;
      for (Result result : recordTable.get(gets)) {
        if (result == null || result.isEmpty()) {
          i++; // Skip this recordId (instead of throwing a RecordNotFoundException)
          continue;
        }
        // Check if the record was deleted
        byte[] deleted = recdec.getLatest(result, RecordCf.DATA.bytes, RecordColumn.DELETED.bytes);
        if ((deleted == null) || (Bytes.toBoolean(deleted))) {
          i++; // Skip this recordId (instead of throwing a RecordNotFoundException)
          continue;
        }
        results.put(recordIds.get(i++), result);
      }
    } catch (IOException e) {
      throw new RecordException(
          "Exception occurred while retrieving records '" + recordIds + "' from HBase table", e);
    }
    return results;
  }
 /*
  * Add to each of the regions in .META. a value.  Key is the startrow of the
  * region (except its 'aaa' for first region).  Actual value is the row name.
  * @param expected
  * @return
  * @throws IOException
  */
 private static int addToEachStartKey(final int expected) throws IOException {
   HTable t = new HTable(TEST_UTIL.getConfiguration(), TABLENAME);
   HTable meta = new HTable(TEST_UTIL.getConfiguration(), HConstants.META_TABLE_NAME);
   int rows = 0;
   Scan scan = new Scan();
   scan.addColumn(HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER);
   ResultScanner s = meta.getScanner(scan);
   for (Result r = null; (r = s.next()) != null; ) {
     byte[] b = r.getValue(HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER);
     if (b == null || b.length <= 0) break;
     HRegionInfo hri = Writables.getHRegionInfo(b);
     // If start key, add 'aaa'.
     byte[] row = getStartKey(hri);
     Put p = new Put(row);
     p.setWriteToWAL(false);
     p.add(getTestFamily(), getTestQualifier(), row);
     t.put(p);
     rows++;
   }
   s.close();
   Assert.assertEquals(expected, rows);
   t.close();
   meta.close();
   return rows;
 }
Exemple #10
0
 @SuppressWarnings("deprecation")
 @Override
 public DResult get(Get get, long startId) throws IOException {
   if (get.hasFamilies()) get.addFamily(DominoConst.INNER_FAMILY);
   get.setTimeRange(0, startId + 1); // [x, y)
   get.setMaxVersions();
   Result preRead = region.get(get);
   List<KeyValue> status = preRead.getColumn(DominoConst.INNER_FAMILY, DominoConst.STATUS_COL);
   if (status == null || status.size() == 0) {
     Result ret = MVCC.handleResult(this, getTrxMetaTable(), preRead, startId, null);
     return new DResult(ret, null);
   }
   Integer lockId = region.getLock(null, get.getRow(), true);
   try {
     Result r =
         MVCC.handleResult(this, getTrxMetaTable(), region.get(get, lockId), startId, lockId);
     return new DResult(r, null);
   } catch (TransactionOutOfDateException oode) {
     return new DResult(null, oode.getMessage());
   } catch (InvalidRowStatusException e) {
     return new DResult(null, e.getMessage());
   } finally {
     region.releaseRowLock(lockId);
   }
 }
Exemple #11
0
  // Retrieves the row from the table and check if it exists and has not been flagged as deleted
  protected Result getRow(
      RecordId recordId, Long version, int numberOfVersions, List<FieldType> fields)
      throws RecordException {
    Result result;
    Get get = new Get(recordId.toBytes());
    get.setFilter(REAL_RECORDS_FILTER);

    try {
      // Add the columns for the fields to get
      addFieldsToGet(get, fields);

      if (version != null)
        get.setTimeRange(0, version + 1); // Only retrieve data within this timerange
      get.setMaxVersions(numberOfVersions);

      // Retrieve the data from the repository
      result = recordTable.get(get);

      if (result == null || result.isEmpty()) throw new RecordNotFoundException(recordId);

    } catch (IOException e) {
      throw new RecordException(
          "Exception occurred while retrieving record '" + recordId + "' from HBase table", e);
    }
    return result;
  }
  private void putAndWait(byte[] row, byte[] fam, HTable source, HTable... targets)
      throws Exception {
    Put put = new Put(row);
    put.add(fam, row, row);
    source.put(put);

    Get get = new Get(row);
    for (int i = 0; i < NB_RETRIES; i++) {
      if (i == NB_RETRIES - 1) {
        fail("Waited too much time for put replication");
      }
      boolean replicatedToAll = true;
      for (HTable target : targets) {
        Result res = target.get(get);
        if (res.size() == 0) {
          LOG.info("Row not available");
          replicatedToAll = false;
          break;
        } else {
          assertArrayEquals(res.value(), row);
        }
      }
      if (replicatedToAll) {
        break;
      } else {
        Thread.sleep(SLEEP_TIME);
      }
    }
  }
  private void deleteAndWait(byte[] row, HTable source, HTable... targets) throws Exception {
    Delete del = new Delete(row);
    source.delete(del);

    Get get = new Get(row);
    for (int i = 0; i < NB_RETRIES; i++) {
      if (i == NB_RETRIES - 1) {
        fail("Waited too much time for del replication");
      }
      boolean removedFromAll = true;
      for (HTable target : targets) {
        Result res = target.get(get);
        if (res.size() >= 1) {
          LOG.info("Row not deleted");
          removedFromAll = false;
          break;
        }
      }
      if (removedFromAll) {
        break;
      } else {
        Thread.sleep(SLEEP_TIME);
      }
    }
  }
 private void checkRow(byte[] row, int count, HTable... tables) throws IOException {
   Get get = new Get(row);
   for (HTable table : tables) {
     Result res = table.get(get);
     assertEquals(count, res.size());
   }
 }
  /**
   * 遍历多行
   *
   * @param tableName 表名
   * @param start_rowkey 开始行键
   * @param stop_rowkey 结束行键
   * @return 行列表
   */
  public ArrayList<HbaseRow> scanRows(String tableName, String start_rowkey, String stop_rowkey) {
    ResultScanner rowstmp = null;
    ArrayList<HbaseRow> rows = null;

    try {
      Scan scan = new Scan();
      scan.setStartRow(Bytes.toBytes(start_rowkey));
      scan.setStopRow(Bytes.toBytes(stop_rowkey));
      HTable table = new HTable(conf, Bytes.toBytes(tableName));
      rowstmp = table.getScanner(scan);
      rows = new ArrayList<>();

      for (Result rowtmp : rowstmp) {
        HbaseRow row = new HbaseRow();
        row.rowkey = Bytes.toString(rowtmp.getRow());
        for (Cell cell : rowtmp.listCells()) {
          HbaseColumn col = new HbaseColumn(cell);
          row.cols.add(col);
        }
        rows.add(row);
      }
    } catch (Exception e) {
      logger.error("scanRows failed", e);
    } finally {
      rowstmp.close();
    }
    return rows;
  }
Exemple #16
0
 public boolean fetchReadAllFieldLine(List<Line> lines, LineSender sender) throws IOException {
   if (null == this.rs) {
     throw new IllegalStateException("HBase Client try to fetch data failed .");
   }
   for (Result result = rs.next(); result != null; result = rs.next()) {
     Get get = new Get(result.getRow());
     for (int i = 0; i < this.families.length; i++) {
       get.addColumn(this.families[i].getBytes(), this.columns[i].getBytes());
     }
     gets.add(get);
     if (gets.size() > this.BUFFER_LINE) {
       Result[] getResults = this.htable.get(gets);
       for (Result resu : getResults) {
         if (null != resu) {
           Line line = sender.createLine();
           for (int i = 0; i < this.families.length; i++) {
             byte[] value = resu.getValue(this.families[i].getBytes(), this.columns[i].getBytes());
             if (null == value) {
               line.addField(null);
             } else {
               line.addField(new String(value, encode));
             }
           }
           line.addField(new String(resu.getRow(), encode));
         }
       }
       return true;
     }
   }
   return false;
 }
  /*
   * (non-Javadoc)
   *
   * @see com.hazelcast.core.MapLoader#load(java.lang.Object)
   */
  @Override
  public String load(String key) {
    String retval = null;
    if (allowLoad) {
      HTableInterface table = null;
      try {
        Get g = new Get(Bytes.toBytes(key));
        table = pool.getTable(tableName);
        Result r = table.get(g);
        byte[] value = r.getValue(family, qualifier);
        if (value != null) {
          if (outputFormatType == StoreFormatType.SMILE) {
            retval = jsonSmileConverter.convertFromSmile(value);
          } else {
            retval = new String(value);
          }
        }
      } catch (IOException e) {
        LOG.error("Value did not exist for row: " + key, e);
      } finally {
        if (pool != null && table != null) {
          pool.putTable(table);
        }
      }
    }

    return retval;
  }
  @TimeDepend
  @Test
  public void testScan_ts_same() throws Exception {

    recreateTable();

    Date ts = parse("2000-01-01", "yyyy-MM-dd");

    Put put = new Put(rowKey_ForTest);

    put.add(ColumnFamilyNameBytes, QName1, ts.getTime(), Bytes.toBytes("a"));

    table.put(put);

    Set<String> resultRowKeys = new HashSet<String>();
    Scan scan = new Scan(rowKey_ForTest, rowKey_ForTest);
    scan.setTimeRange(ts.getTime(), ts.getTime());

    ResultScanner resultScanner = table.getScanner(scan);
    for (Result result = resultScanner.next(); result != null; result = resultScanner.next()) {
      resultRowKeys.add(Bytes.toString(result.getRow()));
    }

    close(resultScanner);

    Assert.assertTrue(resultRowKeys.size() == 0);

    recreateTable();
  }
 /**
  * make a general method that takes a pair of lat/lon and a radius and give a boolean whether it
  * was in or out.
  *
  * @throws IOException
  */
 @Override
 public Map<String, Integer> getAvailableBikesFromAPoint(
     double lat, double lon, double radius, Get get) throws IOException {
   Result r = ((RegionCoprocessorEnvironment) getEnvironment()).getRegion().get(get, null);
   log.debug("r is " + r);
   log.debug(r.getMap().toString());
   Map<String, Integer> result = new HashMap<String, Integer>();
   try {
     String s = null, latStr = null, lonStr = null;
     for (KeyValue kv : r.raw()) {
       s = Bytes.toString(kv.getValue());
       log.debug("cell value is: " + s);
       String[] sArr = s.split(BIXI_DELIMITER); // array of key=value pairs
       latStr = sArr[3];
       lonStr = sArr[4];
       latStr = latStr.substring(latStr.indexOf("=") + 1);
       lonStr = lonStr.substring(lonStr.indexOf("=") + 1);
       log.debug("lon/lat values are: " + lonStr + "; " + latStr);
       double distance =
           giveDistance(Double.parseDouble(latStr), Double.parseDouble(lonStr), lat, lon) - radius;
       log.debug("distance is : " + distance);
       if (distance < 0) { // add it
         result.put(sArr[0], getFreeBikes(kv));
       }
     }
   } finally {
   }
   return result;
 }
Exemple #20
0
 public Optional<Response> get(Optional<Request> request) {
   if (!valid) {
     Logger.error("CANNOT GET! NO VALID CONNECTION");
     return Optional.empty();
   }
   Response response = new Response();
   if (request.isPresent()) {
     Request r = request.get();
     response.key = r.key;
     response.table = r.table;
     try {
       final Table htable = connection.getTable(TableName.valueOf(r.table));
       Result result = htable.get(new Get(r.key));
       if (result == null || result.isEmpty()) {
         return Optional.empty();
       }
       r.columns.forEach(
           c ->
               response.columns.add(
                   new Request.Column(
                       c.family,
                       c.qualifier,
                       result.getValue(c.family.getBytes(), c.qualifier.getBytes()))));
     } catch (IOException e) {
       e.printStackTrace();
     }
   }
   return Optional.of(response);
 }
    private static VesselTrackInfo getTrackInfo(Table TrackInfo_Table, String IMO_str)
        throws IOException {
      Get get = new Get(Bytes.toBytes(IMO_str));
      get.addColumn(details, lastlocation);
      get.addColumn(details, firstrecordtime);
      get.addColumn(details, lastrecordtime);

      Result result = TrackInfo_Table.get(get);

      byte[] last_location = result.getValue(details, lastlocation);
      byte[] fist_recordtime = result.getValue(details, firstrecordtime);
      byte[] last_recordtime = result.getValue(details, lastrecordtime);

      VesselTrackInfo trackinfo = new VesselTrackInfo();
      trackinfo.LastLocation = last_location;

      if (fist_recordtime != null) {
        trackinfo.FirstRecordTime =
            DateTime.parse(Bytes.toString(fist_recordtime), rawformatter).getMillis();
      }

      if (last_recordtime != null) {
        trackinfo.LastRecordTime =
            DateTime.parse(Bytes.toString(last_recordtime), rawformatter).getMillis();
      }

      return trackinfo;
    }
Exemple #22
0
 public Optional<byte[]> get(
     Optional<String> table,
     Optional<String> family,
     Optional<String> qualifier,
     Optional<String> key) {
   if (!valid) {
     Logger.error("CANNOT GET! NO VALID CONNECTION");
     return Optional.empty();
   }
   if (table.isPresent()
       && family.isPresent()
       && qualifier.isPresent()
       && key.isPresent()
       && !key.get().isEmpty()) {
     try {
       final Table htable = connection.getTable(TableName.valueOf(table.get()));
       Result result = htable.get(new Get(key.get().getBytes("UTF8")));
       return Optional.ofNullable(
           result.getValue(family.get().getBytes("UTF8"), qualifier.get().getBytes("UTF8")));
     } catch (IOException e) {
       e.printStackTrace();
     }
   }
   return Optional.empty();
 }
    public static VesselLocation getLocation(Table VTLocation_Table, String RowKey)
        throws IOException {
      if (RowKey != null) {
        Get get = new Get(Bytes.toBytes(RowKey));
        Result result = VTLocation_Table.get(get);

        VesselLocation VL = new VesselLocation();

        for (Cell cell : result.rawCells()) {
          String Qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
          String Value = Bytes.toString(CellUtil.cloneValue(cell));

          if (Qualifier.equals("coordinates")) {
            VL.coordinates = Value;
          } else if (Qualifier.equals("speed")) {
            VL.speed = Value;
          } else if (Qualifier.equals("destination")) {
            VL.destination = Value;
          } else if (Qualifier.equals("timestamp")) {
            VL.recordtime = DateTime.parse(Value, rawformatter).getMillis();
          } else if (Qualifier.equals("previouslocation")) {
            VL.previouslocation = Value;
          } else if (Qualifier.equals("nextlocation")) {
            VL.nextlocation = Value;
          }
        }
        return VL;
      } else {
        return null;
      }
    }
Exemple #24
0
  @Override
  public Set<Element> get(Object key) {
    Get get = new Get(ByteArraySerializer.fromObject(key));
    Result result;
    try {
      result = backingTable.get(get);
    } catch (IOException e) {
      LOG.severe("Cannot get from backing table");
      e.printStackTrace();
      return null;
    }

    NavigableMap<byte[], byte[]> map = result.getFamilyMap(Bytes.toBytes(VALUES));
    if (null == map) return null;

    HashSet<Element> elementHashSet = new HashSet<Element>();

    for (byte[] byteArray : map.keySet()) {

      if (indexClass.equals(HVertex.class) || indexClass.equals(Vertex.class)) {
        HVertex hVertex = new HVertex(hGraph);
        hVertex.setId(byteArray);
        elementHashSet.add(hVertex);
      } else {
        final HEdge hEdge = new HEdge(hGraph);
        hEdge.setId(byteArray);
        elementHashSet.add(hEdge);
      }
    }

    return elementHashSet;
  }
Exemple #25
0
  public String[] getObjectIDs(String objectType, String... tags) throws IOException {
    List<String> ret = new ArrayList<String>();

    FilterList list = new FilterList(FilterList.Operator.MUST_PASS_ALL);
    SingleColumnValueFilter filter1 =
        new SingleColumnValueFilter(
            "tags".getBytes(), "OBJECTTYPE".getBytes(), CompareOp.EQUAL, Bytes.toBytes(objectType));
    list.addFilter(filter1);

    for (String tag : tags) {
      SingleColumnValueFilter filter2 =
          new SingleColumnValueFilter(
              "tags".getBytes(), tag.toUpperCase().getBytes(), CompareOp.EQUAL, Bytes.toBytes(1));
      filter2.setFilterIfMissing(true);
      list.addFilter(filter2);
    }
    Scan s = new Scan();
    s.setFilter(list);
    s.setMaxVersions(1);
    ResultScanner scanner = htable.getScanner(s);
    try {
      for (Result rr = scanner.next(); rr != null; rr = scanner.next()) {

        String localObjectType =
            new String(rr.getValue("tags".getBytes(), "OBJECTTYPE".getBytes()));
        String localObjectId = new String(rr.getValue("tags".getBytes(), "OBJECTID".getBytes()));
        ret.add(localObjectId);
      }
    } finally {
      scanner.close();
    }

    return ret.toArray(new String[] {});
  }
Exemple #26
0
  public static void main(String[] args) {
    Integer id = Integer.parseInt(args[0]);
    System.out.println(id);
    byte[] k = new byte[5];
    k[0] = (byte) 1;
    byte[] col = getColIdbyte(id);

    for (int i = 0; i < 4; i++) {
      k[i + 1] = col[i];
    }
    System.out.println(Bytes.toStringBinary(k));

    Get get = new Get(k);
    // get.addColumn(Bytes.toBytes("A:i"));
    HTable table;
    try {
      table = new HTable(hconf, "new");
      Result result = table.get(get);
      KeyValue[] g = result.raw();
      System.out.println("dfgsdfgsdfgsdfgsdfffffffffffffffffffffffffffffffffff");
      System.out.println("dfgsdfgsdfgsdfgsdfffffffffffffffffffffffffffffffffff");
      System.out.println("dfgsdfgsdfgsdfgsdfffffffffffffffffffffffffffffffffff");
      System.out.println("dfgsdfgsdfgsdfgsdfffffffffffffffffffffffffffffffffff");
      System.out.println("dfgsdfgsdfgsdfgsdfffffffffffffffffffffffffffffffffff");
      System.out.println("dfgsdfgsdfgsdfgsdfffffffffffffffffffffffffffffffffff");
      for (int i = 0; i < g.length; i++) {
        System.out.println(Bytes.toStringBinary(g[i].getValue()) + " kkkkkkkkkkkkkkkkkkkkkkk");
      }
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
Exemple #27
0
  public static String get(String keyspace, String rowKey, String column, long timestamp)
      throws Exception {
    String columnValue = null;

    HTable htable = new HTable(keyspace);
    Get get = new Get(rowKey.getBytes());
    get = get.setTimeStamp(timestamp);
    get = get.setMaxVersions();

    Result res = htable.get(get);

    KeyValue[] data = res.raw();

    for (int i = 0; i < data.length; i++) {
      KeyValue d = data[i];
      String family = new String(data[i].getFamily());
      String qualifier = new String(data[i].getQualifier());
      if (qualifier.toLowerCase().equals(column.toLowerCase())) {
        columnValue = new String(d.getValue());
        System.out.println(
            data[i].toString()
                + " Family:"
                + family
                + " Qualifier:"
                + qualifier
                + " Value:"
                + columnValue);
        break;
      }
    }

    return columnValue;
  }
 public static String get(final MockHTable table, final byte[] key) throws IOException {
   Get get = new Get(key);
   Result result = null;
   result = table.get(get);
   Assert.assertEquals(1, result.size());
   return Bytes.toString(result.getValue(SYNC_COLUMN_FAMILY, SYNC_COLUMN_QUALIFIER));
 }
Exemple #29
0
  public Blog(String blogid) throws IOException {

    Configuration conf = HBaseConfiguration.create();
    table = new HTable(conf, "blogs");

    // 1. Get the row whose row key is blogid from above
    Get g = new Get(Bytes.toBytes(blogid));
    Result r = table.get(g);

    // 2. Extract the rowkey, blog text (column "body") and blog title
    // (column "meta:title")
    key = r.getRow();
    keyStr = Bytes.toString(key);
    blogText = Bytes.toString(r.getValue(Bytes.toBytes("body"), Bytes.toBytes("")));
    blogTitle = Bytes.toString(r.getValue(Bytes.toBytes("meta"), Bytes.toBytes("title")));
    Long reverseTimestamp = Long.parseLong(keyStr.substring(4));
    Long epoch = Math.abs(reverseTimestamp - Long.MAX_VALUE);
    dateOfPost = new Date(epoch);

    // Get an iterator for the comments
    Scan s = new Scan();
    s.addFamily(Bytes.toBytes("comment"));
    // Use a PrefixFilter
    PrefixFilter filter = new PrefixFilter(key);
    s.setFilter(filter);
    scanner = table.getScanner(s);
    resultIterator = scanner.iterator();
  }
  @Override
  protected ArrayList<String> listResourcesImpl(String resPath) throws IOException {
    assert resPath.startsWith("/");
    String lookForPrefix = resPath.endsWith("/") ? resPath : resPath + "/";
    byte[] startRow = Bytes.toBytes(lookForPrefix);
    byte[] endRow = Bytes.toBytes(lookForPrefix);
    endRow[endRow.length - 1]++;

    ArrayList<String> result = new ArrayList<String>();

    HTableInterface table = getConnection().getTable(getAllInOneTableName());
    Scan scan = new Scan(startRow, endRow);
    scan.setFilter(new KeyOnlyFilter());
    try {
      ResultScanner scanner = table.getScanner(scan);
      for (Result r : scanner) {
        String path = Bytes.toString(r.getRow());
        assert path.startsWith(lookForPrefix);
        int cut = path.indexOf('/', lookForPrefix.length());
        String child = cut < 0 ? path : path.substring(0, cut);
        if (result.contains(child) == false) result.add(child);
      }
    } finally {
      IOUtils.closeQuietly(table);
    }
    // return null to indicate not a folder
    return result.isEmpty() ? null : result;
  }