Search in sources :

Example 11 with BufferedMutator

use of org.apache.hadoop.hbase.client.BufferedMutator in project hbase by apache.

the class MasterProcedureTestingUtility method loadData.

public static void loadData(final Connection connection, final TableName tableName, int rows, final byte[][] splitKeys, final String... sfamilies) throws IOException {
    byte[][] families = new byte[sfamilies.length][];
    for (int i = 0; i < families.length; ++i) {
        families[i] = Bytes.toBytes(sfamilies[i]);
    }
    BufferedMutator mutator = connection.getBufferedMutator(tableName);
    // Ensure one row per region
    assertTrue(rows >= splitKeys.length);
    for (byte[] k : splitKeys) {
        byte[] value = Bytes.add(Bytes.toBytes(System.currentTimeMillis()), k);
        byte[] key = Bytes.add(k, Bytes.toBytes(MD5Hash.getMD5AsHex(value)));
        mutator.mutate(createPut(families, key, value));
        rows--;
    }
    // Add other extra rows. more rows, more files
    while (rows-- > 0) {
        byte[] value = Bytes.add(Bytes.toBytes(System.currentTimeMillis()), Bytes.toBytes(rows));
        byte[] key = Bytes.toBytes(MD5Hash.getMD5AsHex(value));
        mutator.mutate(createPut(families, key, value));
    }
    mutator.flush();
}
Also used : BufferedMutator(org.apache.hadoop.hbase.client.BufferedMutator)

Example 12 with BufferedMutator

use of org.apache.hadoop.hbase.client.BufferedMutator in project hbase by apache.

the class SnapshotTestingUtils method loadData.

public static void loadData(final HBaseTestingUtility util, final TableName tableName, int rows, byte[]... families) throws IOException, InterruptedException {
    BufferedMutator mutator = util.getConnection().getBufferedMutator(tableName);
    loadData(util, mutator, rows, families);
}
Also used : BufferedMutator(org.apache.hadoop.hbase.client.BufferedMutator)

Example 13 with BufferedMutator

use of org.apache.hadoop.hbase.client.BufferedMutator in project hbase by apache.

the class BufferedMutatorExample method run.

@Override
public int run(String[] args) throws InterruptedException, ExecutionException, TimeoutException {
    /** a callback invoked when an asynchronous write fails. */
    final BufferedMutator.ExceptionListener listener = new BufferedMutator.ExceptionListener() {

        @Override
        public void onException(RetriesExhaustedWithDetailsException e, BufferedMutator mutator) {
            for (int i = 0; i < e.getNumExceptions(); i++) {
                LOG.info("Failed to sent put " + e.getRow(i) + ".");
            }
        }
    };
    BufferedMutatorParams params = new BufferedMutatorParams(TABLE).listener(listener);
    //
    try (final Connection conn = ConnectionFactory.createConnection(getConf());
        final BufferedMutator mutator = conn.getBufferedMutator(params)) {
        /** worker pool that operates on BufferedTable instances */
        final ExecutorService workerPool = Executors.newFixedThreadPool(POOL_SIZE);
        List<Future<Void>> futures = new ArrayList<>(TASK_COUNT);
        for (int i = 0; i < TASK_COUNT; i++) {
            futures.add(workerPool.submit(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    //
                    // step 2: each worker sends edits to the shared BufferedMutator instance. They all use
                    // the same backing buffer, call-back "listener", and RPC executor pool.
                    //
                    Put p = new Put(Bytes.toBytes("someRow"));
                    p.addColumn(FAMILY, Bytes.toBytes("someQualifier"), Bytes.toBytes("some value"));
                    mutator.mutate(p);
                    // this worker's edits are sent before exiting the Callable
                    return null;
                }
            }));
        }
        //
        for (Future<Void> f : futures) {
            f.get(5, TimeUnit.MINUTES);
        }
        workerPool.shutdown();
    } catch (IOException e) {
        // exception while creating/destroying Connection or BufferedMutator
        LOG.info("exception while creating/destroying Connection or BufferedMutator", e);
    }
    // invoked from here.
    return 0;
}
Also used : BufferedMutator(org.apache.hadoop.hbase.client.BufferedMutator) Connection(org.apache.hadoop.hbase.client.Connection) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Callable(java.util.concurrent.Callable) Put(org.apache.hadoop.hbase.client.Put) RetriesExhaustedWithDetailsException(org.apache.hadoop.hbase.client.RetriesExhaustedWithDetailsException) BufferedMutatorParams(org.apache.hadoop.hbase.client.BufferedMutatorParams) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future)

Example 14 with BufferedMutator

use of org.apache.hadoop.hbase.client.BufferedMutator in project hbase by apache.

the class JavaHBaseMapGetPutExample method main.

public static void main(String[] args) {
    if (args.length < 1) {
        System.out.println("JavaHBaseBulkGetExample {tableName}");
        return;
    }
    final String tableName = args[0];
    SparkConf sparkConf = new SparkConf().setAppName("JavaHBaseBulkGetExample " + tableName);
    JavaSparkContext jsc = new JavaSparkContext(sparkConf);
    try {
        List<byte[]> list = new ArrayList<>(5);
        list.add(Bytes.toBytes("1"));
        list.add(Bytes.toBytes("2"));
        list.add(Bytes.toBytes("3"));
        list.add(Bytes.toBytes("4"));
        list.add(Bytes.toBytes("5"));
        JavaRDD<byte[]> rdd = jsc.parallelize(list);
        Configuration conf = HBaseConfiguration.create();
        JavaHBaseContext hbaseContext = new JavaHBaseContext(jsc, conf);
        hbaseContext.foreachPartition(rdd, new VoidFunction<Tuple2<Iterator<byte[]>, Connection>>() {

            public void call(Tuple2<Iterator<byte[]>, Connection> t) throws Exception {
                Table table = t._2().getTable(TableName.valueOf(tableName));
                BufferedMutator mutator = t._2().getBufferedMutator(TableName.valueOf(tableName));
                while (t._1().hasNext()) {
                    byte[] b = t._1().next();
                    Result r = table.get(new Get(b));
                    if (r.getExists()) {
                        mutator.mutate(new Put(b));
                    }
                }
                mutator.flush();
                mutator.close();
                table.close();
            }
        });
    } finally {
        jsc.stop();
    }
}
Also used : Table(org.apache.hadoop.hbase.client.Table) HBaseConfiguration(org.apache.hadoop.hbase.HBaseConfiguration) Configuration(org.apache.hadoop.conf.Configuration) BufferedMutator(org.apache.hadoop.hbase.client.BufferedMutator) ArrayList(java.util.ArrayList) Connection(org.apache.hadoop.hbase.client.Connection) JavaHBaseContext(org.apache.hadoop.hbase.spark.JavaHBaseContext) Put(org.apache.hadoop.hbase.client.Put) Result(org.apache.hadoop.hbase.client.Result) Tuple2(scala.Tuple2) Get(org.apache.hadoop.hbase.client.Get) Iterator(java.util.Iterator) JavaSparkContext(org.apache.spark.api.java.JavaSparkContext) SparkConf(org.apache.spark.SparkConf)

Example 15 with BufferedMutator

use of org.apache.hadoop.hbase.client.BufferedMutator in project drill by apache.

the class TestTableGenerator method generateHBaseDatasetIntOBDesc.

public static void generateHBaseDatasetIntOBDesc(Connection conn, Admin admin, TableName tableName, int numberRegions) throws Exception {
    if (admin.tableExists(tableName)) {
        admin.disableTable(tableName);
        admin.deleteTable(tableName);
    }
    HTableDescriptor desc = new HTableDescriptor(tableName);
    desc.addFamily(new HColumnDescriptor(FAMILY_F));
    if (numberRegions > 1) {
        admin.createTable(desc, Arrays.copyOfRange(SPLIT_KEYS, 0, numberRegions - 1));
    } else {
        admin.createTable(desc);
    }
    BufferedMutator table = conn.getBufferedMutator(tableName);
    for (int i = -49; i <= 100; i++) {
        byte[] bytes = new byte[5];
        PositionedByteRange br = new SimplePositionedMutableByteRange(bytes, 0, 5);
        OrderedBytes.encodeInt32(br, i, Order.DESCENDING);
        Put p = new Put(bytes);
        p.addColumn(FAMILY_F, COLUMN_C, String.format("value %d", i).getBytes());
        table.mutate(p);
    }
    table.close();
    admin.flush(tableName);
}
Also used : HColumnDescriptor(org.apache.hadoop.hbase.HColumnDescriptor) BufferedMutator(org.apache.hadoop.hbase.client.BufferedMutator) SimplePositionedMutableByteRange(org.apache.hadoop.hbase.util.SimplePositionedMutableByteRange) PositionedByteRange(org.apache.hadoop.hbase.util.PositionedByteRange) Put(org.apache.hadoop.hbase.client.Put) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor)

Aggregations

BufferedMutator (org.apache.hadoop.hbase.client.BufferedMutator)25 Put (org.apache.hadoop.hbase.client.Put)19 HColumnDescriptor (org.apache.hadoop.hbase.HColumnDescriptor)16 HTableDescriptor (org.apache.hadoop.hbase.HTableDescriptor)16 PositionedByteRange (org.apache.hadoop.hbase.util.PositionedByteRange)8 SimplePositionedMutableByteRange (org.apache.hadoop.hbase.util.SimplePositionedMutableByteRange)8 TableName (org.apache.hadoop.hbase.TableName)3 Connection (org.apache.hadoop.hbase.client.Connection)3 Table (org.apache.hadoop.hbase.client.Table)3 ArrayList (java.util.ArrayList)2 Random (java.util.Random)2 Mutation (org.apache.hadoop.hbase.client.Mutation)2 IOException (java.io.IOException)1 Key (java.security.Key)1 SecureRandom (java.security.SecureRandom)1 Date (java.util.Date)1 Iterator (java.util.Iterator)1 Callable (java.util.concurrent.Callable)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1