private Value reduceMultiValue(
     final Key key, final Iterator<Value> iter, final Value firstValue) {
   final String group;
   try {
     group = new String(key.getColumnFamilyData().getBackingArray(), CommonConstants.UTF_8);
   } catch (final UnsupportedEncodingException e) {
     throw new RuntimeException(e.getMessage(), e);
   }
   ElementAggregator aggregator;
   Properties firstPropertySet;
   try {
     firstPropertySet = elementConverter.getPropertiesFromValue(group, firstValue);
     aggregator = schema.getElement(group).getAggregator();
     aggregator.aggregate(firstPropertySet);
     while (iter.hasNext()) {
       aggregator.aggregate(elementConverter.getPropertiesFromValue(group, iter.next()));
     }
   } catch (final AccumuloElementConversionException e) {
     throw new IllegalArgumentException("Failed to get Properties from an accumulo value", e);
   }
   final Properties properties = new Properties();
   aggregator.state(properties);
   try {
     return elementConverter.getValueFromProperties(group, properties);
   } catch (final AccumuloElementConversionException e) {
     throw new IllegalArgumentException("Failed to get Properties from an accumulo value", e);
   }
 }
 private static float getProgressForRange(final Range range, final Key currentKey) {
   if (currentKey == null) {
     return 0f;
   }
   if ((range.getStartKey() != null) && (range.getEndKey() != null)) {
     if (!range.getStartKey().equals(range.getEndKey(), PartialKey.ROW)) {
       // just look at the row progress
       return getProgressForRange(
           range.getStartKey().getRowData(),
           range.getEndKey().getRowData(),
           currentKey.getRowData());
     } else if (!range.getStartKey().equals(range.getEndKey(), PartialKey.ROW_COLFAM)) {
       // just look at the column family progress
       return getProgressForRange(
           range.getStartKey().getColumnFamilyData(),
           range.getEndKey().getColumnFamilyData(),
           currentKey.getColumnFamilyData());
     } else if (!range.getStartKey().equals(range.getEndKey(), PartialKey.ROW_COLFAM_COLQUAL)) {
       // just look at the column qualifier progress
       return getProgressForRange(
           range.getStartKey().getColumnQualifierData(),
           range.getEndKey().getColumnQualifierData(),
           currentKey.getColumnQualifierData());
     }
   }
   // if we can't figure it out, then claim no progress
   return 0f;
 }
示例#3
0
  /** Write entries to a table. */
  public static void writeEntries(
      Connector connector, Map<Key, Value> map, String table, boolean createIfNotExist) {
    if (createIfNotExist && !connector.tableOperations().exists(table))
      try {
        connector.tableOperations().create(table);
      } catch (AccumuloException | AccumuloSecurityException e) {
        log.error("trouble creating " + table, e);
        throw new RuntimeException(e);
      } catch (TableExistsException e) {
        log.error("crazy", e);
        throw new RuntimeException(e);
      }

    BatchWriterConfig bwc = new BatchWriterConfig();
    BatchWriter bw;
    try {
      bw = connector.createBatchWriter(table, bwc);
    } catch (TableNotFoundException e) {
      log.error("tried to write to a non-existant table " + table, e);
      throw new RuntimeException(e);
    }

    try {
      for (Map.Entry<Key, Value> entry : map.entrySet()) {
        Key k = entry.getKey();
        ByteSequence rowData = k.getRowData(),
            cfData = k.getColumnFamilyData(),
            cqData = k.getColumnQualifierData();
        Mutation m = new Mutation(rowData.getBackingArray(), rowData.offset(), rowData.length());
        m.put(
            cfData.getBackingArray(),
            cqData.getBackingArray(),
            k.getColumnVisibilityParsed(),
            entry.getValue().get());
        bw.addMutation(m);
      }

    } catch (MutationsRejectedException e) {
      log.error("mutations rejected", e);
      throw new RuntimeException(e);
    } finally {
      try {
        bw.close();
      } catch (MutationsRejectedException e) {
        log.error("mutations rejected while trying to close BatchWriter", e);
      }
    }
  }
示例#4
0
  @Override
  protected HashCode hash(HashFunction hashFunction, Key k) {
    Hasher hasher = hashFunction.newHasher();

    if (row) {
      putByteSquence(k.getRowData(), hasher);
    }

    if (family) {
      putByteSquence(k.getColumnFamilyData(), hasher);
    }

    if (qualifier) {
      putByteSquence(k.getColumnQualifierData(), hasher);
    }

    if (visibility) {
      putByteSquence(k.getColumnVisibilityData(), hasher);
    }

    return hasher.hash();
  }
示例#5
0
  public static Entry<Key, Value> checkColumn(
      Environment env, IteratorSetting iterConf, Bytes row, Column col) {
    Span span = Span.exact(row, col);

    Scanner scanner;
    try {
      // TODO reuse or share scanner
      scanner = env.getConnector().createScanner(env.getTable(), env.getAuthorizations());
    } catch (TableNotFoundException e) {
      // TODO proper exception handling
      throw new RuntimeException(e);
    }
    scanner.setRange(SpanUtil.toRange(span));
    scanner.addScanIterator(iterConf);

    Iterator<Entry<Key, Value>> iter = scanner.iterator();
    if (iter.hasNext()) {
      Entry<Key, Value> entry = iter.next();

      Key k = entry.getKey();
      Bytes r = Bytes.of(k.getRowData().toArray());
      Bytes cf = Bytes.of(k.getColumnFamilyData().toArray());
      Bytes cq = Bytes.of(k.getColumnQualifierData().toArray());
      Bytes cv = Bytes.of(k.getColumnVisibilityData().toArray());

      if (r.equals(row)
          && cf.equals(col.getFamily())
          && cq.equals(col.getQualifier())
          && cv.equals(col.getVisibility())) {
        return entry;
      } else {
        throw new RuntimeException("unexpected key " + k + " " + row + " " + col);
      }
    }

    return null;
  }
示例#6
0
 public static Column convert(Key k) {
   Bytes f = ByteUtil.toBytes(k.getColumnFamilyData());
   Bytes q = ByteUtil.toBytes(k.getColumnQualifierData());
   Bytes v = ByteUtil.toBytes(k.getColumnVisibilityData());
   return new Column(f, q, v);
 }
示例#7
0
 /**
  * Create a copy of key with all fields except the ones specified cleared.
  *
  * @param key The key to copy
  * @param pk What fields to retain from the key
  * @return A new Key object pointing to new copies of fields specified by pk; other fields are
  *     empty/default.
  */
 public static Key keyCopy(Key key, PartialKey pk) {
   if (key == null || pk == null) return null;
   switch (pk) {
     case ROW:
       return new Key(
           key.getRowData().getBackingArray(),
           EMPTY_BYTES,
           EMPTY_BYTES,
           EMPTY_BYTES,
           Long.MAX_VALUE,
           false,
           true);
     case ROW_COLFAM:
       return new Key(
           key.getRowData().getBackingArray(),
           key.getColumnFamilyData().getBackingArray(),
           EMPTY_BYTES,
           EMPTY_BYTES,
           Long.MAX_VALUE,
           false,
           true);
     case ROW_COLFAM_COLQUAL:
       return new Key(
           key.getRowData().getBackingArray(),
           key.getColumnFamilyData().getBackingArray(),
           key.getColumnQualifierData().getBackingArray(),
           EMPTY_BYTES,
           Long.MAX_VALUE,
           false,
           true);
     case ROW_COLFAM_COLQUAL_COLVIS:
       return new Key(
           key.getRowData().getBackingArray(),
           key.getColumnFamilyData().getBackingArray(),
           key.getColumnQualifierData().getBackingArray(),
           key.getColumnVisibilityData().getBackingArray(),
           Long.MAX_VALUE,
           false,
           true);
     case ROW_COLFAM_COLQUAL_COLVIS_TIME:
       return new Key(
           key.getRowData().getBackingArray(),
           key.getColumnFamilyData().getBackingArray(),
           key.getColumnQualifierData().getBackingArray(),
           key.getColumnVisibilityData().getBackingArray(),
           key.getTimestamp(),
           false,
           true);
     case ROW_COLFAM_COLQUAL_COLVIS_TIME_DEL:
       return new Key(
           key.getRowData().getBackingArray(),
           key.getColumnFamilyData().getBackingArray(),
           key.getColumnQualifierData().getBackingArray(),
           key.getColumnVisibilityData().getBackingArray(),
           key.getTimestamp(),
           key.isDeleted(),
           true);
     default:
       throw new AssertionError("unknown pk: " + pk);
   }
 }