Hbase table之增删查
Posted Justin的后端书架
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hbase table之增删查相关的知识,希望对你有一定的参考价值。
概略步骤
1. 加载Hbase 关于hadoop的配置参数
2. 利用HTable类进行表名实例化
3. 进行相应的增删改操作
代码
import org.apache.commons.io.IOExceptionWithCause;
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.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Test;
import javax.ws.rs.core.Response;
import java.io.IOException;
import static org.apache.hadoop.hbase.util.Bytes.*;
public class PutExample {
//constant definition
public static final String colFam = "colfam1";
public static void main(String[] args) throws IOException {
//Configure the Hbase setting
Configuration conf = HBaseConfiguration.create();
//Hbase with hdfs setting
conf.set("hbase.rootdir", "hdfs://192.168.56.160:8020/hbase");
conf.set("hbase.zookeeper.quorum", "192.168.56.160");
//Create Htable instance then do the insert operation
HTable table = new HTable(conf, "test01");
Put put = new Put(toBytes("row1"));
put.add(Bytes.toBytes("colfam1"), toBytes("qual1"), toBytes("val1"));
put.add(Bytes.toBytes("colfam1"), toBytes("qual2"), toBytes("val2"));
table.put(put);
//Scan the table
System.out.println("Start to print out the table test01's content");
scanTable(table);
//Create new table for Hbase
final HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
createTable(hBaseAdmin);
//Delete the table,specify the row num,column family,column name
DeleteColumnData(table,"row1","colfam1","qual1");
}
public static void scanTable(final HTable hTableName) throws IOException {
Scan scan = new Scan();
final ResultScanner scanner = hTableName.getScanner(scan);
for (Result result : scanner) {
final byte[] value = result.getValue(colFam.getBytes(), "qual1".getBytes());
try {
System.out.println(result);
System.out.println(new String(value));
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void createTable(HBaseAdmin hBaseAdmin) throws IOException{
if(!hBaseAdmin.tableExists("test01"));
{
HTableDescriptor tableDescriptor = new HTableDescriptor("Test02");
//add col family
HColumnDescriptor family = new HColumnDescriptor("col_f02");
tableDescriptor.addFamily(family);
hBaseAdmin.createTable(tableDescriptor);
}
}
public static void DeleteColumnData (HTable table,String rowString,String colCFString,String colString) throws IOException{
Delete delete = new Delete(Bytes.toBytes(rowString));
//delete column
delete.deleteColumn(Bytes.toBytes(colCFString), Bytes.toBytes(colString));
delete.deleteFamily(Bytes.toBytes(colCFString));
//delete columns
Get get1 = new Get(Bytes.toBytes(rowString));
get1.addColumn(Bytes.toBytes(colCFString),Bytes.toBytes(colString));
if (get1.isCheckExistenceOnly() == true){
delete.deleteColumns(Bytes.toBytes("colfam2"), Bytes.toBytes("qual1"));
System.out.println("Get1: " + get1.isCheckExistenceOnly());
}
try {
table.delete(delete);
} catch (IOException e) {
e.printStackTrace();
}
table.close();
}
}
相关文章:
转载声明:本文转载自「Justin谈开发」,搜索「Justin1872116370」即可关注。
以上是关于Hbase table之增删查的主要内容,如果未能解决你的问题,请参考以下文章