`

(转载)hbase的基本操作

 
阅读更多

本文列举一些hbase的基本操作代码。 

Java代码  收藏代码
  1. package allen.studyhbase;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.LinkedList;  
  5. import java.util.List;  
  6.   
  7. import org.apache.commons.logging.Log;  
  8. import org.apache.commons.logging.LogFactory;  
  9. import org.apache.hadoop.hbase.HColumnDescriptor;  
  10. import org.apache.hadoop.hbase.HTableDescriptor;  
  11. import org.apache.hadoop.hbase.client.Delete;  
  12. import org.apache.hadoop.hbase.client.HBaseAdmin;  
  13. import org.apache.hadoop.hbase.client.HTableInterface;  
  14. import org.apache.hadoop.hbase.client.HTablePool;  
  15. import org.apache.hadoop.hbase.client.Put;  
  16. import org.apache.hadoop.hbase.client.Result;  
  17. import org.apache.hadoop.hbase.client.ResultScanner;  
  18. import org.apache.hadoop.hbase.client.Scan;  
  19. import org.apache.hadoop.hbase.util.Bytes;  
  20. import org.junit.After;  
  21. import org.junit.AfterClass;  
  22. import org.junit.Before;  
  23. import org.junit.BeforeClass;  
  24.   
  25. public class BaseTest {  
  26.     protected static Log log = LogFactory.getLog(BaseTest.class);  
  27.   
  28.     protected static HBaseAdmin hbaseAdmin;  
  29.     protected static HTablePool pool;  
  30.     protected static String TableName = "allen_test";  
  31.     protected static byte[] TableNameBytes = Bytes.toBytes(TableName);  
  32.     protected static byte[] ColumnFamilyName = Bytes.toBytes("cf");  
  33.   
  34.     protected static String QNameStr1 = "q1";  
  35.     protected static String QNameStr2 = "q2";  
  36.     protected static String QNameStr3 = "q3";  
  37.   
  38.     protected static byte[] QName1 = Bytes.toBytes(QNameStr1);  
  39.     protected static byte[] QName2 = Bytes.toBytes(QNameStr2);  
  40.     protected static byte[] QName3 = Bytes.toBytes(QNameStr3);  
  41.   
  42.     protected HTableInterface table;  
  43.   
  44.     @BeforeClass  
  45.     public static void beforeClass() throws Exception {  
  46.         hbaseAdmin = CommonConfig.getHBaseAdmin();  
  47.         deleteTable();  
  48.         createTable();  
  49.     }  
  50.   
  51.     @AfterClass  
  52.     public static void afterClass() throws Exception {  
  53.         deleteTable();  
  54.     }  
  55.   
  56.     @Before  
  57.     public void before() throws Throwable {  
  58.         pool = CommonConfig.getHTablePool();  
  59.         table = pool.getTable(TableName);  
  60.         fillData();  
  61.     }  
  62.   
  63.     @After  
  64.     public void after() throws Exception {  
  65.         try {  
  66.             // full scan.  
  67.             Scan scan = new Scan();  
  68.             ResultScanner resultScanner = table.getScanner(scan);  
  69.   
  70.             List<byte[]> rows = new LinkedList<byte[]>();  
  71.             for (Result result = resultScanner.next(); result != null; result = resultScanner  
  72.                     .next()) {  
  73.                 rows.add(result.getRow());  
  74.             }  
  75.   
  76.             for (byte[] row : rows) {  
  77.                 table.delete(new Delete(row));  
  78.                 log.info("delete " + Bytes.toString(row));  
  79.             }  
  80.   
  81.             // return table to pool.  
  82.             table.close();  
  83.   
  84.         } catch (IOException e) {  
  85.             throw new RuntimeException(e);  
  86.         }  
  87.     }  
  88.   
  89.     private static void createTable() throws Exception {  
  90.         // create new table.  
  91.         HTableDescriptor tableDescriptor = new HTableDescriptor(TableName);  
  92.         tableDescriptor.addFamily(new HColumnDescriptor(ColumnFamilyName));  
  93.         hbaseAdmin.createTable(tableDescriptor);  
  94.     }  
  95.   
  96.     private static void deleteTable() throws Exception {  
  97.         // delete table if table exist.  
  98.         if (hbaseAdmin.tableExists(TableName)) {  
  99.             // disable table before delete it.  
  100.             hbaseAdmin.disableTable(TableName);  
  101.             hbaseAdmin.deleteTable(TableName);  
  102.         }  
  103.     }  
  104.   
  105.     String rowKeyStr1 = "allen_test_row1";  
  106.     String rowKeyStr2 = "allen_test_row2";  
  107.     String rowKeyStr3 = "allen_test_row3";  
  108.     String rowKeyStr4 = "allen_test_row4";  
  109.   
  110.     byte[] rowKey1 = Bytes.toBytes(rowKeyStr1);  
  111.     byte[] rowKey2 = Bytes.toBytes(rowKeyStr2);  
  112.     byte[] rowKey3 = Bytes.toBytes(rowKeyStr3);  
  113.     byte[] rowKey4 = Bytes.toBytes(rowKeyStr4);  
  114.   
  115.     private void fillData() throws Throwable {  
  116.   
  117.         Put put = new Put(rowKey1);  
  118.         put.add(ColumnFamilyName, QName1, Bytes.toBytes(100L));  
  119.         put.add(ColumnFamilyName, QName2, Bytes.toBytes(100L));  
  120.         table.put(put);  
  121.   
  122.         put = new Put(rowKey2);  
  123.         put.add(ColumnFamilyName, QName1, Bytes.toBytes(20L));  
  124.         put.add(ColumnFamilyName, QName2, Bytes.toBytes(200L));  
  125.         table.put(put);  
  126.   
  127.         // set null case.  
  128.         put = new Put(rowKey3);  
  129.         put.add(ColumnFamilyName, QName1, null);  
  130.         put.add(ColumnFamilyName, QName2, null);  
  131.         table.put(put);  
  132.   
  133.         // empty case.  
  134.         put = new Put(rowKey4);  
  135.         put.add(ColumnFamilyName, QName3, Bytes.toBytes("test"));  
  136.         table.put(put);  
  137.     }  
  138.   
  139. }  

 

Java代码  收藏代码
  1. package allen.studyhbase;  
  2.   
  3.   
  4.   
  5. import org.apache.hadoop.conf.Configuration;  
  6.   
  7. import org.apache.hadoop.hbase.HBaseConfiguration;  
  8.   
  9. import org.apache.hadoop.hbase.client.HBaseAdmin;  
  10.   
  11. import org.apache.hadoop.hbase.client.HTablePool;  
  12.   
  13.   
  14.   
  15. /** 
  16.  
  17.  * CommonConfig. 
  18.  
  19.  * */  
  20.   
  21. public class CommonConfig {  
  22.   
  23.   
  24.   
  25.     private static HTablePool pool;  
  26.   
  27.     private static HBaseAdmin hbaseAdmin;  
  28.   
  29.     private static Configuration conf;  
  30.   
  31.   
  32.   
  33.     static {  
  34.   
  35.         try {  
  36.   
  37.             conf = HBaseConfiguration.create();  
  38.   
  39.             pool = new HTablePool(conf, 50);  
  40.   
  41.             hbaseAdmin = new HBaseAdmin(conf);  
  42.   
  43.         } catch (Exception e) {  
  44.   
  45.             throw new RuntimeException(e);  
  46.   
  47.         }  
  48.   
  49.     };  
  50.   
  51.   
  52.   
  53.     public static Configuration getConfiguration() {  
  54.   
  55.         return conf;  
  56.   
  57.     }  
  58.   
  59.   
  60.   
  61.     public static HBaseAdmin getHBaseAdmin() {  
  62.   
  63.         return hbaseAdmin;  
  64.   
  65.     }  
  66.   
  67.   
  68.   
  69.     public static HTablePool getHTablePool() {  
  70.   
  71.         return pool;  
  72.   
  73.     }  
  74.   
  75. }  

 

Java代码  收藏代码
  1. package allen.studyhbase;  
  2.   
  3. import java.util.HashSet;  
  4. import java.util.Set;  
  5.   
  6. import org.apache.hadoop.hbase.client.Delete;  
  7. import org.apache.hadoop.hbase.client.Get;  
  8.   
  9. import org.apache.hadoop.hbase.client.Put;  
  10. import org.apache.hadoop.hbase.client.Result;  
  11. import org.apache.hadoop.hbase.client.ResultScanner;  
  12. import org.apache.hadoop.hbase.client.Scan;  
  13.   
  14. import org.apache.hadoop.hbase.util.Bytes;  
  15.   
  16. import org.junit.Assert;  
  17.   
  18. import org.junit.Test;  
  19.   
  20. public class TestHbaseOp extends BaseTest {  
  21.   
  22.     @Test  
  23.     public void testDelete() throws Exception {  
  24.   
  25.         Get get = new Get(rowKey1);  
  26.         Result result = table.get(get);  
  27.         Assert.assertTrue(!result.isEmpty());  
  28.   
  29.         Delete delete = new Delete(rowKey1);  
  30.         table.delete(delete);  
  31.   
  32.         get = new Get(rowKey1);  
  33.         result = table.get(get);  
  34.         Assert.assertTrue(result.isEmpty());  
  35.   
  36.     }  
  37.   
  38.     @Test  
  39.     public void testDeleteNotExistRow() throws Exception {  
  40.         byte[] rowKey = Bytes.toBytes("allen_test_row");  
  41.         Delete delete = new Delete(rowKey);  
  42.         table.delete(delete);  
  43.     }  
  44.   
  45.     @Test  
  46.     public void testScan_01() throws Exception {  
  47.   
  48.         Set<String> resultRowKeys = new HashSet<String>();  
  49.         Scan scan = new Scan(rowKey1, rowKey2);  
  50.         ResultScanner resultScanner = table.getScanner(scan);  
  51.         for (Result result = resultScanner.next(); result != null; result = resultScanner  
  52.                 .next()) {  
  53.             resultRowKeys.add(Bytes.toString(result.getRow()));  
  54.         }  
  55.   
  56.         Assert.assertTrue(resultRowKeys.size() == 1);  
  57.         Assert.assertTrue(resultRowKeys.contains(rowKeyStr1));  
  58.     }  
  59.   
  60.     @Test  
  61.     public void testScan_02() throws Exception {  
  62.         Set<String> resultRowKeys = new HashSet<String>();  
  63.   
  64.         Scan scan = new Scan(rowKey1);  
  65.         ResultScanner resultScanner = table.getScanner(scan);  
  66.         for (Result result = resultScanner.next(); result != null; result = resultScanner  
  67.                 .next()) {  
  68.             resultRowKeys.add(Bytes.toString(result.getRow()));  
  69.         }  
  70.   
  71.         Assert.assertTrue(resultRowKeys.size() == 4);  
  72.         Assert.assertTrue(resultRowKeys.contains(rowKeyStr1));  
  73.         Assert.assertTrue(resultRowKeys.contains(rowKeyStr2));  
  74.         Assert.assertTrue(resultRowKeys.contains(rowKeyStr3));  
  75.         Assert.assertTrue(resultRowKeys.contains(rowKeyStr4));  
  76.     }  
  77.   
  78.     @Test  
  79.     public void testScan_03() throws Exception {  
  80.         Set<String> resultRowKeys = new HashSet<String>();  
  81.   
  82.         Scan scan = new Scan(rowKey1);  
  83.         scan.addColumn(ColumnFamilyName, QName1);  
  84.         ResultScanner resultScanner = table.getScanner(scan);  
  85.         for (Result result = resultScanner.next(); result != null; result = resultScanner  
  86.                 .next()) {  
  87.             resultRowKeys.add(Bytes.toString(result.getRow()));  
  88.         }  
  89.   
  90.         Assert.assertTrue(resultRowKeys.size() == 3);  
  91.         Assert.assertTrue(resultRowKeys.contains(rowKeyStr1));  
  92.         Assert.assertTrue(resultRowKeys.contains(rowKeyStr2));  
  93.         Assert.assertTrue(resultRowKeys.contains(rowKeyStr3));  
  94.   
  95.     }  
  96.   
  97.     @Test  
  98.     public void testCheckAndPut() throws Exception {  
  99.         byte[] rowKey = Bytes.toBytes("allen_test_row");  
  100.         Put put = new Put(rowKey);  
  101.         put.add(ColumnFamilyName, QName1, Bytes.toBytes("a"));  
  102.         put.add(ColumnFamilyName, QName2, Bytes.toBytes("b"));  
  103.   
  104.         boolean result = false;  
  105.   
  106.         result = table.checkAndPut(rowKey, ColumnFamilyName, QName2,  
  107.                 Bytes.toBytes("b"), put);  
  108.         // check fail, put fail.  
  109.         Assert.assertFalse(result);  
  110.   
  111.         result = table.checkAndPut(rowKey, ColumnFamilyName, QName2, null, put);  
  112.         // check ok, put ok.  
  113.         Assert.assertTrue(result);  
  114.   
  115.         result = table.checkAndPut(rowKey, ColumnFamilyName, QName2, null, put);  
  116.         // check fail, put fail.  
  117.         Assert.assertFalse(result);  
  118.   
  119.         result = table.checkAndPut(rowKey, ColumnFamilyName, QName2,  
  120.                 Bytes.toBytes("b"), put);  
  121.         // check ok, put ok.  
  122.         Assert.assertTrue(result);  
  123.     }  
  124.   
  125.     @Test  
  126.     public void testPutAndGet() throws Exception {  
  127.         byte[] rowKey = Bytes.toBytes("allen_test_row");  
  128.         Put put = new Put(rowKey);  
  129.         put.add(ColumnFamilyName, QName1, Bytes.toBytes("a"));  
  130.         put.add(ColumnFamilyName, QName3, null);  
  131.         table.put(put);  
  132.   
  133.         Get get = new Get(rowKey);  
  134.         Result result = table.get(get);  
  135.   
  136.         byte[] q1 = result.getValue(ColumnFamilyName, QName1);  
  137.         byte[] q2 = result.getValue(ColumnFamilyName, QName2);  
  138.         byte[] q3 = result.getValue(ColumnFamilyName, QName3);  
  139.   
  140.         Assert.assertEquals("a", Bytes.toString(q1));  
  141.         // we get null byte array here.  
  142.         Assert.assertEquals(null, Bytes.toString(q2));  
  143.         // we get empty byte array here. not a null.  
  144.         Assert.assertEquals("", Bytes.toString(q3));  
  145.   
  146.         // get a row doesn't exist.  
  147.         byte[] rowKey2 = Bytes.toBytes("allen_test_row_not_exist");  
  148.         get = new Get(rowKey2);  
  149.         result = table.get(get);  
  150.         Assert.assertTrue(result.isEmpty());  
  151.     }  
  152.   
  153.     @Test  
  154.     public void testPutWithoutColumn() throws Exception {  
  155.         byte[] rowKey = Bytes.toBytes("allen_test_row");  
  156.         Put put = new Put(rowKey);  
  157.         try {  
  158.             table.put(put);  
  159.             Assert.fail();  
  160.         } catch (IllegalArgumentException e) {  
  161.             // ignore.  
  162.         }  
  163.     }  
  164. }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics