HBaseAPI详解
1 HBaseAPI
1.1 获取Configuration对象
public static Configuration conf; static{ //使用HBaseConfiguration的单例方法实例化 conf = ate(); conf.set("keeper.quorum", "192.168.9.102"); conf.set("keeper.property.clientPort", "2181"); } |
1.2 判断表是否存在
public static boolean isTableExist(String tableName) throws MasterNotRunningException, ZooKeeperConnectionException, IOException{ //在HBase中管理、访问表需要先创建HBaseAdmin对象 //Connection connection = ateConnection(conf); //HBaseAdmin admin = (HBaseAdmin) Admin(); HBaseAdmin admin = new HBaseAdmin(conf); return admin.tableExists(tableName); } |
1.3 创建表
public static void createTable(String tableName, columnFamily) throws MasterNotRunningException, ZooKeeperConnectionException, IOException{ HBaseAdmin admin = new HBaseAdmin(conf); //判断表是否存在 if(isTableExist(tableName)){ System.out.println("表" + tableName + "已存在"); //it(0); }else{ //创建表属性对象,表名需要转字节 HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName)); //创建多个列族 for(String cf : columnFamily){ descriptor.addFamily(new HColumnDescriptor(cf)); } //根据对表的配置,创建表 ateTable(descriptor); System.out.println("表" + tableName + "创建成功!"); } } |
1.4 删除表
public static void dropTable(String tableName) throws MasterNotRunningException, ZooKeeperConnectionException, IOException{ HBaseAdmin admin = new HBaseAdmin(conf); if(isTableExist(tableName)){ admin.disableTable(tableName); admin.deleteTable(tableName); System.out.println("表" + tableName + "删除成功!"); }else{ System.out.println("表" + tableName + "不存在!"); } } |
1.5 向表中插入数据
public static void addRowData(String tableName, String rowKey, String columnFamily, String column, String value) throws IOException{ //创建HTable对象 HTable hTable = new HTable(conf, tableName); //向表中插入数据 Put put = new Bytes(rowKey)); //向Put对象中组装数据 put.Bytes(columnFamily), Bytes(column), Bytes(value)); hTable.put(put); hTable.close(); System.out.println("插入数据成功"); } |
1.6 删除多行数据
public static void deleteMultiRow(String tableName, rows) throws IOException{ HTable hTable = new HTable(conf, tableName); List<Delete> deleteList = new ArrayList<Delete>(); for(String row : rows){ Delete delete = new Bytes(row)); deleteList.add(delete); } hTable.delete(deleteList); hTable.close(); } |
drop table if exists admin |
1.7 获取所有数据
public static void getAllRows(String tableName) throws IOException{ HTable hTable = new HTable(conf, tableName); //得到用于扫描region的对象 Scan scan = new Scan(); //使用HTable得到resultcanner实现类的对象 ResultScanner resultScanner = Scanner(scan); for(Result result : resultScanner){ Cell[] cells = result.rawCells(); for(Cell cell : cells){ //得到rowkey System.out.println("行键:" + String(CellUtil.cloneRow(cell))); //得到列族 System.out.println("列族" + String(CellUtil.cloneFamily(cell))); System.out.println("列:" + String(CellUtil.cloneQualifier(cell))); System.out.println("值:" + String(CellUtil.cloneValue(cell))); } } } |
1.8 获取某一行数据
public static void getRow(String tableName, String rowKey) throws IOException{ HTable table = new HTable(conf, tableName); Get get = new Bytes(rowKey)); //get.setMaxVersions();显示所有版本 //get.setTimeStamp();显示指定时间戳的版本 Result result = (get); for(Cell cell : result.rawCells()){ System.out.println("行键:" + Row())); System.out.println("列族" + String(CellUtil.cloneFamily(cell))); System.out.println("列:" + String(CellUtil.cloneQualifier(cell))); System.out.println("值:" + String(CellUtil.cloneValue(cell))); System.out.println("时间戳:" + Timestamp()); } } |
1.9 获取某一行指定“列族:列”的数据
public static void getRowQualifier(String tableName, String rowKey, String family, String qualifier) throws IOException{ HTable table = new HTable(conf, tableName); Get get = new Bytes(rowKey)); get.Bytes(family), Bytes(qualifier)); Result result = (get); for(Cell cell : result.rawCells()){ System.out.println("行键:" + Row())); System.out.println("列族" + String(CellUtil.cloneFamily(cell))); System.out.println("列:" + String(CellUtil.cloneQualifier(cell))); System.out.println("值:" + String(CellUtil.cloneValue(cell))); } } |
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论