关于hbase增删改查的操作(通过eclipse)
1.建立java project。
2.引入hbase的jar包,去除重复的jar包以免出错。
3.书写test测试增删改查
private static Configuration conf =null;
/**
* 初始化配置
*/
static {
conf = HBaseConfiguration.create();
//设置zookeeper的地址
conf.set("hbase.zookeeper.quorum", "192.168.1.11");
//设置zookeeper的端口
conf.set("hbase.zookeeper.property.clientPort", "2181");
}
/**
* 创建表
* @param tableName 表名
* @param familys 列名
* @throws Exception
*/
public static void creatTable(String tableName, String[]familys) throws Exception {
//创建hbaseadmin对象
HBaseAdmin admin = new HBaseAdmin(conf);
//判断是否存在tableName的表名
if (admin.tableExists(tableName)) {
System.out.println("table already exists!");
} else {
HTableDescriptor tableDesc = new HTableDescriptor(tableName);
for(int i=0; i<familys.length; i++){
HColumnDescriptorcolumnDescriptor = newHColumnDescriptor(familys[i]);
// columnDescriptor.setInMemory(true); //将表放到regionserver缓存中,保证读取时候被cache命中
columnDescriptor.setMaxVersions(Integer.MAX_VALUE); //创建表时给予最大保留版本数,最大值为integer.max_value
// columnDescriptor.setTimeToLive(2*24*60*60); //设置表中数据存储时间 此为2天后删除
tableDesc.addFamily(columnDescriptor); //table添加这一列
}
admin.createTable(tableDesc); //创建table
System.out.println("create table " + tableName + " ok.");
}
admin.close();
}
/**
* 根据表名得到表 为了如同插入只是打开一次不然会造成线程不够,使用后如果间隔时间很长请手动关闭table
* @param tableName
* @return
*/
public static HTable openTable(String tableName){
try {
HTabletable = new HTable(conf, tableName);
return table;
} catch (MasterNotRunningException e) {
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 删除表
* @param tableName 表名
* 删除表必须经过2步操作先锁定让其不能使用diableTable 在进行deleteTable ,
* 不能直接进行delete操作此处已写好,如进行shell操作应按照步骤执行
* 在说明删除表重新创建一个相同表语句为:truncate '表名'
*/
public static void deleteTable(String tableName) throws Exception {
try {
HBaseAdmin admin = new HBaseAdmin(conf);
admin.disableTable(tableName);
admin.deleteTable(tableName);
System.out.println("delete table " + tableName + " ok.");
} catch (MasterNotRunningException e) {
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
e.printStackTrace();
}
}
/**
* 插入一条记录
* @param table table对象
* @param tableName 表名
* @param rowKey 标识列
* @param family 列镞
* @param qualifier (如列在分子项:例--列为:column 列族为:column:art;column:math)
* @param value 列值
* 这种方法为最简单单条插入单条提交,如果频繁会影响效率
*/
public static void insertRecord(HTable table,StringtableName, String rowKey, String family, String qualifier, String value)
throws Exception{
try {
Put put = new Put(Bytes.toBytes(rowKey));
put.add(Bytes.toBytes(family),Bytes.toBytes(qualifier),Bytes.toBytes(value));
table.put(put);
System.out.println("insert recored " + rowKey + " to table " + tableName +" ok.");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 删除一条记录
* @param table table对象
* @param tableName 表名
* @param rowKey 标识列
*/
public static void delRecord (HTable table,String tableName,String rowKey) throws IOException{
List list = new ArrayList();
Delete del = new Delete(rowKey.getBytes());
list.add(del);
table.delete(list);
System.out.println("del recored " + rowKey + " ok.");
}
/**
* 查询一条记录通过标识列
* @param table table对象
* @param tableName 表名
* @param rowKey 标识列
*/
public static void getOneRecord (HTable table,String tableName,String rowKey) throws IOException{
Get get = new Get(rowKey.getBytes());
Result rs = table.get(get);
List<KeyValue> list = rs.list();
for(KeyValue kv : rs.raw()){
System.out.print(new String(kv.getRow()) + "--" ); //标识列
System.out.print(new String(kv.getFamily())+ ":" ); //列族
System.out.print(new String(kv.getQualifier())+ "--" ); //列
System.out.print(kv.getTimestamp() + "--" ); //系统时间
System.out.println(new String(kv.getValue())); //值
}
}
/**
* get方式,通过rowKey、column查询
* @param tablename 表名
* @param rowKey rowKey
* @param column 列镞
* @param qualifier 子列
* @throws IOException
*/
public static void selectByRowKeyColumn(HTabletable,String tablename,String rowKey,String column,String qualifier) throws IOException{
Get g = new Get(Bytes.toBytes(rowKey));
g.addColumn(Bytes.toBytes(column),Bytes.toBytes(qualifier));
Result r=table.get(g);
for(KeyValue kv:r.raw()){
System.out.print(new String(kv.getRow()) + "--" );
System.out.print(new String(kv.getFamily())+ ":" );
System.out.print(new String(kv.getQualifier())+ "--" );
System.out.print(kv.getTimestamp() + "--" );
System.out.println(new String(kv.getValue()));
}
}
/**
* 根据列列族 value属性得到对应集合
* @param table table对象
* @param tablename 表名
* @param arr List集合 String---表示列,列族,值
* @throws IOException
*/
public static void selectByFilter(HTable table, Stringtablename,
List<String> arr) throws IOException {
FilterList filterList = new FilterList();
Scan s1 = new Scan();
for (String v : arr) { // 各个条件之间是“与”的关系
String[] s = v.split(",");
filterList.addFilter(new SingleColumnValueFilter(Bytes
.toBytes(s[0]), Bytes.toBytes(s[1]),CompareOp.EQUAL, Bytes
.toBytes(s[2])));
// 添加下面这一行后,则只返回指定的cell,同一行中的其他cell不返回
// s1.addColumn(Bytes.toBytes(s[0]),Bytes.toBytes(s[1]));
}
s1.setFilter(filterList);
ResultScanner ResultScannerFilterList =table.getScanner(s1);
for (Result rr = ResultScannerFilterList.next();rr != null; rr = ResultScannerFilterList
.next()) {
for (KeyValue kv : rr.list()){
// System.out.println("row :" + new String(kv.getRow()));
// System.out.println("value :" + new String(kv.getValue()));
}
}
}
/**
* 根据开始结束rowkey获取对应的集合
* @param table table对象
* @param startRow 开始行
* @param stopRow 结束行
*/
public static void selectByStratAndStopRow(HTabletable,String startRow,String stopRow){
try {
Scan scan = new Scan();
//设置开始数
scan.setStartRow(Bytes.toBytes(startRow));
//设置结束数
scan.setStopRow(Bytes.toBytes(stopRow));
ResultScanner rs =table.getScanner(scan);
int i = 0;
for(Result r:rs){
i+=r.size();
for(KeyValue kv : r.raw()){
System.out.print(new String(kv.getRow()) + " ");
System.out.print(new String(kv.getFamily())+ ":");
System.out.print(new String(kv.getQualifier())+ " ");
System.out.print(kv.getTimestamp() + " ");
System.out.println(new String(kv.getValue()));
}
}
System.out.println(i);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 显示所有
* @param table table对象
* @param tableName 表名
*/
public static void getAllRecord (HTable table,StringtableName) {
try{
Scan s = new Scan();
ResultScanner ss =table.getScanner(s);
int i = 0;
for(Result r:ss){
i+=r.size();
// for(KeyValue kv :r.raw()){
// System.out.print(newString(kv.getRow()) + " ");
// System.out.print(newString(kv.getFamily()) + ":");
// System.out.print(newString(kv.getQualifier()) + " ");
// System.out.print(kv.getTimestamp()+ " ");
// System.out.println(newString(kv.getValue()));
// }
}
System.out.println(i);
} catch (IOException e){
e.printStackTrace();
}
}