一.准备工作
首先创建一个Maven,选择 IDEA 所 关联的 Maven 配置文件(settings.xml)。
在pom.xml文件里面插入一段代码(改成自己的hbase版本)
<dependencies>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.4.11</version>
</dependency>
</dependencies>
添加配置文件 hbase-site.xml到resource里面
二.操作
-
建表
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; public class createTbale { public static void main(String[] args) throws IOException { Configuration configuration = HBaseConfiguration.create(); configuration.set("hbase.zookeeper.quorum","192.168.56.8"); Connection connection = ConnectionFactory.createConnection(configuration); Admin admin = null; { try { connection = ConnectionFactory.createConnection(configuration); System.out.println(connection); admin = connection.getAdmin(); System.out.println(admin); TableName tableName = TableName.valueOf("WATER_BILL"); // 2 构建表描述构建器 TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tableName); // 3 构建列簇描述构建器 ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("C1")); // 4 构建列蔟描述 ColumnFamilyDescriptor columnFamilyDescriptor = columnFamilyDescriptorBuilder.build(); // 5 构建表描述 tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor); // 5)定义表描述对象 添加列蔟 TableDescriptor tableDescriptor = tableDescriptorBuilder.build(); // 6)创建表 admin.createTable(tableDescriptor); } catch (IOException e) { e.printStackTrace(); } finally { assert admin != null; admin.close(); connection.close(); } } } }
-
添加数据
public class addTable { public static void putTable(Connection connection,String tableName,String C1,String columnFamily,String column,String value) throws IOException { Table table=connection.getTable(TableName.valueOf(tableName)); Put put=new Put(Bytes.toBytes(C1)); put.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(column),Bytes.toBytes(value)); table.put(put); table.close(); }
-
添加多个数据
public static void addListData() throws IOException{ Configuration configuration = HBaseConfiguration.create(); configuration.set("hbase.zookeeper.quorum","192.168.56.8"); Connection connection = ConnectionFactory.createConnection(configuration); List<Put> puts=new ArrayList<Put>(); Put haha=new Put(Bytes.toBytes("1")); haha.addColumn(Bytes.toBytes("C1"),Bytes.toBytes("ADDRESS"),Bytes.toBytes("贵州省铜仁市德江县7单元267室")); haha.addColumn(Bytes.toBytes("C1"),Bytes.toBytes("SEX"),Bytes.toBytes("男")); haha.addColumn(Bytes.toBytes("C1"),Bytes.toBytes("PAY_DATE"),Bytes.toBytes("2020-05-10")); haha.addColumn(Bytes.toBytes("C1"),Bytes.toBytes("NUM_CURRENT"),Bytes.toBytes("308.1")); haha.addColumn(Bytes.toBytes("C1"),Bytes.toBytes("NUM_PREVIOUS"),Bytes.toBytes("283.1")); haha.addColumn(Bytes.toBytes("C1"),Bytes.toBytes("NUM_USAGE"),Bytes.toBytes("25")); puts.add(haha); Table table=connection.getTable(TableName.valueOf("WATER_BILL")); table.put(puts); }
-
打印数据
public static void printData() throws IOException { Configuration configuration = HBaseConfiguration.create(); configuration.set("hbase.zookeeper.quorum","192.168.56.8"); Connection connection = ConnectionFactory.createConnection(configuration); TableName waterBillTableName = TableName.valueOf("WATER_BILL"); Table waterBilltable = connection.getTable(waterBillTableName); Get get = new Get(Bytes.toBytes("1")); Result result = waterBilltable.get(get); List<Cell> cellList = result.listCells(); System.out.println("rowkey => " + Bytes.toString(result.getRow())); for (Cell cell : cellList) { // 打印列蔟名 System.out.print(Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength())); System.out.println(" => " + Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); } waterBilltable.close(); }
-
删除数据
public static void deleteData() throws IOException { Configuration configuration = HBaseConfiguration.create(); configuration.set("hbase.zookeeper.quorum","192.168.56.8"); Connection connection = ConnectionFactory.createConnection(configuration); Table waterBillTable = connection.getTable(TableName.valueOf("WATER_BILL")); Delete delete = new Delete(Bytes.toBytes("1")); waterBillTable.delete(delete); waterBillTable.close(); }
-
删除表
public static boolean deleTable() throws IOException{ Configuration configuration = HBaseConfiguration.create(); configuration.set("hbase.zookeeper.quorum","192.168.56.8"); Connection connection = ConnectionFactory.createConnection(configuration); TableName tableName = TableName.valueOf("WATER_BILL"); Admin admin =connection.getAdmin(); if (admin.tableExists(tableName)){ admin.disableTable(tableName); admin.deleteTable(tableName); return true; }else { return false; } }
-
主函数
public static void main(String[] args) throws IOException { boolean result=deleTable(); if (result){ System.out.println("删除成功"); }else System.out.println("表不存在"); //删除数据 deleteData(); //打印数据 printData(); //添加多列数据 addListData(); Configuration configuration = HBaseConfiguration.create(); configuration.set("hbase.zookeeper.quorum","192.168.56.8"); Connection connection = ConnectionFactory.createConnection(configuration); Admin admin =connection.getAdmin(); //添加单个数据 putTable(admin.getConnection(),"WATER_BILL","1","C1","NAME","zhangsan"); admin.close(); }
三.遇到的问题
1.运行时间过长
解决措施
在/etc/hostname下面更改主机名和IP地址的映射关系
2. org.apache.hadoop.hbase.MasterNotRunningException
解决措施:打开C:\Windows\System32\drivers\etc目录,按住win+x,选择命令提示符(管理员),输入notepad,将打开记事本,选择文件下的打开,点击选择文件类型为所有文件类型,打开host文件,添加自己hadoop集群的IP地址和master信息,然后重新运行即可。