public static void main(String args[]) throws IOException {
    LoaderOptions options = LoaderOptions.parseArgs(args);
    try {
      SSTableLoader loader =
          new SSTableLoader(options.directory, new ExternalClient(options), options);
      SSTableLoader.LoaderFuture future = loader.stream(options.ignores);

      if (options.noProgress) {
        future.get();
      } else {
        ProgressIndicator indicator = new ProgressIndicator(future.getPendingFiles());
        indicator.start();
        System.out.println("");
        while (!future.isDone()) {
          if (indicator.printProgress()) {
            // We're done with streaming
            System.out.println("\nWaiting for targets to rebuild indexes ...");
            future.get();
            assert future.isDone();
          } else {
            try {
              Thread.sleep(1000L);
            } catch (Exception e) {
            }
          }
        }
      }

      System.exit(0); // We need that to stop non daemonized threads
    } catch (Exception e) {
      System.err.println(e.getMessage());
      if (options.debug) e.printStackTrace(System.err);
      System.exit(1);
    }
  }
Exemplo n.º 2
0
  /**
   * Insert a record in the database. Any field/value pairs in the specified values HashMap will be
   * written into the record with the specified record key.
   *
   * @param table The name of the table
   * @param key The record key of the record to insert.
   * @param values A HashMap of field/value pairs to insert in the record
   * @return Zero on success, a non-zero error code on error
   */
  public int insert(String table, String key, HashMap<String, ByteIterator> values) {
    if (!_table.equals(table)) {
      try {
        client.set_keyspace(table);
        _table = table;
      } catch (Exception e) {
        e.printStackTrace();
        e.printStackTrace(System.out);
        return Error;
      }
    }

    for (int i = 0; i < OperationRetries; i++) {
      if (_debug) {
        System.out.println("Inserting key: " + key);
      }

      try {
        ByteBuffer wrappedKey = ByteBuffer.wrap(key.getBytes("UTF-8"));

        ColumnOrSuperColumn column;
        for (Map.Entry<String, ByteIterator> entry : values.entrySet()) {
          column = new ColumnOrSuperColumn();
          Column subColumn = new Column(ByteBuffer.wrap(entry.getKey().getBytes("UTF-8")));
          subColumn.setValue(ByteBuffer.wrap(entry.getValue().toArray()));
          subColumn.setTimestamp(System.currentTimeMillis());
          column.setColumn(subColumn);

          mutations.add(new Mutation().setColumn_or_supercolumn(column));
        }

        mutationMap.put(column_family, mutations);
        record.put(wrappedKey, mutationMap);

        client.batch_mutate(record, ConsistencyLevel.ONE);

        mutations.clear();
        mutationMap.clear();
        record.clear();

        return Ok;
      } catch (Exception e) {
        errorexception = e;
      }
      try {
        Thread.sleep(500);
      } catch (InterruptedException e) {
      }
    }

    errorexception.printStackTrace();
    errorexception.printStackTrace(System.out);
    return Error;
  }
Exemplo n.º 3
0
  /**
   * Delete a record from the database.
   *
   * @param table The name of the table
   * @param key The record key of the record to delete.
   * @return Zero on success, a non-zero error code on error
   */
  public int delete(String table, String key) {
    if (!_table.equals(table)) {
      try {
        client.set_keyspace(table);
        _table = table;
      } catch (Exception e) {
        e.printStackTrace();
        e.printStackTrace(System.out);
        return Error;
      }
    }

    for (int i = 0; i < OperationRetries; i++) {
      try {
        client.remove(
            ByteBuffer.wrap(key.getBytes("UTF-8")),
            new ColumnPath(column_family),
            System.currentTimeMillis(),
            ConsistencyLevel.ONE);

        if (_debug) {
          System.out.println("Delete key: " + key);
        }

        return Ok;
      } catch (Exception e) {
        errorexception = e;
      }
      try {
        Thread.sleep(500);
      } catch (InterruptedException e) {
      }
    }
    errorexception.printStackTrace();
    errorexception.printStackTrace(System.out);
    return Error;
  }
Exemplo n.º 4
0
  public static void main(String[] args) {
    CassandraClient7 cli = new CassandraClient7();

    Properties props = new Properties();

    props.setProperty("hosts", args[0]);
    cli.setProperties(props);

    try {
      cli.init();
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(0);
    }

    HashMap<String, ByteIterator> vals = new HashMap<String, ByteIterator>();
    vals.put("age", new StringByteIterator("57"));
    vals.put("middlename", new StringByteIterator("bradley"));
    vals.put("favoritecolor", new StringByteIterator("blue"));
    int res = cli.insert("usertable", "BrianFrankCooper", vals);
    System.out.println("Result of insert: " + res);

    HashMap<String, ByteIterator> result = new HashMap<String, ByteIterator>();
    HashSet<String> fields = new HashSet<String>();
    fields.add("middlename");
    fields.add("age");
    fields.add("favoritecolor");
    res = cli.read("usertable", "BrianFrankCooper", null, result);
    System.out.println("Result of read: " + res);
    for (String s : result.keySet()) {
      System.out.println("[" + s + "]=[" + result.get(s) + "]");
    }

    res = cli.delete("usertable", "BrianFrankCooper");
    System.out.println("Result of delete: " + res);
  }
  public static void main(String args[]) {
    Config.setClientMode(true);
    LoaderOptions options = LoaderOptions.parseArgs(args);
    OutputHandler handler = new OutputHandler.SystemOutput(options.verbose, options.debug);
    SSTableLoader loader =
        new SSTableLoader(
            options.directory,
            new ExternalClient(
                options.hosts,
                options.rpcPort,
                options.user,
                options.passwd,
                options.transportFactory,
                options.storagePort,
                options.sslStoragePort,
                options.serverEncOptions),
            handler);
    DatabaseDescriptor.setStreamThroughputOutboundMegabitsPerSec(options.throttle);
    StreamResultFuture future = null;
    try {
      if (options.noProgress) future = loader.stream(options.ignores);
      else future = loader.stream(options.ignores, new ProgressIndicator());
    } catch (Exception e) {
      System.err.println(e.getMessage());
      if (e.getCause() != null) System.err.println(e.getCause());
      if (options.debug) e.printStackTrace(System.err);
      else System.err.println("Run with --debug to get full stack trace or --help to get help.");
      System.exit(1);
    }

    handler.output(String.format("Streaming session ID: %s", future.planId));

    try {
      future.get();
      System.exit(0); // We need that to stop non daemonized threads
    } catch (Exception e) {
      System.err.println("Streaming to the following hosts failed:");
      System.err.println(loader.getFailedHosts());
      System.err.println(e);
      if (options.debug) e.printStackTrace(System.err);
      System.exit(1);
    }
  }
Exemplo n.º 6
0
  /**
   * Retrieves a local subBlock
   *
   * @param blockId row key
   * @param sblockId SubBlock column name
   * @param offset inside the sblock
   * @return a local sublock
   * @throws TException
   */
  private LocalBlock getLocalSubBlock(
      String subBlockCFName, ByteBuffer blockId, ByteBuffer sblockId, int offset)
      throws TException {
    DecoratedKey<Token<?>> decoratedKey =
        new DecoratedKey<Token<?>>(StorageService.getPartitioner().getToken(blockId), blockId);

    Table table = Table.open(cfsKeyspace);
    ColumnFamilyStore sblockStore = table.getColumnFamilyStore(subBlockCFName);

    Collection<SSTableReader> sstables = sblockStore.getSSTables();

    for (SSTableReader sstable : sstables) {

      long position = sstable.getPosition(decoratedKey, Operator.EQ);

      if (position == -1) continue;

      String filename = sstable.descriptor.filenameFor(Component.DATA);
      RandomAccessFile raf = null;
      int mappedLength = -1;
      MappedByteBuffer mappedData = null;
      MappedFileDataInput file = null;
      try {
        raf = new RandomAccessFile(filename, "r");
        assert position < raf.length();

        mappedLength =
            (raf.length() - position) < Integer.MAX_VALUE
                ? (int) (raf.length() - position)
                : Integer.MAX_VALUE;

        mappedData = raf.getChannel().map(FileChannel.MapMode.READ_ONLY, position, mappedLength);

        file = new MappedFileDataInput(mappedData, filename, 0);

        if (file == null) continue;

        // Verify key was found in data file
        DecoratedKey keyInDisk =
            SSTableReader.decodeKey(
                sstable.partitioner, sstable.descriptor, ByteBufferUtil.readWithShortLength(file));
        assert keyInDisk.equals(decoratedKey)
            : String.format("%s != %s in %s", keyInDisk, decoratedKey, file.getPath());

        long rowSize = SSTableReader.readRowSize(file, sstable.descriptor);

        assert rowSize > 0;
        assert rowSize < mappedLength;

        Filter bf = IndexHelper.defreezeBloomFilter(file, sstable.descriptor.usesOldBloomFilter);

        // verify this column in in this version of the row.
        if (!bf.isPresent(sblockId)) continue;

        List<IndexHelper.IndexInfo> indexList = IndexHelper.deserializeIndex(file);

        // we can stop early if bloom filter says none of the
        // columns actually exist -- but,
        // we can't stop before initializing the cf above, in
        // case there's a relevant tombstone
        ColumnFamilySerializer serializer = ColumnFamily.serializer();
        try {
          ColumnFamily cf =
              serializer.deserializeFromSSTableNoColumns(
                  ColumnFamily.create(sstable.metadata), file);

          if (cf.isMarkedForDelete()) continue;

        } catch (Exception e) {
          e.printStackTrace();

          throw new IOException(
              serializer
                  + " failed to deserialize "
                  + sstable.getColumnFamilyName()
                  + " with "
                  + sstable.metadata
                  + " from "
                  + file,
              e);
        }

        Integer sblockLength = null;

        if (indexList == null) sblockLength = seekToSubColumn(sstable.metadata, file, sblockId);
        else sblockLength = seekToSubColumn(sstable.metadata, file, sblockId, indexList);

        if (sblockLength == null || sblockLength < 0) continue;

        int bytesReadFromStart = mappedLength - (int) file.bytesRemaining();

        if (logger.isDebugEnabled())
          logger.debug("BlockLength = " + sblockLength + " Availible " + file.bytesRemaining());

        assert offset <= sblockLength : String.format("%d > %d", offset, sblockLength);

        long dataOffset = position + bytesReadFromStart;

        if (file.bytesRemaining() == 0 || sblockLength == 0) continue;

        return new LocalBlock(file.getPath(), dataOffset + offset, sblockLength - offset);

      } catch (IOException e) {
        throw new TException(e);
      } finally {
        FileUtils.closeQuietly(raf);
      }
    }

    return null;
  }
Exemplo n.º 7
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
   */
  public int scan(
      String table,
      String startkey,
      int recordcount,
      Set<String> fields,
      Vector<HashMap<String, ByteIterator>> result) {
    if (!_table.equals(table)) {
      try {
        client.set_keyspace(table);
        _table = table;
      } catch (Exception e) {
        e.printStackTrace();
        e.printStackTrace(System.out);
        return Error;
      }
    }

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

      try {
        SlicePredicate predicate;
        if (fields == null) {
          predicate =
              new SlicePredicate()
                  .setSlice_range(new SliceRange(emptyByteBuffer, emptyByteBuffer, false, 1000000));

        } else {
          ArrayList<ByteBuffer> fieldlist = new ArrayList<ByteBuffer>(fields.size());
          for (String s : fields) {
            fieldlist.add(ByteBuffer.wrap(s.getBytes("UTF-8")));
          }

          predicate = new SlicePredicate().setColumn_names(fieldlist);
        }

        KeyRange kr =
            new KeyRange()
                .setStart_key(startkey.getBytes("UTF-8"))
                .setEnd_key(new byte[] {})
                .setCount(recordcount);

        List<KeySlice> results =
            client.get_range_slices(parent, predicate, kr, ConsistencyLevel.ONE);

        if (_debug) {
          System.out.println("Scanning startkey: " + startkey);
        }

        HashMap<String, ByteIterator> tuple;
        for (KeySlice oneresult : results) {
          tuple = new HashMap<String, ByteIterator>();

          Column column;
          String name;
          ByteIterator value;
          for (ColumnOrSuperColumn onecol : oneresult.columns) {
            column = onecol.column;
            name =
                new String(
                    column.name.array(),
                    column.name.position() + column.name.arrayOffset(),
                    column.name.remaining());
            value =
                new ByteArrayByteIterator(
                    column.value.array(),
                    column.value.position() + column.value.arrayOffset(),
                    column.value.remaining());

            tuple.put(name, value);

            if (_debug) {
              System.out.print("(" + name + "=" + value + ")");
            }
          }

          result.add(tuple);
          if (_debug) {
            System.out.println();
          }
        }

        return Ok;
      } catch (Exception e) {
        errorexception = e;
      }
      try {
        Thread.sleep(500);
      } catch (InterruptedException e) {
      }
    }
    errorexception.printStackTrace();
    errorexception.printStackTrace(System.out);
    return Error;
  }
Exemplo n.º 8
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 int read(
      String table, String key, Set<String> fields, HashMap<String, ByteIterator> result) {
    if (!_table.equals(table)) {
      try {
        client.set_keyspace(table);
        _table = table;
      } catch (Exception e) {
        e.printStackTrace();
        e.printStackTrace(System.out);
        return Error;
      }
    }

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

      try {
        SlicePredicate predicate;
        if (fields == null) {
          predicate =
              new SlicePredicate()
                  .setSlice_range(new SliceRange(emptyByteBuffer, emptyByteBuffer, false, 1000000));

        } else {
          ArrayList<ByteBuffer> fieldlist = new ArrayList<ByteBuffer>(fields.size());
          for (String s : fields) {
            fieldlist.add(ByteBuffer.wrap(s.getBytes("UTF-8")));
          }

          predicate = new SlicePredicate().setColumn_names(fieldlist);
        }

        List<ColumnOrSuperColumn> results =
            client.get_slice(
                ByteBuffer.wrap(key.getBytes("UTF-8")), parent, predicate, ConsistencyLevel.ONE);

        if (_debug) {
          System.out.print("Reading key: " + key);
        }

        Column column;
        String name;
        ByteIterator value;
        for (ColumnOrSuperColumn oneresult : results) {

          column = oneresult.column;
          name =
              new String(
                  column.name.array(),
                  column.name.position() + column.name.arrayOffset(),
                  column.name.remaining());
          value =
              new ByteArrayByteIterator(
                  column.value.array(),
                  column.value.position() + column.value.arrayOffset(),
                  column.value.remaining());

          result.put(name, value);

          if (_debug) {
            System.out.print("(" + name + "=" + value + ")");
          }
        }

        if (_debug) {
          System.out.println();
        }

        return Ok;
      } catch (Exception e) {
        errorexception = e;
      }

      try {
        Thread.sleep(500);
      } catch (InterruptedException e) {
      }
    }
    errorexception.printStackTrace();
    errorexception.printStackTrace(System.out);
    return Error;
  }