操作步骤:
创建Maven工程,需要导入的Pom文件如下
将虚拟主机当中的hbase-site.xml和hdfs-site.xml文件复制到项目下的classpath下
Hbase-site.xml
/usr/bigdata/hbase/hbase-1.3.1/conf
Hdfs-site.xml
/usr/bigdata/hadoop/hadoop-2.8.0/etc/hadoop
复制到项目的src/main/resources/下
框架:
修改windows真机的hosts文件,添加三台机器的映射
创建表
public static void createTable() throws Exception { // 获取Configuration对象 Configuration con = HBaseConfiguration.create(); // 创建HBaseAdmin对象是用来对表进行操作:包括创建 删除 HBaseAdmin admin = new HBaseAdmin(con); // 判断表是否存在 if (admin.tableExists("hbase_demo_api")) { System.out.println("表已经存在"); } else { // 表的描述 HTableDescriptor htable = new HTableDescriptor("hbase_demo_api"); htable.addFamily(new HColumnDescriptor("grand")); htable.addFamily(new HColumnDescriptor("course")); admin.createTable(htable); System.out.println("表创建成功"); } admin.close(); }
插入数据
public static void putData() throws IOException { Configuration con = HBaseConfiguration.create(); // 创建HTable句柄 HTable hTable = new HTable(con, "hbase_demo-api"); // 创建Put对象 Put put = new Put("dinghzongqiu".getBytes()); put.addColumn("course".getBytes(), "java".getBytes(), "98".getBytes()); put.addColumn("course".getBytes(), "sql".getBytes(), "80".getBytes()); put.addColumn("grand".getBytes(), "".getBytes(), "Y2".getBytes()); hTable.put(put); hTable.close(); }
获取表中的数据
public static void scanData() throws IOException { Configuration con = HBaseConfiguration.create(); // 创建HTable句柄 HTable hTable = new HTable(con, "hbase_demo-api"); ResultScanner scanner = hTable.getScanner(new Scan()); for (Result result : scanner) { for (Cell cell : result.rawCells()) { System.out.println("Row KEY:" + Bytes.toString(result.getRow())); System.out.println("列族:" + Bytes.toString(CellUtil.cloneFamily(cell))); System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell))); System.out.println("value:" + Bytes.toString(CellUtil.cloneValue(cell))); } } hTable.close(); }
获取指定ROWKey的数据
private static void getDataByRowKey() throws Exception { // TODO Auto-generated method stub Configuration con = HBaseConfiguration.create(); // 创建HTable句柄 HTable hTable = new HTable(con, "hbase_demo-api"); Get get = new Get(Bytes.toBytes("丁中秋")); Result result = hTable.get(get); for (Cell cell : result.rawCells()) { System.out.println("Row KEY:" + Bytes.toString(result.getRow())); System.out.println("列族:" + Bytes.toString(CellUtil.cloneFamily(cell))); System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell))); System.out.println("value:" + Bytes.toString(CellUtil.cloneValue(cell))); } hTable.close(); }
获取指定ROWKey的数据2
private static void getDataByRowKey2() throws Exception { // TODO Auto-generated method stub Configuration con = HBaseConfiguration.create(); // 创建HTable句柄 HTable hTable = new HTable(con, "hbase_demo-api"); Get get = new Get(Bytes.toBytes("丁中秋")); get.addColumn(Bytes.toBytes("course"), Bytes.toBytes("java")); Result result = hTable.get(get); for (Cell cell : result.rawCells()) { System.out.println("Row KEY:" + Bytes.toString(result.getRow())); System.out.println("列族:" + Bytes.toString(CellUtil.cloneFamily(cell))); System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell))); System.out.println("value:" + Bytes.toString(CellUtil.cloneValue(cell))); } hTable.close(); }
删除数据
public static void deleteData() throws Exception { // 获取Configuration对象 Configuration con = HBaseConfiguration.create(); // 创建HTable句柄 HTable hTable = new HTable(con, "hbase_demo-api"); Delete delete = new Delete(Bytes.toBytes("dingzhongqiu")); delete.addColumn(Bytes.toBytes("course"), Bytes.toBytes("java")); hTable.delete(delete); hTable.close(); }
删除表
public static void deleteTable() throws Exception { // 获取Configuration对象 Configuration con = HBaseConfiguration.create(); // 创建HBaseAdmin对象是用来对表进行操作:包括创建 删除 HBaseAdmin admin = new HBaseAdmin(con); // 禁用表 admin.disableTable("hbase_demo-api"); // 删除表 admin.deleteTable(Bytes.toBytes("hbase_demo-api")); admin.close(); }