Example usage for org.apache.cassandra.utils ByteBufferUtil bytes

List of usage examples for org.apache.cassandra.utils ByteBufferUtil bytes

Introduction

In this page you can find the example usage for org.apache.cassandra.utils ByteBufferUtil bytes.

Prototype

public static ByteBuffer bytes(UUID uuid) 

Source Link

Usage

From source file:appSetup.java

License:Apache License

public static void main(String[] args) throws Exception {
    Cassandra.Iface client = createConnection();

    setupKeyspace(client);/*w ww . java  2 s . c o  m*/
    client.set_keyspace(app.KEYSPACE);
    Map<ByteBuffer, Map<String, List<Mutation>>> mutationMap;
    Column c;

    /*
            // text0: no rows
    try{
          File file = new File("/home/kane/text/2008LNCS/4.txt");
            
          if (checkBeforeReadfile(file)){
            BufferedReader br = new BufferedReader(new FileReader(file));
            
            String str;
            while((str = br.readLine()) != null){
        total += str;
            }
            System.out.println(total);
            br.close();
          }else{
            System.out.println("no file");
          }
        }catch(FileNotFoundException e){
          System.out.println(e);
        }catch(IOException e){
          System.out.println(e);
        }
    */
    String string;
    for (int i = 0; i < TEST_COUNT; i++) {
        string = Integer.toString(i);
        readtext(string + ".txt");

        // text1: 1 row, 1 word
        c = new Column(ByteBufferUtil.bytes("2008LNCS/" + string), ByteBufferUtil.bytes(total),
                System.currentTimeMillis());
        mutationMap = getMutationMap(ByteBufferUtil.bytes("key" + string), app.COLUMN_FAMILY, c);
        client.batch_mutate(mutationMap, ConsistencyLevel.ONE);
        logger.info("added text" + string);
    }
    /*
    readtext("4.txt");
            // text1: 1 row, 2 word
            c = new Column(ByteBufferUtil.bytes("2008LNCS/4"), ByteBufferUtil.bytes(total), System.currentTimeMillis());
            mutationMap = getMutationMap(ByteBufferUtil.bytes("key0"), app.COLUMN_FAMILY, c);
            client.batch_mutate(mutationMap, ConsistencyLevel.ONE);
            logger.info("added text2");
    */
    /*
            // text3: 1000 rows, 1 word
            mutationMap = new HashMap<ByteBuffer,Map<String,List<Mutation>>>();
            for (int i=0; i<1; i++)
            {
    c = new Column(ByteBufferUtil.bytes("text3"), ByteBufferUtil.bytes("word1"), System.currentTimeMillis());
    addToMutationMap(mutationMap, ByteBufferUtil.bytes("key" + i), app.COLUMN_FAMILY, c);
            }
            client.batch_mutate(mutationMap, ConsistencyLevel.ONE);
            logger.info("added text3");
    */
    System.exit(0);
}

From source file:WordCountSetup.java

License:Apache License

public static void main(String[] args) throws Exception {
    Cassandra.Iface client = createConnection();

    setupKeyspace(client);//from   w  w w  . ja  v a  2  s .  c  om

    client.set_keyspace(WordCount.KEYSPACE);

    Map<ByteBuffer, Map<String, List<Mutation>>> mutationMap;
    Column c;

    // text0: no rows

    // text1: 1 row, 1 word
    c = new Column(ByteBufferUtil.bytes("text1"), ByteBufferUtil.bytes("word1"), System.currentTimeMillis());
    mutationMap = getMutationMap(ByteBufferUtil.bytes("key0"), WordCount.COLUMN_FAMILY, c);
    client.batch_mutate(mutationMap, ConsistencyLevel.ONE);
    logger.info("added text1");

    // text1: 1 row, 2 word
    c = new Column(ByteBufferUtil.bytes("text2"), ByteBufferUtil.bytes("word1 word2"),
            System.currentTimeMillis());
    mutationMap = getMutationMap(ByteBufferUtil.bytes("key0"), WordCount.COLUMN_FAMILY, c);
    client.batch_mutate(mutationMap, ConsistencyLevel.ONE);
    logger.info("added text2");

    // text3: 1000 rows, 1 word
    mutationMap = new HashMap<ByteBuffer, Map<String, List<Mutation>>>();
    for (int i = 0; i < 1000; i++) {
        c = new Column(ByteBufferUtil.bytes("text3"), ByteBufferUtil.bytes("word1"),
                System.currentTimeMillis());
        addToMutationMap(mutationMap, ByteBufferUtil.bytes("key" + i), WordCount.COLUMN_FAMILY, c);
    }
    client.batch_mutate(mutationMap, ConsistencyLevel.ONE);
    logger.info("added text3");

    System.exit(0);
}

From source file:WordCount.java

License:Apache License

public int run(String[] args) throws Exception {
    ///start//ww  w  . ja  va2s . c om
    final long startTime = System.currentTimeMillis();
    String outputReducerType = "filesystem";
    if (args != null && args[0].startsWith(OUTPUT_REDUCER_VAR)) {
        String[] s = args[0].split("=");
        if (s != null && s.length == 2)
            outputReducerType = s[1];
    }
    logger.info("output reducer type: " + outputReducerType);

    // use a smaller page size that doesn't divide the row count evenly to exercise the paging logic better
    ConfigHelper.setRangeBatchSize(getConf(), 99);

    for (int i = 0; i < WordCountSetup.TEST_COUNT; i++) {
        String columnName = "userId";
        Job job = new Job(getConf(), "wordcount");
        job.setJarByClass(WordCount.class);
        job.setMapperClass(TokenizerMapper.class);
        if (outputReducerType.equalsIgnoreCase("filesystem")) {
            job.setReducerClass(ReducerToFilesystem.class);
            job.setOutputKeyClass(Text.class);
            job.setOutputValueClass(Text.class);
            FileOutputFormat.setOutputPath(job, new Path(OUTPUT_PATH_PREFIX + i));
        } else {
            job.setReducerClass(ReducerToCassandra.class);

            job.setMapOutputKeyClass(Text.class);
            job.setMapOutputValueClass(Text.class);
            job.setOutputKeyClass(ByteBuffer.class);
            job.setOutputValueClass(List.class);

            job.setOutputFormatClass(ColumnFamilyOutputFormat.class);

            ConfigHelper.setOutputColumnFamily(job.getConfiguration(), KEYSPACE, OUTPUT_COLUMN_FAMILY);
            job.getConfiguration().set(CONF_COLUMN_NAME, "sum");
        }
        job.setInputFormatClass(ColumnFamilyInputFormat.class);
        ConfigHelper.setInputRpcPort(job.getConfiguration(), "9160");
        ConfigHelper.setInputInitialAddress(job.getConfiguration(), "localhost");

        //Change partitioner here
        ConfigHelper.setInputPartitioner(job.getConfiguration(), "RandomPartitioner");
        ConfigHelper.setInputColumnFamily(job.getConfiguration(), KEYSPACE, COLUMN_FAMILY);

        SlicePredicate predicate = new SlicePredicate()
                .setColumn_names(Arrays.asList(ByteBufferUtil.bytes(columnName)));
        ConfigHelper.setInputSlicePredicate(job.getConfiguration(), predicate);

        // this will cause the predicate to be ignored in favor of scanning everything as a wide row
        //Son degisiklik Super Column Support ?
        // ConfigHelper.setInputColumnFamily(job.getConfiguration(), KEYSPACE, COLUMN_FAMILY, true);

        ConfigHelper.setOutputInitialAddress(job.getConfiguration(), "localhost");
        ConfigHelper.setOutputPartitioner(job.getConfiguration(), "RandomPartitioner");
        job.waitForCompletion(true);
    }

    final double duration = (System.currentTimeMillis() - startTime) / 1000.0;
    System.out.println();
    System.out.println("Job Finished in " + duration + " seconds");
    System.out.println();

    return 0;
}

From source file:WordCount.java

License:Apache License

public int run(String[] args) throws Exception {

    ///start/* w w  w  .  ja  va  2  s . c  om*/
    final long startTime = System.currentTimeMillis();
    String outputReducerType = "filesystem";
    if (args != null && args[0].startsWith(OUTPUT_REDUCER_VAR)) {
        String[] s = args[0].split("=");
        if (s != null && s.length == 2)
            outputReducerType = s[1];
    }
    logger.info("output reducer type: " + outputReducerType);

    // use a smaller page size that doesn't divide the row count evenly to exercise the paging logic better
    ConfigHelper.setRangeBatchSize(getConf(), 99);

    for (int i = 0; i < WordCountSetup.TEST_COUNT; i++) {
        String columnName = "userId";
        Job job = new Job(getConf(), "wordcount");
        job.setJarByClass(WordCount.class);
        job.setMapperClass(TokenizerMapper.class);

        if (outputReducerType.equalsIgnoreCase("filesystem")) {
            job.setReducerClass(ReducerToFilesystem.class);
            job.setOutputKeyClass(Text.class);
            job.setOutputValueClass(Text.class);
            FileOutputFormat.setOutputPath(job, new Path(OUTPUT_PATH_PREFIX + i));
        } else {
            job.setReducerClass(ReducerToCassandra.class);

            job.setMapOutputKeyClass(Text.class);
            job.setMapOutputValueClass(Text.class);
            job.setOutputKeyClass(ByteBuffer.class);
            job.setOutputValueClass(List.class);

            job.setOutputFormatClass(ColumnFamilyOutputFormat.class);

            ConfigHelper.setOutputColumnFamily(job.getConfiguration(), KEYSPACE, OUTPUT_COLUMN_FAMILY);
            job.getConfiguration().set(CONF_COLUMN_NAME, "sum");
        }

        job.setInputFormatClass(ColumnFamilyInputFormat.class);
        ConfigHelper.setInputRpcPort(job.getConfiguration(), "9160");
        ConfigHelper.setInputInitialAddress(job.getConfiguration(), "localhost");
        ConfigHelper.setInputPartitioner(job.getConfiguration(), "RandomPartitioner");
        ConfigHelper.setInputColumnFamily(job.getConfiguration(), KEYSPACE, COLUMN_FAMILY);

        SlicePredicate predicate = new SlicePredicate()
                .setColumn_names(Arrays.asList(ByteBufferUtil.bytes(columnName)));
        ConfigHelper.setInputSlicePredicate(job.getConfiguration(), predicate);

        // this will cause the predicate to be ignored in favor of scanning everything as a wide row
        //Son degisiklik
        // ConfigHelper.setInputColumnFamily(job.getConfiguration(), KEYSPACE, COLUMN_FAMILY, true);
        //System.out.println("tessssssaaat");

        ConfigHelper.setOutputInitialAddress(job.getConfiguration(), "localhost");
        ConfigHelper.setOutputPartitioner(job.getConfiguration(), "RandomPartitioner");

        job.waitForCompletion(true);
    }
    //print
    final double duration = (System.currentTimeMillis() - startTime) / 1000.0; // after
    System.out.println();
    System.out.println("Job Finished in " + duration + " seconds");
    System.out.println();

    return 0;
}

From source file:WordCount.java

License:Apache License

public int run(String[] args) throws Exception {

    ///start/* w  w  w .j  a  v  a2  s  . c o  m*/
    final long startTime = System.currentTimeMillis();
    String outputReducerType = "filesystem";
    if (args != null && args[0].startsWith(OUTPUT_REDUCER_VAR)) {
        String[] s = args[0].split("=");
        if (s != null && s.length == 2)
            outputReducerType = s[1];
    }

    logger.info("output reducer type: " + outputReducerType);

    // use a smaller page size that doesn't divide the row count evenly to exercise the paging logic better
    ConfigHelper.setRangeBatchSize(getConf(), 99);

    for (int i = 0; i < WordCountSetup.TEST_COUNT; i++) {
        String columnName = "userId";

        Job job = new Job(getConf(), "wordcount");
        job.setJarByClass(WordCount.class);
        job.setMapperClass(TokenizerMapper.class);

        if (outputReducerType.equalsIgnoreCase("filesystem")) {
            job.setReducerClass(ReducerToFilesystem.class);
            job.setOutputKeyClass(Text.class);
            job.setOutputValueClass(Text.class);
            FileOutputFormat.setOutputPath(job, new Path(OUTPUT_PATH_PREFIX + i));
        } else {
            job.setReducerClass(ReducerToCassandra.class);
            job.setMapOutputKeyClass(Text.class);
            job.setMapOutputValueClass(Text.class);
            job.setOutputKeyClass(ByteBuffer.class);
            job.setOutputValueClass(List.class);
            job.setOutputFormatClass(ColumnFamilyOutputFormat.class);
            ConfigHelper.setOutputColumnFamily(job.getConfiguration(), KEYSPACE, OUTPUT_COLUMN_FAMILY);
            job.getConfiguration().set(CONF_COLUMN_NAME, "sum");
        }
        job.setInputFormatClass(ColumnFamilyInputFormat.class);
        ConfigHelper.setInputRpcPort(job.getConfiguration(), "9160");
        ConfigHelper.setInputInitialAddress(job.getConfiguration(), "localhost");
        ConfigHelper.setInputPartitioner(job.getConfiguration(), "RandomPartitioner");
        ConfigHelper.setInputColumnFamily(job.getConfiguration(), KEYSPACE, COLUMN_FAMILY);

        SlicePredicate predicate = new SlicePredicate()
                .setColumn_names(Arrays.asList(ByteBufferUtil.bytes(columnName)));
        ConfigHelper.setInputSlicePredicate(job.getConfiguration(), predicate);

        // this will cause the predicate to be ignored in favor of scanning everything as a wide row          
        //Son degisiklik
        // ConfigHelper.setInputColumnFamily(job.getConfiguration(), KEYSPACE, COLUMN_FAMILY, true);
        //System.out.println("tessssssaaat");

        ConfigHelper.setOutputInitialAddress(job.getConfiguration(), "localhost");
        ConfigHelper.setOutputPartitioner(job.getConfiguration(), "RandomPartitioner");

        job.waitForCompletion(true);
    }

    //print
    final double duration = (System.currentTimeMillis() - startTime) / 1000.0; // after
    System.out.println();
    System.out.println("Job Finished in " + duration + " seconds");
    System.out.println();

    return 0;
}

From source file:WordCount.java

License:Apache License

public int run(String[] args) throws Exception {

    ///start/*ww  w  .j  a v a2  s  .c om*/
    final long startTime = System.currentTimeMillis();
    String outputReducerType = "filesystem";
    if (args != null && args[0].startsWith(OUTPUT_REDUCER_VAR)) {
        String[] s = args[0].split("=");
        if (s != null && s.length == 2)
            outputReducerType = s[1];
    }
    logger.info("output reducer type: " + outputReducerType);

    // use a smaller page size that doesn't divide the row count evenly to exercise the paging logic better
    ConfigHelper.setRangeBatchSize(getConf(), 99);

    for (int i = 0; i < WordCountSetup.TEST_COUNT; i++) {
        String columnName = "userId";
        Job job = new Job(getConf(), "wordcount");
        job.setJarByClass(WordCount.class);
        job.setMapperClass(TokenizerMapper.class);
        //System.out.println("test");

        if (outputReducerType.equalsIgnoreCase("filesystem")) {
            job.setReducerClass(ReducerToFilesystem.class);
            job.setOutputKeyClass(Text.class);
            job.setOutputValueClass(Text.class);
            FileOutputFormat.setOutputPath(job, new Path(OUTPUT_PATH_PREFIX + i));
        } else {
            job.setReducerClass(ReducerToCassandra.class);
            job.setMapOutputKeyClass(Text.class);
            job.setMapOutputValueClass(Text.class);
            job.setOutputKeyClass(ByteBuffer.class);
            job.setOutputValueClass(List.class);
            job.setOutputFormatClass(ColumnFamilyOutputFormat.class);
            ConfigHelper.setOutputColumnFamily(job.getConfiguration(), KEYSPACE, OUTPUT_COLUMN_FAMILY);
            job.getConfiguration().set(CONF_COLUMN_NAME, "sum");
        }

        job.setInputFormatClass(ColumnFamilyInputFormat.class);

        ConfigHelper.setInputRpcPort(job.getConfiguration(), "9160");
        ConfigHelper.setInputInitialAddress(job.getConfiguration(), "localhost");
        ConfigHelper.setInputPartitioner(job.getConfiguration(), "RandomPartitioner");
        ConfigHelper.setInputColumnFamily(job.getConfiguration(), KEYSPACE, COLUMN_FAMILY);
        SlicePredicate predicate = new SlicePredicate()
                .setColumn_names(Arrays.asList(ByteBufferUtil.bytes(columnName)));
        ConfigHelper.setInputSlicePredicate(job.getConfiguration(), predicate);

        // this will cause the predicate to be ignored in favor of scanning everything as a wide row

        //Son degisiklik
        // ConfigHelper.setInputColumnFamily(job.getConfiguration(), KEYSPACE, COLUMN_FAMILY, true);
        ConfigHelper.setOutputInitialAddress(job.getConfiguration(), "localhost");
        ConfigHelper.setOutputPartitioner(job.getConfiguration(), "RandomPartitioner");
        job.waitForCompletion(true);
    }
    //print
    final double duration = (System.currentTimeMillis() - startTime) / 1000.0; // after
    System.out.println();
    System.out.println("Job Finished in " + duration + " seconds");
    System.out.println();

    return 0;
}

From source file:ClientOnlyExample.java

License:Apache License

private static void testReading() throws Exception {
    // do some queries.
    Collection<ByteBuffer> cols = new ArrayList<ByteBuffer>() {
        {//from  w w w .j a  v  a2s  .c  o m
            add(ByteBufferUtil.bytes("colb"));
        }
    };
    for (int i = 0; i < 100; i++) {
        List<ReadCommand> commands = new ArrayList<ReadCommand>();
        SliceByNamesReadCommand readCommand = new SliceByNamesReadCommand(KEYSPACE,
                ByteBuffer.wrap(("key" + i).getBytes()), new QueryPath(COLUMN_FAMILY, null, null), cols);
        readCommand.setDigestQuery(false);
        commands.add(readCommand);
        List<Row> rows = StorageProxy.read(commands, ConsistencyLevel.ONE);
        assert rows.size() == 1;
        Row row = rows.get(0);
        ColumnFamily cf = row.cf;
        if (cf != null) {
            for (IColumn col : cf.getSortedColumns()) {
                System.out
                        .println(ByteBufferUtil.string(col.name()) + ", " + ByteBufferUtil.string(col.value()));
            }
        } else
            System.err.println("This output indicates that nothing was read.");
    }
}

From source file:andromache.hadoop.WritableMutation.java

License:Apache License

/**
 * Useful method to prepare mutation for single column.
 *
* @param columnName/*from   w ww.  ja v  a  2 s  .c  o m*/
* @param columnValue
* @return
*/
public static WritableMutation setColumn(String keySpace, String columnFamily, String columnName,
        ByteBuffer columnValue) {
    long timestamp = getTimeStamp();

    Column c = new Column();

    c.setName(ByteBufferUtil.bytes(columnName));
    c.setValue(columnValue);
    c.setTimestamp(timestamp);

    Mutation m = new Mutation();

    m.setColumn_or_supercolumn(new ColumnOrSuperColumn());
    m.column_or_supercolumn.setColumn(c);

    return new WritableMutation(keySpace, columnFamily, m);
}

From source file:andromache.hadoop.WritableMutation.java

License:Apache License

public static WritableMutation incCounterColumn(String keySpace, String columnFamily, String counterColumnName,
        long counterDiff) {

    CounterColumn cc = new CounterColumn();

    cc.setName(ByteBufferUtil.bytes(counterColumnName));
    cc.setValue(counterDiff);/*w  ww  . j  av  a2  s.co m*/

    Mutation m = new Mutation();

    m.setColumn_or_supercolumn(new ColumnOrSuperColumn());
    m.column_or_supercolumn.setCounter_column(cc);

    return new WritableMutation(keySpace, columnFamily, m);
}

From source file:co.cask.hydrator.plugin.batch.sink.BatchCassandraSink.java

License:Apache License

private ByteBuffer encodeObject(Object object, Schema schema) throws IOException {
    switch (schema.getType()) {
    case NULL:/*from   w w w. j a  v  a  2  s  .  c  o  m*/
        return ByteBufferUtil.EMPTY_BYTE_BUFFER;
    case BOOLEAN:
        byte[] bytes = new byte[1];
        bytes[0] = (byte) ((boolean) object ? 1 : 0);
        return ByteBuffer.wrap(bytes);
    case INT:
        return ByteBufferUtil.bytes((int) object);
    case LONG:
        return ByteBufferUtil.bytes((long) object);
    case FLOAT:
        return ByteBufferUtil.bytes((float) object);
    case DOUBLE:
        return ByteBufferUtil.bytes((double) object);
    case BYTES:
        return ByteBuffer.wrap((byte[]) object);
    case STRING:
    case ENUM:
        // Currently there is no standard container to represent enum type
        return ByteBufferUtil.bytes((String) object);
    case UNION:
        if (schema.isNullableSimple()) {
            return object == null ? ByteBufferUtil.EMPTY_BYTE_BUFFER
                    : encodeObject(object, schema.getNonNullable());
        }
    }
    throw new IOException("Unsupported field type; only simple types are supported: " + schema);
}