余子越的博客
Toggle navigation
余子越的博客
主页
计算机网络
大数据分析
系统与工具
编程之路
容器引擎
作者
归档
标签
Hbase JAVA接口示例
2020-12-23 18:03:14
28
0
0
yuziyue
[TOC] # 一. 添加依赖 - 根据hbase的版本,添加相同版本的 hbase-client 客户端。 ``` <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>2.0.2</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-common</artifactId> <version>2.0.2</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-server</artifactId> <version>2.0.2</version> </dependency> ``` # 二. 配置文件 - 将安装Hbase的配置文件`hbase-site.xml`拷贝到`src/main/resources/`目录下,如果需要打包然后在其他服务器上跑的话,将该配置文件放置在`classpath`下即可。 # 三. 编写代码 - 编写代码并运行 `HbaseHello.java` ``` package com; import com.google.common.collect.Lists; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.Admin; import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor; import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.Get; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.client.TableDescriptor; import org.apache.hadoop.hbase.client.TableDescriptorBuilder; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Collections; import java.util.stream.Collectors; public class HbaseHello { public static void main(String[] args) throws Exception { Hbase hbase = new Hbase(); hbase.listTables(); hbase.dropTable(); hbase.createTable(); hbase.putRows(); hbase.scanRows(); hbase.showColumnFamilyNames(); hbase.getByRowKey("20201223-1"); hbase.closeHbase(); } } class Hbase { Configuration configuration; Connection connection; Admin admin; public Hbase() throws Exception { this.configuration = HBaseConfiguration.create(); this.connection = ConnectionFactory.createConnection(configuration); this.admin = connection.getAdmin(); } public void closeHbase() throws IOException { this.connection.close(); this.admin.close(); } /** * 创建table * * @throws IOException */ public void createTable() throws IOException { System.out.println("method: " + Thread.currentThread().getStackTrace()[1].getMethodName()); TableName tableName = TableName.valueOf("student"); if (admin.tableExists(tableName)) { System.err.println(new String(tableName.getName()) + "已存在"); return; } List<ColumnFamilyDescriptor> families = new ArrayList<>(); Collections.addAll(families, ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("grade")).build(), ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("course")).build(), ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("info")).build() ); TableDescriptor tableDescriptor = TableDescriptorBuilder .newBuilder(tableName) .setColumnFamilies(families) .build(); admin.createTable(tableDescriptor); System.out.println(); } /** * 查看所有列族名称 * * @throws IOException */ public void showColumnFamilyNames() throws IOException { System.out.println("method: " + Thread.currentThread().getStackTrace()[1].getMethodName()); TableName tableName = TableName.valueOf("student"); TableDescriptor tableDescriptor = this.admin.getDescriptor(tableName); for (byte[] b : tableDescriptor.getColumnFamilyNames()) { System.out.println(new String(b)); } System.out.println(); } /** * 扫描整个表 * * @throws IOException */ public void scanRows() throws IOException { System.out.println("method: " + Thread.currentThread().getStackTrace()[1].getMethodName()); TableName tableName = TableName.valueOf("student"); HTable hTable = (HTable) this.connection.getTable(tableName); ResultScanner scanner = hTable.getScanner(new Scan()); for (Result r : scanner) { for (Cell cell : r.listCells()) { printCell(cell); } } scanner.close(); System.out.println(); } /** * 打印cell * * @param cell */ public void printCell(Cell cell) { String rowKey = Bytes.toString(CellUtil.cloneRow(cell)); String columnFamily = Bytes.toString(CellUtil.cloneFamily(cell)); String columnCell = Bytes.toString(CellUtil.cloneQualifier(cell)); String value = Bytes.toString(CellUtil.cloneValue(cell)); System.out.println( String.join(",", new ArrayList<String>() {{ add(rowKey); add(columnFamily); add(columnCell); add(value); }})); } /** * 获取一行数据 * * @param rowKey rowKey * @throws IOException */ public void getByRowKey(String rowKey) throws IOException { System.out.println("method: " + Thread.currentThread().getStackTrace()[1].getMethodName()); TableName tableName = TableName.valueOf("student"); HTable hTable = (HTable) this.connection.getTable(tableName); Get get = new Get(Bytes.toBytes(rowKey)); Result result = hTable.get(get); Cell[] cells = result.rawCells(); for (Cell cell : cells) { this.printCell(cell); } System.out.println(); } /** * 删除表 * * @throws IOException */ public void dropTable() throws IOException { System.out.println("method: " + Thread.currentThread().getStackTrace()[1].getMethodName()); TableName tableName = TableName.valueOf("student"); if (!admin.tableExists(tableName)) { System.err.println(new String(tableName.getName()) + "不存在"); return; } this.admin.disableTable(TableName.valueOf("student")); this.admin.deleteTable(TableName.valueOf("student")); System.out.println(); } /** * 插入行 * * @throws IOException */ public void putRows() throws IOException { System.out.println("method: " + Thread.currentThread().getStackTrace()[1].getMethodName()); TableName tableName = TableName.valueOf("student"); HTable hTable = (HTable) this.connection.getTable(tableName); for (int i = 0; i < 4; i++) { Put put = new Put(Bytes.toBytes("20201223-" + i)); put.addColumn(Bytes.toBytes("course"), Bytes.toBytes("poker"), Bytes.toBytes("100")); put.addColumn(Bytes.toBytes("course"), Bytes.toBytes("phone"), Bytes.toBytes("101")); put.addColumn(Bytes.toBytes("course"), Bytes.toBytes("computer"), Bytes.toBytes("102")); hTable.put(put); } hTable.close(); System.out.println(); } /** * 打印所有表 * * @throws Exception */ public void listTables() throws Exception { System.out.println("method: " + Thread.currentThread().getStackTrace()[1].getMethodName()); List<TableDescriptor> allTable = this.admin.listTableDescriptors(); for (TableDescriptor hTableDescriptor : allTable) { System.out.println(hTableDescriptor.getTableName()); } System.out.println(); } } ``` <br><br><br>
上一篇:
Hadoop JAVA接口示例
下一篇:
Go语言之slice
0
赞
28 人读过
新浪微博
微信
腾讯微博
QQ空间
人人网
文档导航