1、新建一个IDEA的Maven工程
2、引入依赖
3、新建测试类
package com.motoon; 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 java.io.IOException; /** * @author rjsong */ public class HBaseTest { public static void main( String[] args ) { // 建立连接 Configuration conf = HBaseConfiguration.create(); conf.set("hbase.rootdir", "hdfs://192.168.61.138:9000/hbase"); Connection conn = null; Admin admin = null; try { conn = ConnectionFactory.createConnection(conf); admin = conn.getAdmin(); } catch (IOException e) { e.printStackTrace(); } String TABLE_NAME = "test"; String COLUMN_FAMILY_NAME = "columnfamily"; String COLUMN_FAMILY_NAME2 = "columnfamily1"; String[] COLUMNS = {"column0", "column1", "column2"}; // Some data String ROW_KEYS = "row0"; String[] VALUES = {"value0", "value1", "value2"}; System.out.println("CREATE TABLE =========================="); // 创建表 TableName tableName = TableName.valueOf(TABLE_NAME); try { // 存在则删除 if (admin.tableExists(tableName)) { admin.disableTable(tableName); admin.deleteTable(tableName); } TableDescriptor descriptor = TableDescriptorBuilder.newBuilder(tableName) .addColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(COLUMN_FAMILY_NAME.getBytes()).build()) .addColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(COLUMN_FAMILY_NAME2.getBytes()).build()) .build(); admin.createTable(descriptor); } catch (IOException e) { e.printStackTrace(); } System.out.println("PUT VALUE =========================="); // 添加数据 try { Table table = conn.getTable(tableName); Put put = new Put(ROW_KEYS.getBytes()); put.addColumn(COLUMN_FAMILY_NAME.getBytes(), COLUMNS[0].getBytes(), VALUES[0].getBytes()); put.addColumn(COLUMN_FAMILY_NAME.getBytes(), COLUMNS[1].getBytes(), VALUES[1].getBytes()); put.addColumn(COLUMN_FAMILY_NAME.getBytes(), COLUMNS[2].getBytes(), VALUES[2].getBytes()); table.put(put); table.close(); } catch (IOException e) { e.printStackTrace(); } System.out.println("DELETE VALUE =========================="); // 删除数据: 删除row0的column2列 try { Table table = conn.getTable(tableName); Delete delete = new Delete(ROW_KEYS.getBytes()); delete.addColumn(COLUMN_FAMILY_NAME.getBytes(), COLUMNS[2].getBytes()); table.delete(delete); table.close(); } catch (IOException e) { e.printStackTrace(); } System.out.println("GET VALUE =========================="); // 查询数据 try { Table table = conn.getTable(tableName); Get get = new Get(ROW_KEYS.getBytes()); //get.addFamily(COLUMN_FAMILY_NAME.getBytes()); // 获取特定列族的数据 //get.addColumn(COLUMN_FAMILY_NAME.getBytes(), COLUMNS[1].getBytes()); // 获取特定列的数据 Result result = table.get(get); // get column family result.getFamilyMap(COLUMN_FAMILY_NAME.getBytes()).forEach((k,v) -> System.out.println(new String(k) + ":" + new String(v))); table.close(); } catch (IOException e) { e.printStackTrace(); } // 关闭连接 try { if (admin != null) { admin.close(); } if (conn != null) { conn.close(); } } catch (IOException e) { e.printStackTrace(); } } }