MAPREDUCE服务 MRS-创建HBase表:代码样例

时间:2024-06-29 14:10:55

代码样例

以下代码片段在com.huawei.bigdata.hbase.examples包的“HBaseSample”类的testCreateTable方法中。

public void testCreateTable() {
              LOG .info("Entering testCreateTable.");
             // Specify the table descriptor.
             TableDescriptorBuilder htd = TableDescriptorBuilder.newBuilder(tableName);(1)

             // Set the column family name to info.
             ColumnFamilyDescriptorBuilder hcd = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("info"));(2)

             // Set data encoding methods, HBase provides DIFF,FAST_DIFF,PREFIX
 
             hcd.setDataBlockEncoding(DataBlockEncoding.FAST_DIFF);
             // Set compression methods, HBase provides two default compression
             // methods:GZ and SNAPPY
             // GZ has the highest compression rate,but low compression and
             // decompression effeciency,fit for cold data
             // SNAPPY has low compression rate, but high compression and
             // decompression effeciency,fit for hot data.
             // it is advised to use SNAANPPY
             hcd.setCompressionType(Compression.Algorithm.SNAPPY);//注[1]
             htd.setColumnFamily(hcd.build());  (3)
             Admin admin = null; 
             try {
               // Instantiate an Admin object.
               admin = conn.getAdmin();  (4)
               if (!admin.tableExists(tableName)) {
                 LOG.info("Creating table...");
                 admin.createTable(htd.build());//注[2]  (5)
                 LOG.info(admin.getClusterMetrics().toString());
                 LOG.info(admin.listNamespaceDescriptors().toString());
                 LOG.info("Table created successfully.");
               } else {
                 LOG.warn("table already exists");
               }
             } catch (IOException e) {
                 LOG.error("Create table failed " ,e);
             } finally {
               if (admin != null) {
                 try {
                   // Close the Admin object.
                   admin.close();
                 } catch (IOException e) {
                   LOG.error("Failed to close admin " ,e);
                 }
               }
             }
             LOG.info("Exiting testCreateTable.");
  }     
support.huaweicloud.com/devg3-mrs/mrs_07_290015.html