BufferedMutator

org.apache.hadoop.hbase.client.BufferedMutator主要用来对HBase的单个表进行操作。它和Put类的作用差不多,但是主要用来实现批量的异步写操作。

BufferedMutator替换了HTable的setAutoFlush(false)的作用。

可以从Connection的实例中获取BufferedMutator的实例。在使用完成后需要调用close()方法关闭连接。对BufferedMutator进行配置需要通过BufferedMutatorParams完成。

MapReduce Job的是BufferedMutator使用的典型场景。MapReduce作业需要批量写入,但是无法找到恰当的点执行flush。BufferedMutator接收MapReduce作业发送来的Put数据后,会根据某些因素(比如接收的Put数据的总量)启发式地执行Batch Put操作,且会异步的提交Batch Put请求,这样MapReduce作业的执行也不会被打断。

BufferedMutator也可以用在一些特殊的情况上。MapReduce作业的每个线程将会拥有一个独立的BufferedMutator对象。一个独立的BufferedMutator也可以用在大容量的在线系统上来执行批量Put操作,但是这时需要注意一些极端情况比如JVM异常或机器故障,此时有可能造成数据丢失。

如下是几个使用BufferedMutator的实例。

实例一:

protected void instantiateHTable() throws IOException {
    for (int i = 0; i < DEFAULT_TABLES_COUNT; i++) {
        BufferedMutatorParamsparams = new BufferedMutatorParams(getTableName(i));
        params.writeBufferSize(4 * 1024 * 1024);
        BufferedMutatortable = connection.getBufferedMutator(params);
        this.tables[i] = table;
    }
}

实例二:

 public int run(String[] args) throws InterruptedException, ExecutionException, TimeoutException {
    final BufferedMutator.ExceptionListenerlistener = new BufferedMutator.ExceptionListener() {
        @Override
        public void onException(RetriesExhaustedWithDetailsException e, BufferedMutatormutator) {
            for (int i = 0; i < e.getNumExceptions(); i++) {
                LOG.info("Failed to sent put " + e.getRow(i) + ".");
            }
        }
    };
    BufferedMutatorParamsparams = new BufferedMutatorParams(TABLE).listener(listener);
    try (final Connectionconn = ConnectionFactory.createConnection(getConf()); final BufferedMutatormutator = conn.getBufferedMutator(params)) {
        final ExecutorServiceworkerPool = 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 {
                    Put p = new Put(Bytes.toBytes("someRow"));
                    p.addColumn(FAMILY, Bytes.toBytes("someQualifier"), Bytes.toBytes("some value"));
                    mutator.mutate(p);
                    return null;
                }
            }));
        }
        for (Future<Void> f : futures) {
            f.get(5, TimeUnit.MINUTES);
        }
        workerPool.shutdown();
    } catch (IOException e) {
        LOG.info("exception while creating/destroying Connection or BufferedMutator", e);
    }
    return 0;
 }

实例三:

    public void testBufferSizeFlush() throws Exception {
        int maxSize = 1024;
        BufferedMutatorParamsparams = new BufferedMutatorParams(TABLE_NAME).writeBufferSize(maxSize);
        try (BufferedMutatormutator = getConnection().getBufferedMutator(params)) {
            Assert.assertTrue(0 == mutator.getWriteBufferSize() || maxSize == mutator.getWriteBufferSize());
            Putput = getPut();
            mutator.mutate(put);
            Assert.assertTrue(mutator.getWriteBufferSize() > 0);
            PutlargePut = new Put(dataHelper.randomData("testrow-"));
            largePut.addColumn(COLUMN_FAMILY, qualifier, Bytes.toBytes(RandomStringUtils.randomAlphanumeric(maxSize * 2)));
            long heapSize = largePut.heapSize();
            Assert.assertTrue("largePut heapsize is : " + heapSize, heapSize > maxSize);
            mutator.mutate(largePut);
            Assert.assertTrue(0 == mutator.getWriteBufferSize() || maxSize == mutator.getWriteBufferSize());
        }
    }

实例四:

public void setup(Contextcontext) throws IOException {
    conf = context.getConfiguration();
    recordsToWrite = conf.getLong(NUM_TO_WRITE_KEY, NUM_TO_WRITE_DEFAULT);
    String tableName = conf.get(TABLE_NAME_KEY, TABLE_NAME_DEFAULT);
    numBackReferencesPerRow = conf.getInt(NUM_BACKREFS_KEY, NUM_BACKREFS_DEFAULT);
    this.connection = ConnectionFactory.createConnection(conf);
    mutator = connection.getBufferedMutator(new BufferedMutatorParams(TableName.valueOf(tableName)).writeBufferSize(4 * 1024 * 1024));
    String taskId = conf.get("mapreduce.task.attempt.id");
    Matchermatcher = Pattern.compile(".+_m_(\\d+_\\d+)").matcher(taskId);
    if (!matcher.matches()) {
        throw new RuntimeException("Strange task ID: " + taskId);
    }
    shortTaskId = matcher.group(1);
    rowsWritten = context.getCounter(Counters.ROWS_WRITTEN);
    refsWritten = context.getCounter(Counters.REFERENCES_WRITTEN);
}

实例五:

protected void setup(Contextcontext) throws IOException, InterruptedException {
    this.conf = context.getConfiguration();
    Connection c = ConnectionFactory.createConnection(this.conf);
    this.fs = FileSystem.get(conf);
    mobCompactionDelay = conf.getLong(SweepJob.MOB_SWEEP_JOB_DELAY, SweepJob.ONE_DAY);
    String tableName = conf.get(TableInputFormat.INPUT_TABLE);
    String familyName = conf.get(TableInputFormat.SCAN_COLUMN_FAMILY);
    TableNametn = TableName.valueOf(tableName);
    this.familyDir = MobUtils.getMobFamilyPath(conf, tn, familyName);
    Adminadmin = c.getAdmin();
    try {
        family = admin.getTableDescriptor(tn).getFamily(Bytes.toBytes(familyName));
        if (family == null) {
            throw new InvalidFamilyOperationException("Column family '" + familyName + "' does not exist. It might be removed.");
        }
    } finally {
        try {
            admin.close();
        } catch (IOException e) {
            LOG.warn("Failed to close the HBaseAdmin", e);
        }
    }
    ConfigurationcopyOfConf = new Configuration(conf);
    copyOfConf.setFloat(HConstants.HFILE_BLOCK_CACHE_SIZE_KEY, 0f);
    this.cacheConfig = new CacheConfig(copyOfConf);
    table = c.getBufferedMutator(new BufferedMutatorParams(tn).writeBufferSize(1 * 1024 * 1024));
    memstore = new MemStoreWrapper(context, fs, table, family, new DefaultMemStore(), cacheConfig);
    this.compactionBegin = conf.getLong(MobConstants.MOB_SWEEP_TOOL_COMPACTION_START_DATE, 0);
    mobTableDir = FSUtils.getTableDir(MobUtils.getMobHome(conf), tn);
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值