HBase 基本操作与api

编程入门 行业动态 更新时间:2024-10-09 07:22:56

基本操作

1.进入HBase客户端命令行
[atguigu@cm1 hbase]$ bin/hbase shell
2.查看帮助命令
hbase(main):001:0> help
3.查看当前数据库中有哪些表
hbase(main):002:0> list

表的操作

1.创建表
hbase(main):002:0> create ‘student’,‘info’
2.插入数据到表
hbase(main):003:0> put ‘student’,‘1001’,‘info:sex’,‘male’
hbase(main):004:0> put ‘student’,‘1001’,‘info:age’,‘18’
hbase(main):005:0> put ‘student’,‘1002’,‘info:name’,‘Janna’
hbase(main):006:0> put ‘student’,‘1002’,‘info:sex’,‘female’
hbase(main):007:0> put ‘student’,‘1002’,‘info:age’,‘20’
3.扫描查看表数据
hbase(main):008:0> scan ‘student’
hbase(main):009:0> scan ‘student’,{STARTROW => ‘1001’, STOPROW => ‘1001’}
hbase(main):010:0> scan ‘student’,{STARTROW => ‘1001’}
4.查看表结构
hbase(main):011:0> describe ‘student’
5.更新指定字段的数据
hbase(main):012:0> put ‘student’,‘1001’,‘info:name’,‘Nick’
hbase(main):013:0> put ‘student’,‘1001’,‘info:age’,‘100’
6.查看“指定行”或“指定列族:列”的数据
hbase(main):014:0> get ‘student’,‘1001’
hbase(main):015:0> get ‘student’,‘1001’,‘info:name’
7.统计表数据行数
hbase(main):021:0> count ‘student’
8.删除数据
删除某rowkey的全部数据:
hbase(main):016:0> deleteall ‘student’,‘1001’
删除某rowkey的某一列数据:
hbase(main):017:0> delete ‘student’,‘1002’,‘info:sex’
9.清空表数据
hbase(main):018:0> truncate ‘student’
提示:清空表的操作顺序为先disable,然后再truncate。
10.删除表
首先需要先让该表为disable状态:
hbase(main):019:0> disable ‘student’
然后才能drop这个表:
hbase(main):020:0> drop ‘student’
提示:如果直接drop表,会报错:ERROR: Table student is enabled. Disable it first.
11.变更表信息
将info列族中的数据存放3个版本:
hbase(main):022:0> alter ‘student’,{NAME=>‘info’,VERSIONS=>3}
hbase(main):022:0> get ‘student’,‘1001’,{COLUMN=>‘info:name’,VERSIONS=>3}

基本api (java)

pom.xml

	<dependency><groupId>net.sf.json-lib</groupId><artifactId>json-lib</artifactId><version>2.4</version><classifier>jdk15</classifier></dependency><!-- mvnrepository./artifact/.apache.httpponents/httpcore --><dependency><groupId>.apache.httpponents</groupId><artifactId>httpcore</artifactId><version>4.4.10</version></dependency><!-- mvnrepository./artifact/.apache.httpponents/httpclient --><dependency><groupId>.apache.httpponents</groupId><artifactId>httpclient</artifactId><version>4.5.6</version></dependency><dependency><groupId>.apache.axis</groupId><artifactId>axis</artifactId><version>1.4</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><dependency><groupId>.apache.kylin</groupId><artifactId>kylin-jdbc</artifactId><version>2.2.0</version></dependency><dependency><groupId>.slf4j</groupId><artifactId>slf4j-nop</artifactId><version>1.7.2</version></dependency><dependency><groupId>net.iharder</groupId><artifactId>base64</artifactId><version>2.3.8</version></dependency>
package .zzti.Hbase01;import java.io.IOException;
import java.util.ArrayList;
import java.util.List;import .apache.hadoop.conf.Configuration;
import .apache.hadoop.hbase.Cell;
import .apache.hadoop.hbase.CellUtil;
import .apache.hadoop.hbase.HBaseConfiguration;
import .apache.hadoop.hbase.HColumnDescriptor;
import .apache.hadoop.hbase.HTableDescriptor;
import .apache.hadoop.hbase.MasterNotRunningException;
import .apache.hadoop.hbase.TableName;
import .apache.hadoop.hbase.ZooKeeperConnectionException;
import .apache.hadoop.hbase.client.Admin;
import .apache.hadoop.hbase.client.Connection;
import .apache.hadoop.hbase.client.ConnectionFactory;
import .apache.hadoop.hbase.client.Delete;
import .apache.hadoop.hbase.client.Get;
import .apache.hadoop.hbase.client.HBaseAdmin;
import .apache.hadoop.hbase.client.HTable;
import .apache.hadoop.hbase.client.Put;
import .apache.hadoop.hbase.client.Result;
import .apache.hadoop.hbase.client.ResultScanner;
import .apache.hadoop.hbase.client.Scan;
import .apache.hadoop.hbase.util.Bytes;public class HBaseapi {public static Configuration conf;static {// 使用HBaseConfiguration的单例方法实例化conf = HBaseConfiguration.create();conf.set("hbase.zookeeper.quorum", "222.22.91.81");// conf.set("hbase.zookeeper.property.clientPort", "2181");}/*** * @param tableName* @return boolean 判断表是否存在* @throws Exception*/public static boolean isTableExist(String tableName) throws IOException {// 在HBase中管理、访问表需要先创建HBaseAdmin对象// Connection connection = ConnectionFactory.createConnection(conf);// HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();HBaseAdmin admin = new HBaseAdmin(conf);return admin.tableExists(tableName);}/*** * @param tableName* @param columnFamily 创建表 创建列族* @throws Exception shell creat 'tablename' 'columnfamily'*/public static void createTable(String tableName, String... columnFamily) throws Exception {HBaseAdmin admin = new HBaseAdmin(conf);// 判断表是否存在if (isTableExist(tableName)) {System.out.println("表" + tableName + "已存在");// System.exit(0);} else {// 创建表属性对象,表名需要转字节HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));// 创建多个列族for (String cf : columnFamily) {descriptor.addFamily(new HColumnDescriptor(cf));}// 根据对表的配置,创建表admin.createTable(descriptor);System.out.println("表" + tableName + "创建成功!");}}/*** * @param tableName 删除表* @throws MasterNotRunningException* @throws ZooKeeperConnectionException* @throws IOException                  shell(不可以直接删除) disable 'tablename' drop*                                      'tablename'*/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 + "不存在!");}}/*** * @param tableName      表名* @param rowKey         行建* @param columnFamily列族* @param column列* @param value值* @throws IOException shell put*                     'tablename','roowkey','columnfamily:coumn','value'*/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 Put(Bytes.toBytes(rowKey));// 向Put对象中组装数据put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));hTable.put(put);hTable.close();System.out.println("插入数据成功");}/*** * @param tableName 查看表的数据* @throws IOException shell scan 'tablename'*/public static void getAllRows(String tableName) throws IOException {HTable hTable = new HTable(conf, tableName);// 得到用于扫描region的对象Scan scan = new Scan();// 使用HTable得到resultcanner实现类的对象ResultScanner resultScanner = hTable.getScanner(scan);for (Result result : resultScanner) {Cell[] cells = result.rawCells();for (Cell cell : cells) {// 得到rowkeySystem.out.println("行键:" + Bytes.toString(CellUtil.cloneRow(cell)));// 得到列族System.out.println("列族" + Bytes.toString(CellUtil.cloneFamily(cell)));System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));}}}/*** * @param tableName* @param rowKey    根据行键查询,将行键所在的列的所有内容输出* @throws IOException shell get 'student','1001'*/public static void getRow(String tableName, String rowKey) throws IOException {HTable table = new HTable(conf, tableName);Get get = new Get(Bytes.toBytes(rowKey));// get.setMaxVersions();显示所有版本// get.setTimeStamp();显示指定时间戳的版本Result result = table.get(get);for (Cell cell : result.rawCells()) {System.out.println("行键:" + Bytes.toString(result.getRow()));System.out.println("列族" + Bytes.toString(CellUtil.cloneFamily(cell)));System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));System.out.println("时间戳:" + cell.getTimestamp());}}/*** * @param tableName* @param rowKey* @param family* @param qualifier 获取某一行指定“列族:列”的数据* @throws IOException shell get 'student','1001','info:age'*/public static void getRowQualifier(String tableName, String rowKey, String family, String qualifier)throws IOException {HTable table = new HTable(conf, tableName);Get get = new Get(Bytes.toBytes(rowKey));get.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));Result result = table.get(get);for (Cell cell : result.rawCells()) {System.out.println("行键:" + Bytes.toString(result.getRow()));System.out.println("列族" + Bytes.toString(CellUtil.cloneFamily(cell)));System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));}}/*** * @param tableName* @param rows      刪除表一行或多行刪除数据* @throws IOException*/public static void deleteMultiRow(String tableName, String... rows) throws IOException {HTable hTable = new HTable(conf, tableName);List<Delete> deleteList = new ArrayList<Delete>();for (String row : rows) {Delete delete = new Delete(Bytes.toBytes(row));deleteList.add(delete);}hTable.delete(deleteList);hTable.close();}public static void main(String[] args) {// TODO Auto-generated method stubtry {// System.out.println(isTableExist("demo"));// createTable("demo","frist");// dropTable("demo");// getAllRows("student");getRowQualifier("student", "1001", "info", "age");} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

更多推荐

操作,HBase,api

本文发布于:2023-05-28 00:46:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/308207.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:操作   HBase   api

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!