1.启动hadoop和hbase
2.关闭linux防火墙
3.java操作
3.1.导入/root/training/hbase-1.0.2/lib中所有的jar包到项目中
3.2 代码
package com.lanqiao;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@SuppressWarnings("all")
public class HBaseTest {
Configuration config = null;
HBaseAdmin admin = null;
TableName tableName = TableName.valueOf("test");
HTable table = null;
@Before
public void init(){
//加载配置文件
config = HBaseConfiguration.create();
//zookeeper端口
config.set("hbase.zookeeper.property.clientPort", "2181");
//zookeeper集群的地址
config.set("hbase.zookeeper.quorum", "192.168.0.107");
//hbase主节点
config.set("hbase.master", "192.168.0.107:6000");
}
@After
public void close(){
try {
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void createTable() {
//创建表
try {
//用于管理HBase集群,添加和丢弃表
admin = new HBaseAdmin(config);
//创建表描述符
HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
//创建列族描述符
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor("data");
//列族加入表
hTableDescriptor.addFamily(hColumnDescriptor);
//执行表的创建
admin.createTable(hTableDescriptor);
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void insert(){
//添加3条数据
try {
//获取表对象
table = new HTable(config,tableName);
for(int i=1;i<=3;i++){
//rowkey
byte[] row = Bytes.toBytes("row"+i);
Put put = new Put(row);
byte[] family =Bytes.toBytes("data");
byte[] qualifier =Bytes.toBytes(i);
byte[] value =Bytes.toBytes("value"+i);
//使用put对象添加一行记录
put.add(family, qualifier, value);
table.put(put);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void get(){
//根据rowKey查询一条记录
Get get = new Get(Bytes.toBytes("row1"));
try {
table = new HTable(config,tableName);
Result result = table.get(get);
System.out.println(result);
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void scan(){
//查询所有记录
Scan scan = new Scan();
try {
table = new HTable(config,tableName);
ResultScanner scanner = table.getScanner(scan);
for(Result result : scanner){
System.out.println(result);
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void dropTable(){
//删除表
try {
admin = new HBaseAdmin(config);
admin.disableTable(tableName);
admin.deleteTable(tableName);
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}