hbase api

Configuration config = HbaseConfiguration.create();  // 创建一个Hbase的实例  
HbaseAdmin admin = new HbaseAdmin(config);           // 获取一个Hbase实例的句柄  
HTableDescriptor htd = new HTableDescriptor("tableName");// 创建一个表名  
HColumnDescriptor hcd = new HColumnDescriptor("columnCell"); //创建一个列簇  
hcd.addFamily(htd); //把列簇放到表中  
admin.createTable(htd);//创建一个表  
byte [] tableName= htd.getName(); // 把表名转成一个byte 型  
HTableDescriptor [] tables = admin.listTables(); //获取此Hbase实例句柄下所有的表名  
//如果此Hbase 下有表,而且,和需要创建的表名一样,创建失败,抛出异常。  
if(tables.length!=1 && Bytes.equals(tableName,tables[0].getName())){   
  throw new IOException("that table is exits,created faild");  
}  
HTable table = new HTable (config.tableName);  // 从Hbase句柄下获取指定的表名  
byte [] row1 = Bytes.toBytes("row1");   
Put p1 = new Put(row1); // 创建一个key 为row1 的值  
byte [] dataBytes = Bytes.toBytes("data"); //表名下创建的列簇  
//把列名为data:1,值为values1 的值 放到一个key中  
p1.add(dataBytes,Bytes.toBytes("1"),Bytes.toBytes("values1"));  
table.put(p1); //把key 放到table 中   
Get g = new Get(row1);//创建一个键值  
Result result = table.get(g); //表通过键值 获取一条记录  
System.out.pringtln("get:"+result);//把记录打印出来  
Scan scan = new Scan();    // 创建一个扫描指针  
ResultResult scanner = table.getScanner(scan);//表名通过指定来扫描每一条记录  
for(Result scannerResult:scanner){  
  System.out.println("scan:"+scannerResult);  
}  
scanner.close();// 关闭 扫描 ,释放资源  
admin.disableTable(tableName);//把表设为不可用  
admin.deleteTable(tableName);//删除表  
### HBase API 编程入门 #### 使用Java进行HBase操作 对于希望利用 Java 对 HBase 进行编程的开发者来说,官方提供的 `org.apache.hadoop.hbase.client` 包提供了必要的工具来创建表、插入数据以及执行查询等基本功能[^1]。 下面是一个简单的例子展示如何连接到 HBase 并向其中写入一些测试数据: ```java 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.Put; import org.apache.hadoop.hbase.client.Table; public class HBaseExample { public static void main(String[] args) throws Exception { Configuration config = HBaseConfiguration.create(); Connection connection = ConnectionFactory.createConnection(config); Table table = connection.getTable(TableName.valueOf("test_table")); Put put = new Put(Bytes.toBytes("row_key_001")); put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("qualifier"), Bytes.toBytes("value")); table.put(put); System.out.println("Data inserted successfully."); table.close(); connection.close(); } } ``` 这段代码展示了怎样建立与 HBase 的连接并往指定表格内添加一条记录。需要注意的是,在实际应用中应当处理好异常情况,并合理配置参数以适应不同的环境需求。 #### 利用Thrift接口访问HBase 除了直接通过 Java API 访问外,还可以借助 Apache Thrift 来构建跨语言的应用程序。这涉及到对 `hbase.thrift` 文件的理解及其编译过程,从而使得其他支持的语言也能方便地调用 HBase 提供的服务[^2]。 例如,如果想要开发基于 Python 的应用程序,则可以按照如下方式安装依赖项并与 HBase 交互: ```bash thrift --gen py hbase.thrift pip install . ``` 之后就可以编写类似于这样的Python脚本来读取或修改数据库中的内容了: ```python from thrift.transport import TSocket from thrift.transport import TTransport from thrift.protocol import TBinaryProtocol from hbase import THBaseService from hbase.ttypes import * transport = TSocket.TSocket('localhost', 9090) transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = THBaseService.Client(protocol) transport.open() # Your operations here... transport.close() ``` 此部分仅作为概念性的介绍;具体实现细节会依据所使用的编程语言而有所不同。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值