如果是DDL的操作就找HbaseAdmin.
如果是表上的增删改查的操作就找HTable.
附录代码:
1 mport java.util.Arrays; 2 3 import org.apache.hadoop.conf.Configuration; 4 import org.apache.hadoop.hbase.HBaseConfiguration; 5 import org.apache.hadoop.hbase.HColumnDescriptor; 6 import org.apache.hadoop.hbase.HTableDescriptor; 7 import org.apache.hadoop.hbase.KeyValue; 8 import org.apache.hadoop.hbase.TableName; 9 import org.apache.hadoop.hbase.client.Delete; 10 import org.apache.hadoop.hbase.client.Get; 11 import org.apache.hadoop.hbase.client.HBaseAdmin; 12 import org.apache.hadoop.hbase.client.HTable; 13 import org.apache.hadoop.hbase.client.Put; 14 import org.apache.hadoop.hbase.client.Result; 15 import org.apache.hadoop.hbase.client.ResultScanner; 16 import org.apache.hadoop.hbase.client.Scan; 17 import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp; 18 import org.apache.hadoop.hbase.filter.Filter; 19 import org.apache.hadoop.hbase.filter.SingleColumnValueFilter; 20 import org.junit.After; 21 import org.junit.Before; 22 import org.junit.Test; 23 24 public class HBaseCRUDTest { 25 26 private HBaseAdmin hBaseAdmin; 27 private Configuration conf; 28 29 /** 30 * 对必须用的对象放在@Before中进行初始化 31 * @throws Exception 32 */ 33 @Before 34 public void setUp() throws Exception{ 35 /*创建conf对象的第一种方式 36 conf = new Configuration(); 37 conf.set("hbase.rootdir", "hdfs://crxy99:9000/hbase"); 38 conf.set("hbase.cluster.distributed", "true"); 39 conf.set("hbase.zookeeper.quorum", "crxy99"); 40 */ 41 //创建对象的第二种方式 42 //这种方式需要再Maven的src/test/resoures目录中放置Hbase集群上的hbase-site.xml文件. 43 conf = HBaseConfiguration.create(); 44 45 hBaseAdmin = new HBaseAdmin(conf);//通过Configuration对象得到HBaseAdmin对象. 46 System.out.println("-------setUp-------"); 47 48 } 49 50 51 /** 52 * 是用完HBaseAdmin对象之后需要关闭. 53 * @throws Exception 54 */ 55 @After 56 public void cleanUp() throws Exception { 57 hBaseAdmin.close(); 58 System.out.println("-------cleanUp-----"); 59 } 60 61 62 /** 63 * 打印出Hbase中所有的表. 64 * @throws Exception 65 */ 66 @Test 67 @SuppressWarnings("deprecation") 68 public void testListTables() throws Exception{ 69 System.out.println("------listTables------"); 70 //获得Hbase中表的方法一: 71 String [] tableNames = hBaseAdmin.getTableNames(); 72 System.out.println(Arrays.toString(tableNames)); 73 74 //获得Hbase中表的方法二: 75 TableName[] listTables= hBaseAdmin.listTableNames(); 76 System.out.println(Arrays.toString(listTables)); 77 78 //获得Hbase中表的方法三(用到了HTableDescriptor对象): 79 HTableDescriptor[] listTabels2 = hBaseAdmin.listTables(); 80 for (HTableDescriptor hTD : listTabels2) { 81 System.out.print(hTD.getTableName() + " "); 82 } 83 System.out.println(); 84 } 85 86 /** 87 * 向HBase中增加一个表 88 */ 89 @Test 90 public void testAddTable() throws Exception { 91 System.out.println("-----addTable-----"); 92 HTableDescriptor desc = new HTableDescriptor("t2");//创建表的名称 93 HColumnDescriptor family = new HColumnDescriptor("cf"); //创建表的列族 94 desc.addFamily(family);//向表中添加列族 (Hbase在创建表的时候就要添加列族.) 95 /* 96 *HBase对单行做到了一个事务,hbase不支持跨行的事务 97 */ 98 99 //table.checkAndPut(row, family, qualifier, value, put) 100 101 hBaseAdmin.createTable(desc); 102 } 103 104 /** 105 * 向表中插入数据 106 * 操作表中的数据就是需要操作表本身,需要用到了HTable对象. 107 */ 108 @Test 109 public void testAddRecord() throws Exception { 110 HTable table = new HTable(conf, "t2");//操作上面新建的t2 111 String rowKey = "1"; 112 String family = "cf"; 113 //String qualifier = "name"; 114 String qualifier = "age"; 115 //String value = "SummerChill"; 116 String value = "18"; 117 Put put = new Put(rowKey.getBytes());//put对象传入的是一个"行键rowkey" 118 //HBase中存储的都是字节数组 119 //插入表中数据的时候hbase的shell操作用的put命令,JavaAPI对应的是Put对象. 120 put.add(family.getBytes(), qualifier.getBytes(), value.getBytes()); 121 table.put(put); 122 table.close(); 123 System.out.println("------addRecord-------"); 124 } 125 126 /** 127 * 查询表中的数据 128 */ 129 @SuppressWarnings("deprecation") 130 @Test 131 public void testQueryRecord() throws Exception { 132 HTable table = new HTable(conf, "t2"); 133 String rowKey = "1"; 134 //查询表中数据的时候HBase的shell操作用的get命令,JavaAPI对应的是Get对象. 135 Get get = new Get(rowKey.getBytes()); 136 get.addColumn("cf".getBytes(), "age".getBytes()); 137 Result result = table.get(get); 138 KeyValue[] keyVs = result.raw(); 139 for(KeyValue kv : keyVs ){ 140 byte[] qualifier = kv.getQualifier(); 141 byte[] value = kv.getValue(); 142 System.out.println(new String(qualifier) + "=" + new String(value)); 143 } 144 table.close(); 145 System.out.println("-----getRecord-----"); 146 } 147 148 /** 149 * 条件查询 150 */ 151 @SuppressWarnings("deprecation") 152 @Test 153 public void testQueryByCondition() throws Exception { 154 HTable table = new HTable(conf, "t2"); 155 Scan scan = new Scan(); 156 Filter filter = new SingleColumnValueFilter( 157 "cf".getBytes(),"age".getBytes(), 158 CompareOp.GREATER, "13".getBytes()); 159 //CompareOp是一个操作的枚举常量 160 scan.setFilter(filter); 161 ResultScanner rs = table.getScanner(scan); 162 163 for (Result result : rs) { 164 KeyValue[] keyVs = result.raw(); 165 for (KeyValue kv : keyVs) { 166 byte[] qualifier = kv.getQualifier(); 167 byte[] value = kv.getValue(); 168 System.out.print(new String(qualifier) + "=" + new String(value) + " "); 169 } 170 System.out.println(); 171 } 172 table.close(); 173 System.out.println("-----getRecord-----"); 174 } 175 176 /** 177 * 删除表中的记录 178 */ 179 @Test 180 public void testDeleteRecored() throws Exception { 181 HTable table = new HTable(conf, "t2"); 182 byte[] row = "2".getBytes(); 183 Delete delete = new Delete(row); 184 //删除指定的rowkey对应的某一列 185 //delete.deleteColumn("cf".getBytes(), "age".getBytes()); 186 table.delete(delete); 187 table.close(); 188 System.out.println("-----deleteRecord-----"); 189 } 190 191 /** 192 * 删除表 193 */ 194 @Test 195 public void testDropTable() throws Exception { 196 //删除表之前对表是否存在进行判断. 197 if(!hBaseAdmin.tableExists("t2")) { 198 throw new RuntimeException("Table Not Exists,你想弄啥嘞!"); 199 } 200 if(hBaseAdmin.isTableEnabled("t2")) { 201 hBaseAdmin.disableTable("t2"); 202 } 203 //在删除表之前必须对表设置disable,否则直接删除 会抛出异常 204 hBaseAdmin.deleteTable("t2"); 205 System.out.println("-----deleteTable-----"); 206 } 207 208 }
本文转自SummerChill博客园博客,原文链接:http://www.cnblogs.com/DreamDrive/p/5572060.html,如需转载请自行联系原作者