使用java语句对Hbase进行操作
package com.hbase;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
public class HbaseTest {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//读取配置
Configuration conf = new Configuration();
//设置配置
conf.set("fs.defaultFs", "hdfs://master:8020"); //hdfs 的地址
conf.set("hbase.zookeeper.property.clientPort", "2181"); //zookeeper 的端口号
conf.set("hbase.zookeeper.quorum", "master"); // zookeeper 的主节点 master
conf.set("hbase.master", "master:60000"); // hmaster的地址 master:60000
Connection conn = ConnectionFactory.createConnection(conf); //创建到hbase的连接
Admin admin = conn.getAdmin(); //创建管理员通道
HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("hbase_table_1")); //表名的描述类
HColumnDescriptor hcd = new HColumnDescriptor("c1"); //创建列族描述类
HColumnDescriptor hcd2 = new HColumnDescriptor("c2");
hcd.setVersions(1, 3); //设置版本号
hcd2.setVersions(1, 5);
htd.addFamily(hcd); //将列族添加到表里
htd.addFamily(hcd2);
admin.createTable(htd); //创建表
admin.close();
}
}