HBASE API

package org.jumore.test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

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.KeyValue;
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;
import org.apache.hadoop.hbase.util.Bytes;

public class HBaseTest {

// 声明静态配置
private static Configuration conf = null;

static {
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "192.168.1.141,192.168.1.143,192.168.1.145");
// conf.set("hbase.zookeeper.quorum", "192.168.23.31,192.168.23.33,192.168.23.34");
// conf.set("hbase.zookeeper.quorum", "172.18.203.113,172.18.203.114,172.18.203.115,172.18.203.116");
conf.set("hbase.zookeeper.property.clientPort", "2181");
}

//判断表是否存在
private static boolean isExist(String tableName) throws IOException {
HBaseAdmin hAdmin = new HBaseAdmin(conf);
return hAdmin.tableExists(tableName);
}

// 创建数据库表
public static void createTable(String tableName, String[] columnFamilys)
throws Exception {
// 新建一个数据库管理员
HBaseAdmin hAdmin = new HBaseAdmin(conf);
if (hAdmin.tableExists(tableName)) {
System.out.println("表 "+tableName+" 已存在!");
System.exit(0);
} else {
// 新建一个students表的描述
HTableDescriptor tableDesc = new HTableDescriptor(tableName);
// 在描述里添加列族
for (String columnFamily : columnFamilys) {
tableDesc.addFamily(new HColumnDescriptor(columnFamily));
}
// 根据配置好的描述建表
hAdmin.createTable(tableDesc);
System.out.println("创建表 "+tableName+" 成功!");
}
}

// 删除数据库表
public static void deleteTable(String tableName) throws Exception {
// 新建一个数据库管理员
HBaseAdmin hAdmin = new HBaseAdmin(conf);
if (hAdmin.tableExists(tableName)) {
// 关闭一个表
hAdmin.disableTable(tableName);
hAdmin.deleteTable(tableName);
System.out.println("删除表 "+tableName+" 成功!");
} else {
System.out.println("删除的表 "+tableName+" 不存在!");
System.exit(0);
}
}

// 添加一条数据
public static void addRow(String tableName, String row,
String columnFamily, String column, String value) throws Exception {
HTable table = new HTable(conf, tableName);
Put put = new Put(Bytes.toBytes(row));// 指定行
// 参数分别:列族、列、值
put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column),
Bytes.toBytes(value));
table.put(put);

}

// 删除一条(行)数据
public static void delRow(String tableName, String row) throws Exception {
HTable table = new HTable(conf, tableName);
Delete del = new Delete(Bytes.toBytes(row));
table.delete(del);
}

// 删除多条数据
public static void delMultiRows(String tableName, String[] rows)
throws Exception {
HTable table = new HTable(conf, tableName);
List<Delete> delList = new ArrayList<Delete>();
for (String row : rows) {
Delete del = new Delete(Bytes.toBytes(row));
delList.add(del);
}
table.delete(delList);
}

// 获取一条数据
public static void getRow(String tableName, String row) throws Exception {
HTable table = new HTable(conf, tableName);
Get get = new Get(Bytes.toBytes(row));
Result result = table.get(get);
// 输出结果,raw方法返回所有keyvalue数组
for (KeyValue rowKV : result.raw()) {
System.out.print("行名:" + new String(rowKV.getRow()) + " ");
System.out.print("时间戳:" + rowKV.getTimestamp() + " ");
System.out.print("列族名:" + new String(rowKV.getFamily()) + " ");
System.out.print("列名:" + new String(rowKV.getQualifier()) + " ");
System.out.println("值:" + new String(rowKV.getValue()));
}
}

// 获取所有数据
public static void getAllRows(String tableName) throws Exception {
HTable table = new HTable(conf, tableName);
Scan scan = new Scan();
ResultScanner results = table.getScanner(scan);
// 输出结果
for (Result result : results) {
for (KeyValue rowKV : result.raw()) {
// System.out.print("行名:" + new String(rowKV.getRow()) + " ");
// System.out.print("时间戳:" + rowKV.getTimestamp() + " ");
// System.out.print("列族名:" + new String(rowKV.getFamily()) + " ");
// System.out
// .print("列名:" + new String(rowKV.getQualifier()) + " ");
// System.out.println("值:" + new String(rowKV.getValue()));
String putStr = "put " + "\""+tableName+"\"" + ", " + "\"" +new String(rowKV.getRow())+ "\"" + ", " + "\""+ new String(rowKV.getFamily()) + ":" + new String(rowKV.getQualifier()) + "\""+ ", "+ "\"" + new String(rowKV.getValue())+ "\"";
System.out.println(putStr);
}
}
}

// 主函数
// public static void main(String[] args) {
// try {
// String tableName = "huym:student";
// // 第一步:创建数据库表:“student”
// String[] columnFamilys = { "info", "course" };
// HBaseTest.createTable(tableName, columnFamilys);
// // 第二步:向数据表的添加数据
// // 添加第一行数据
// if (isExist(tableName)) {
// HBaseTest.addRow(tableName, "zpc", "info", "age", "20");
// HBaseTest.addRow(tableName, "zpc", "info", "sex", "boy");
// HBaseTest.addRow(tableName, "zpc", "course", "china", "97");
// HBaseTest.addRow(tableName, "zpc", "course", "math", "128");
// HBaseTest.addRow(tableName, "zpc", "course", "english", "85");
//
// HBaseTest.addRow(tableName, "niaopeng", "course", "english","99");
// // 第三步:获取一条数据
// System.out.println("**************获取一条(zpc)数据*************");
// HBaseTest.getRow(tableName, "zpc");
// // 第四步:获取所有数据
// System.out.println("**************获取所有数据***************");
// HBaseTest.getAllRows(tableName);
//
// // 第五步:删除一条数据
// System.out.println("************删除一条(zpc)数据************");
// HBaseTest.delRow(tableName, "zpc");
// HBaseTest.getAllRows(tableName);
// // 第六步:删除多条数据
// System.out.println("**************删除多条数据***************");
// String rows[] = new String[] { "qingqing","xiaoxue" };
// HBaseTest.delMultiRows(tableName, rows);
// HBaseTest.getAllRows(tableName);
// // 第七步:删除数据库
// System.out.println("***************删除数据库表**************");
// HBaseTest.deleteTable(tableName);
// System.out.println("表"+tableName+"存在吗?"+isExist(tableName));
// } else {
// System.out.println(tableName + "此数据库表不存在!");
// }
//
// } catch (Exception e) {
// e.printStackTrace();
// }
// }



public static void main(String[] args) {
try {
String tableName = "jmnx:transaction";


// 添加第一行数据
if (isExist(tableName)) {

// 第三步:获取一条数据
//System.out.println("**************获取一条(zpc)数据*************");
//HBaseTest.getRow(tableName, "platform");
// 第四步:获取所有数据
// System.out.println("**************获取所有数据***************");
HBaseTest.getAllRows(tableName);


} else {
System.out.println(tableName + "此数据库表不存在!");
}

} catch (Exception e) {
e.printStackTrace();
}
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值