Hbase之批量数据写入

19 篇文章 0 订阅
/**
  * Created by similarface on 16/8/16.
  */
 
import  java.io.IOException;
 
import  org.apache.hadoop.conf.Configuration;
import  org.apache.hadoop.hbase.HBaseConfiguration;
import  org.apache.hadoop.hbase.TableName;
import  org.apache.hadoop.hbase.client.Connection;
import  org.apache.hadoop.hbase.client.ConnectionFactory;
import  org.apache.hadoop.hbase.client.Get;
import  org.apache.hadoop.hbase.client.Table;
import  org.apache.hadoop.hbase.client.Put;
import  org.apache.hadoop.hbase.client.Result;
import  org.apache.hadoop.hbase.util.Bytes;
import  org.apache.hadoop.hbase.client.BufferedMutator;
import  org.apache.hadoop.hbase.Cell;
import  org.apache.hadoop.hbase.CellUtil;
import  org.apache.hadoop.hbase.client.Mutation;
import  java.util.List;
import  java.util.ArrayList;
public  class  PutBufferExample {
     public  static  void  main(String[] args)  throws  IOException {
         //获取陪着参数
         Configuration config = HBaseConfiguration.create();
         //建立连接
         Connection connection = ConnectionFactory.createConnection(config);
         try  {
             //连接表 获取表对象
             Table t = connection.getTable(TableName.valueOf( "testtable" ));
             BufferedMutator table = connection.getBufferedMutator(TableName.valueOf( "testtable" ));
             try  {
                 Put p =  new  Put(Bytes.toBytes( "myrow-1" ));
                 //p.add(); 这个地方的add 是个过期的方法然而我并不知道Cell的用法是什么
                 p.add(Bytes.toBytes( "colfam1" ), Bytes.toBytes( "name1" ), Bytes.toBytes( "zhangsan1" ));
                 //table.put(p);
                 List<Mutation> mutations =  new  ArrayList<Mutation>();
                 mutations.add(p);
                 table.mutate(mutations);
                 //如果不flush 在后面get可能是看不见的
                 table.flush();
                 // Close your table and cluster connection.
                 Get get= new  Get(Bytes.toBytes( "myrow-1" ));
                 Result result=t.get(get);
                 for (Cell cell:result.rawCells()){
                     System.out.print( "行健: " + new  String(CellUtil.cloneRow(cell)));
                     System.out.print( "\t列簇: " + new  String(CellUtil.cloneFamily(cell)));
                     System.out.print( "\t列: " + new  String(CellUtil.cloneQualifier(cell)));
                     System.out.print( "\t值: " + new  String(CellUtil.cloneValue(cell)));
                     System.out.println( "\t时间戳: " +cell.getTimestamp());
                 }
                 System.out.print( ">>>>end" );
             finally  {
                 if  (table !=  null ) table.close();
             }
         finally  {
             connection.close();
         }
     }
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
HBase中进行多线程批量数据写入可以提高写入效率。以下是一个简单的示例代码,演示了如何使用Java多线程进行批量数据写入: ```java import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class HBaseMultiThreadedWriter { private static final String TABLE_NAME = "your_table"; private static final String COLUMN_FAMILY = "cf"; private static final String COLUMN_QUALIFIER = "col"; public static void main(String[] args) { Configuration config = HBaseConfiguration.create(); config.set("hbase.zookeeper.quorum", "your_zookeeper_quorum"); try (Connection connection = ConnectionFactory.createConnection(config); Table table = connection.getTable(TableName.valueOf(TABLE_NAME))) { ExecutorService executorService = Executors.newFixedThreadPool(10); // 控制线程池大小 List<Runnable> tasks = new ArrayList<>(); // 创建100个写入任务 for (int i = 0; i < 100; i++) { final int index = i; Runnable task = () -> { try { // 构造Put对象 Put put = new Put(Bytes.toBytes("rowkey_" + index)); put.addColumn(Bytes.toBytes(COLUMN_FAMILY), Bytes.toBytes(COLUMN_QUALIFIER), Bytes.toBytes("value_" + index)); // 执行写入操作 table.put(put); } catch (Exception e) { e.printStackTrace(); } }; tasks.add(task); } // 提交任务给线程池执行 tasks.forEach(executorService::submit); // 关闭线程池 executorService.shutdown(); } catch (Exception e) { e.printStackTrace(); } } } ``` 在上述示例代码中,我们使用了Java的`ExecutorService`和`Runnable`接口来创建一个固定大小的线程池,并提交多个写入任务。每个任务都是独立的,负责向HBase写入一行数据。 通过使用多线程和批量写入,可以并行地向HBase写入多个数据行,从而提高写入效率。请根据实际情况调整线程池大小和批量写入数据量。记得根据需要设置适当的HBase连接参数和表信息。 需要注意的是,多线程写入时可能会对HBase集群产生较大的负载,请确保集群的硬件资源和网络带宽足够支持高并发的写入操作。此外,还要考虑表的预分区策略、RegionServer的负载均衡等因素,以避免潜在的性能问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值