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.client.Delete;
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;
public class HBaseApp {
private static final String TABLE_NAME = "xxc";
private static final String FAMILY_NAME = "family1";
private static final String ROW_KEY = "rowkey1";
// 创建表、删除表、插入记录、查询一条记录、遍历所有记录
public static void main(String[] args) throws IOException {
// 配置信息
Configuration conf = HBaseConfiguration.create();
// 设置HBASE在HDFS上的存储路径
conf.set("hbase.rootdir", "hdfs://xxc:9000/hbase");
// 使用eclipse时必须添加这个,否则无法定位 使用zookepper定位HBASE
conf.set("hbase.zookeeper.quorum", "xxc");
// 1.创建表、删除表使用HBaseAdmin
HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
// 如果表不存在则创建表
createTable(hBaseAdmin);
// 删除表
// deleteTable(hBaseAdmin);
// 2.插入记录、查询一条记录、遍历所有记录 使用HTable
HTable hTable = new HTable(conf, TABLE_NAME);
deleteRecord(hTable);//删除一条记录
//putRecord(hTable);//插入一条记录
// getRecord(hTable);//获取一条记录
// scanTable(hTable);//遍历整张表
hTable.close();
}
private static void deleteRecord(HTable hTable) throws IOException {
Delete delete = new Delete(ROW_KEY.getBytes());//参数是行键
delete.deleteColumn(FAMILY_NAME.getBytes(), "age".getBytes());//参数是列族,列名
hTable.delete(delete);
}
/**
* 遍历整张表
*/
private static void scanTable(HTable hTable) throws IOException {
Scan scan = new Scan();
ResultScanner scanner = hTable.getScanner(scan);
for (Result result : scanner) {
byte[] value = result.getValue(FAMILY_NAME.getBytes(), "age".getBytes());//列族,列名
System.out.println(new String(value));
}
}
/**
* 获取一条记录
*/
private static void getRecord(HTable hTable) throws IOException {
Get get = new Get(ROW_KEY.getBytes());//参数是行键
Result result = hTable.get(get);
byte[] value = result.getValue(FAMILY_NAME.getBytes(), "age".getBytes());//列族,列名
System.out.println(result + "\t" + new String(value));
}
/**
* 插入一条记录
*/
private static void putRecord(HTable hTable) throws IOException {
Put put = new Put(ROW_KEY.getBytes());//参数是行键
put.add(FAMILY_NAME.getBytes(), "age".getBytes(), "20".getBytes());//设置列族,列名,值
hTable.put(put);
}
/**
* 删除表
*/
private static void deleteTable(HBaseAdmin hBaseAdmin) throws IOException {
hBaseAdmin.disableTable(TABLE_NAME);// 删除表之前需要禁用表,否则删除失败
hBaseAdmin.deleteTable(TABLE_NAME);
}
/**
* 添加表
*/
private static void createTable(HBaseAdmin hBaseAdmin) throws IOException {
if (!hBaseAdmin.tableExists(TABLE_NAME)) {
HTableDescriptor hd = new HTableDescriptor(TABLE_NAME);// 实参是表名称
HColumnDescriptor family = new HColumnDescriptor(FAMILY_NAME);//创建列族 实参是列族名称
hd.addFamily(family);//将列族添加到表上
hBaseAdmin.createTable(hd);
}
}
}
用JAVA的API操作HBASE
最新推荐文章于 2024-03-12 17:08:44 发布