@Override
    public void close(TaskAttemptContext attempt) throws IOException, InterruptedException {
      log.debug("mutations written: " + mutCount + ", values written: " + valCount);
      if (simulate) return;

      try {
        mtbw.close();
      } catch (MutationsRejectedException e) {
        if (e.getSecurityErrorCodes().size() >= 0) {
          HashMap<String, Set<SecurityErrorCode>> tables =
              new HashMap<String, Set<SecurityErrorCode>>();
          for (Entry<TabletId, Set<SecurityErrorCode>> ke : e.getSecurityErrorCodes().entrySet()) {
            String tableId = ke.getKey().getTableId().toString();
            Set<SecurityErrorCode> secCodes = tables.get(tableId);
            if (secCodes == null) {
              secCodes = new HashSet<SecurityErrorCode>();
              tables.put(tableId, secCodes);
            }
            secCodes.addAll(ke.getValue());
          }

          log.error("Not authorized to write to tables : " + tables);
        }

        if (e.getConstraintViolationSummaries().size() > 0) {
          log.error("Constraint violations : " + e.getConstraintViolationSummaries().size());
        }
        throw new IOException(e);
      }
    }
Пример #2
0
  /**
   * Writes a specified number of entries to Accumulo using a {@link BatchWriter}.
   *
   * @throws AccumuloException
   * @throws AccumuloSecurityException
   * @throws TableNotFoundException
   */
  public static void main(String[] args)
      throws AccumuloException, AccumuloSecurityException, TableNotFoundException {

    String seed = null;

    int index = 0;
    String processedArgs[] = new String[13];
    for (int i = 0; i < args.length; i++) {
      if (args[i].equals("-s")) {
        seed = args[++i];
      } else {
        processedArgs[index++] = args[i];
      }
    }

    if (index != 13) {
      System.out.println(
          "Usage : RandomBatchWriter [-s <seed>] <instance name> <zoo keepers> <username> <password> <table> <num> <min> <max> <value size> <max memory> <max latency> <num threads> <visibility>");
      return;
    }

    String instanceName = processedArgs[0];
    String zooKeepers = processedArgs[1];
    String user = processedArgs[2];
    byte[] pass = processedArgs[3].getBytes();
    String table = processedArgs[4];
    int num = Integer.parseInt(processedArgs[5]);
    long min = Long.parseLong(processedArgs[6]);
    long max = Long.parseLong(processedArgs[7]);
    int valueSize = Integer.parseInt(processedArgs[8]);
    long maxMemory = Long.parseLong(processedArgs[9]);
    long maxLatency =
        Long.parseLong(processedArgs[10]) == 0 ? Long.MAX_VALUE : Long.parseLong(processedArgs[10]);
    int numThreads = Integer.parseInt(processedArgs[11]);
    String visiblity = processedArgs[12];

    // Uncomment the following lines for detailed debugging info
    // Logger logger = Logger.getLogger(Constants.CORE_PACKAGE_NAME);
    // logger.setLevel(Level.TRACE);

    Random r;
    if (seed == null) r = new Random();
    else {
      r = new Random(Long.parseLong(seed));
    }

    ZooKeeperInstance instance = new ZooKeeperInstance(instanceName, zooKeepers);
    Connector connector = instance.getConnector(user, pass);
    BatchWriter bw = connector.createBatchWriter(table, maxMemory, maxLatency, numThreads);

    // reuse the ColumnVisibility object to improve performance
    ColumnVisibility cv = new ColumnVisibility(visiblity);

    for (int i = 0; i < num; i++) {

      long rowid = (Math.abs(r.nextLong()) % (max - min)) + min;

      Mutation m = createMutation(rowid, valueSize, cv);

      bw.addMutation(m);
    }

    try {
      bw.close();
    } catch (MutationsRejectedException e) {
      if (e.getAuthorizationFailures().size() > 0) {
        HashSet<String> tables = new HashSet<String>();
        for (KeyExtent ke : e.getAuthorizationFailures()) {
          tables.add(ke.getTableId().toString());
        }
        System.err.println("ERROR : Not authorized to write to tables : " + tables);
      }

      if (e.getConstraintViolationSummaries().size() > 0) {
        System.err.println(
            "ERROR : Constraint violations occurred : " + e.getConstraintViolationSummaries());
      }
    }
  }