|
| 1 | +四、HBase API操作 |
| 2 | +--- |
| 3 | +### 1、环境准备 |
| 4 | +  新建项目后在pom.xml中添加依赖: |
| 5 | +```xml |
| 6 | +<dependency> |
| 7 | + <groupId>org.apache.hbase</groupId> |
| 8 | + <artifactId>hbase-server</artifactId> |
| 9 | + <version>1.3.1</version> |
| 10 | +</dependency> |
| 11 | + |
| 12 | +<dependency> |
| 13 | + <groupId>org.apache.hbase</groupId> |
| 14 | + <artifactId>hbase-client</artifactId> |
| 15 | + <version>1.3.1</version> |
| 16 | +</dependency> |
| 17 | + |
| 18 | +<dependency> |
| 19 | + <groupId>jdk.tools</groupId> |
| 20 | + <artifactId>jdk.tools</artifactId> |
| 21 | + <version>1.8</version> |
| 22 | + <scope>system</scope> |
| 23 | + <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath> |
| 24 | +</dependency> |
| 25 | +``` |
| 26 | + |
| 27 | +### 2、HBase API |
| 28 | +1)获取Configuration对象 |
| 29 | +```java |
| 30 | +public static Configuration conf; |
| 31 | +static{ |
| 32 | + //使用HBaseConfiguration的单例方法实例化 |
| 33 | + conf = HBaseConfiguration.create(); |
| 34 | +conf.set("hbase.zookeeper.quorum", "192.168.9.102"); |
| 35 | +conf.set("hbase.zookeeper.property.clientPort", "2181"); |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +2)判断表是否存在 |
| 40 | +```java |
| 41 | +public static boolean isTableExist(String tableName) throws MasterNotRunningException, |
| 42 | + ZooKeeperConnectionException, IOException{ |
| 43 | + //在HBase中管理、访问表需要先创建HBaseAdmin对象 |
| 44 | +//Connection connection = ConnectionFactory.createConnection(conf); |
| 45 | +//HBaseAdmin admin = (HBaseAdmin) connection.getAdmin(); |
| 46 | + HBaseAdmin admin = new HBaseAdmin(conf); |
| 47 | + return admin.tableExists(tableName); |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +3)创建表 |
| 52 | +```java |
| 53 | +public static void createTable(String tableName, String... columnFamily) throws |
| 54 | + MasterNotRunningException, ZooKeeperConnectionException, IOException{ |
| 55 | + HBaseAdmin admin = new HBaseAdmin(conf); |
| 56 | + //判断表是否存在 |
| 57 | + if(isTableExist(tableName)){ |
| 58 | + System.out.println("表" + tableName + "已存在"); |
| 59 | + //System.exit(0); |
| 60 | + }else{ |
| 61 | + //创建表属性对象,表名需要转字节 |
| 62 | + HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName)); |
| 63 | + //创建多个列族 |
| 64 | + for(String cf : columnFamily){ |
| 65 | + descriptor.addFamily(new HColumnDescriptor(cf)); |
| 66 | + } |
| 67 | + //根据对表的配置,创建表 |
| 68 | + admin.createTable(descriptor); |
| 69 | + System.out.println("表" + tableName + "创建成功!"); |
| 70 | + } |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +4)删除表 |
| 75 | +```java |
| 76 | +public static void dropTable(String tableName) throws MasterNotRunningException, |
| 77 | + ZooKeeperConnectionException, IOException{ |
| 78 | + HBaseAdmin admin = new HBaseAdmin(conf); |
| 79 | + if(isTableExist(tableName)){ |
| 80 | + admin.disableTable(tableName); |
| 81 | + admin.deleteTable(tableName); |
| 82 | + System.out.println("表" + tableName + "删除成功!"); |
| 83 | + }else{ |
| 84 | + System.out.println("表" + tableName + "不存在!"); |
| 85 | + } |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +5)向表中插入数据向表中插入数据 |
| 90 | +```java |
| 91 | +public static void addRowData(String tableName, String rowKey, String columnFamily, String |
| 92 | + column, String value) throws IOException{ |
| 93 | + //创建HTable对象 |
| 94 | + HTable hTable = new HTable(conf, tableName); |
| 95 | + //向表中插入数据 |
| 96 | + Put put = new Put(Bytes.toBytes(rowKey)); |
| 97 | + //向Put对象中组装数据 |
| 98 | + put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value)); |
| 99 | + hTable.put(put); |
| 100 | + hTable.close(); |
| 101 | + System.out.println("插入数据成功"); |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +6)删除多行数据 |
| 106 | +```java |
| 107 | +public static void deleteMultiRow(String tableName, String... rows) throws IOException{ |
| 108 | + HTable hTable = new HTable(conf, tableName); |
| 109 | + List<Delete> deleteList = new ArrayList<Delete>(); |
| 110 | + for(String row : rows){ |
| 111 | + Delete delete = new Delete(Bytes.toBytes(row)); |
| 112 | + deleteList.add(delete); |
| 113 | + } |
| 114 | + hTable.delete(deleteList); |
| 115 | + hTable.close(); |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +7)获取所有数据 |
| 120 | +```java |
| 121 | +public static void getAllRows(String tableName) throws IOException{ |
| 122 | + HTable hTable = new HTable(conf, tableName); |
| 123 | + //得到用于扫描region的对象 |
| 124 | + Scan scan = new Scan(); |
| 125 | + //使用HTable得到resultcanner实现类的对象 |
| 126 | + ResultScanner resultScanner = hTable.getScanner(scan); |
| 127 | + for(Result result : resultScanner){ |
| 128 | + Cell[] cells = result.rawCells(); |
| 129 | + for(Cell cell : cells){ |
| 130 | + //得到rowkey |
| 131 | + System.out.println("行键:" + Bytes.toString(CellUtil.cloneRow(cell))); |
| 132 | + //得到列族 |
| 133 | + System.out.println("列族" + Bytes.toString(CellUtil.cloneFamily(cell))); |
| 134 | + System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell))); |
| 135 | + System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell))); |
| 136 | + } |
| 137 | + } |
| 138 | +} |
| 139 | +``` |
| 140 | + |
| 141 | +8)获取某一行数据 |
| 142 | +```java |
| 143 | +public static void getRow(String tableName, String rowKey) throws IOException{ |
| 144 | + HTable table = new HTable(conf, tableName); |
| 145 | + Get get = new Get(Bytes.toBytes(rowKey)); |
| 146 | + //get.setMaxVersions();显示所有版本 |
| 147 | + //get.setTimeStamp();显示指定时间戳的版本 |
| 148 | + Result result = table.get(get); |
| 149 | + for(Cell cell : result.rawCells()){ |
| 150 | + System.out.println("行键:" + Bytes.toString(result.getRow())); |
| 151 | + System.out.println("列族" + Bytes.toString(CellUtil.cloneFamily(cell))); |
| 152 | + System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell))); |
| 153 | + System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell))); |
| 154 | + System.out.println("时间戳:" + cell.getTimestamp()); |
| 155 | + } |
| 156 | +} |
| 157 | +``` |
| 158 | + |
| 159 | +9)获取某一行指定“列族:列”的数据 |
| 160 | +```java |
| 161 | +public static void getRowQualifier(String tableName, String rowKey, String family, String |
| 162 | + qualifier) throws IOException{ |
| 163 | + HTable table = new HTable(conf, tableName); |
| 164 | + Get get = new Get(Bytes.toBytes(rowKey)); |
| 165 | + get.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier)); |
| 166 | + Result result = table.get(get); |
| 167 | + for(Cell cell : result.rawCells()){ |
| 168 | + System.out.println("行键:" + Bytes.toString(result.getRow())); |
| 169 | + System.out.println("列族" + Bytes.toString(CellUtil.cloneFamily(cell))); |
| 170 | + System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell))); |
| 171 | + System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell))); |
| 172 | + } |
| 173 | +} |
| 174 | +``` |
| 175 | + |
| 176 | + |
| 177 | + |
| 178 | + |
| 179 | + |
| 180 | + |
| 181 | + |
| 182 | + |
| 183 | + |
| 184 | + |
| 185 | + |
| 186 | + |
| 187 | + |
| 188 | + |
| 189 | + |
| 190 | + |
| 191 | + |
| 192 | + |
| 193 | + |
| 194 | + |
0 commit comments