1 package com.felix.hbaseapi_test; 2 3 /* 这是旧版的 API操作 */ 16 public class hbaseapifelix { 17 18 public static final String TABLE_NAME = "testapi"; 19 public static final String COLUMNFAMILY_NAME = "cf"; 20 public static final String ROW_KEY = "rowkey1"; 21 22 public static void main(String[] args) throws Exception { 23 Configuration conf = HBaseConfiguration.create(); 24 HBaseAdmin admin=new HBaseAdmin(conf); 51 createtable(admin); 53 HTable htable=new HTable(conf, TABLE_NAME); 54 55 put(htable,"age","25"); 56 put(htable,"age","26"); 57 put(htable,"age","27"); 58 put(htable,"age","28"); 59 60 //Get 61 Get get=new Get(ROW_KEY.getBytes()); 62 htable.get(get); 63 64 //scan 65 Scan scan=new Scan(); 66 ResultScanner scanner = htable.getScanner(scan); 67 } 68 69 private static void put(HTable htable,String column,String value) throws IOException { 70 Put put=new Put(ROW_KEY.getBytes()); 71 put.addColumn(COLUMNFAMILY_NAME.getBytes(), column.getBytes(), value.getBytes()); 72 htable.put(put); 73 } 74 75 private static void createtable(HBaseAdmin admin) throws IOException { 76 HTableDescriptor desc = new HTableDescriptor(TABLE_NAME); 77 HColumnDescriptor family = new HColumnDescriptor(COLUMNFAMILY_NAME); 78 desc.addFamily(family); 79 family.setMaxVersions(3); 80 if (!admin.tableExists(TABLE_NAME)) { 81 //该表不存在,直接创建 82 admin.createTable(desc); 83 }else{ 84 //该表存在,删除后再创建 85 if(!admin.isTableAvailable(TABLE_NAME)){ 86 //该表disable,直接删除 87 admin.deleteTable(TABLE_NAME); 88 }else{ 89 //该表enable,先disable,再删除 90 admin.disableTable(TABLE_NAME); 91 admin.deleteTable(TABLE_NAME); 92 } 93 admin.createTable(desc); 94 } 95 } 96 97 }
1 package com.felix.hbaseapi_test; 2 3 4 import java.io.IOException; 5 6 import org.apache.hadoop.conf.Configuration; 7 import org.apache.hadoop.hbase.HBaseConfiguration; 8 import org.apache.hadoop.hbase.HColumnDescriptor; 9 import org.apache.hadoop.hbase.HTableDescriptor; 10 import org.apache.hadoop.hbase.TableName; 11 import org.apache.hadoop.hbase.client.Admin; 12 import org.apache.hadoop.hbase.client.Connection; 13 import org.apache.hadoop.hbase.client.ConnectionFactory; 14 import org.apache.hadoop.hbase.client.Get; 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.client.Table; 20 21 22 public class hbaseapifelix { 23 24 public static final String TABLE_NAME = "testapi"; 25 public static final String COLUMNFAMILY_NAME = "cf"; 26 public static final String ROW_KEY = "rowkey1"; 27 28 public static void main(String[] args) throws Exception { 29 Configuration conf = HBaseConfiguration.create(); 30 //下面的配置,在configuration文件中都配置过了这里没必要配置,也不方便 31 //conf.set("hbase.rootdir", "hdfs://centos:9000/hbase"); 32 //conf.set("hbase.zookeeper.quorum","centos"); 33 //conf.set("hbase.zookeeper.property.clientPort", "2181"); 34 35 Connection connection = ConnectionFactory.createConnection(conf); 36 Admin admin = connection.getAdmin(); 37 Table table = connection.getTable(TableName.valueOf("user")); 38 TableName name = table.getName(); 39 40 initBeforeCreate(admin, name); 41 createTable(admin, table); 42 43 try { 44 Put put=new Put("rowkey".getBytes()); 45 put.addColumn(COLUMNFAMILY_NAME.getBytes(), "age".getBytes(), "25".getBytes()); 46 put.addColumn(COLUMNFAMILY_NAME.getBytes(), "age".getBytes(), "26".getBytes()); 47 put.addColumn(COLUMNFAMILY_NAME.getBytes(), "age".getBytes(), "27".getBytes()); 48 put.addColumn(COLUMNFAMILY_NAME.getBytes(), "age".getBytes(), "28".getBytes()); 49 table.put(put); 50 } finally { 51 table.close(); 52 connection.close(); 53 } 54 55 56 Get get = new Get(ROW_KEY.getBytes()); 57 Result result = table.get(get); 58 59 Scan scan=new Scan(); 60 ResultScanner scanner = table.getScanner(scan); 61 62 /* HBaseAdmin admin=new HBaseAdmin(conf); 63 createtable(admin); 64 65 HTable htable=new HTable(conf, TABLE_NAME); 66 67 put(htable,"age","25"); 68 put(htable,"age","26"); 69 put(htable,"age","27"); 70 put(htable,"age","28"); 71 72 //Get 73 Get get=new Get(ROW_KEY.getBytes()); 74 htable.get(get); 75 76 //scan 77 Scan scan=new Scan(); 78 ResultScanner scanner = htable.getScanner(scan);*/ 79 } 80 81 private static void initBeforeCreate(Admin admin, TableName name) 82 throws IOException { 83 /*创建前存在就删除 84 * */ 85 if(admin.tableExists(name)){ 86 if(admin.isTableEnabled(name)){ 87 admin.disableTable(name); 88 } 89 admin.deleteTable(name); 90 } 91 } 92 93 private static void createTable(Admin admin, Table table) 94 throws IOException { 95 HTableDescriptor desc=new HTableDescriptor(table.getName()); 96 HColumnDescriptor family=new HColumnDescriptor(COLUMNFAMILY_NAME); 97 family.setMaxVersions(3); 98 family.setMinVersions(0); 99 desc.addFamily(family); 100 admin.createTable(desc); 101 } 102 103 /*private static void put(HTable htable,String column,String value) throws IOException { 104 Put put=new Put(ROW_KEY.getBytes()); 105 put.addColumn(COLUMNFAMILY_NAME.getBytes(), column.getBytes(), value.getBytes()); 106 htable.put(put); 107 } 108 109 private static void createtable(HBaseAdmin admin) throws IOException { 110 HTableDescriptor desc = new HTableDescriptor(TABLE_NAME); 111 HColumnDescriptor family = new HColumnDescriptor(COLUMNFAMILY_NAME); 112 desc.addFamily(family); 113 family.setMaxVersions(3); 114 if (!admin.tableExists(TABLE_NAME)) { 115 //该表不存在,直接创建 116 admin.createTable(desc); 117 }else{ 118 //该表存在,删除后再创建 119 if(!admin.isTableAvailable(TABLE_NAME)){ 120 //该表disable,直接删除 121 admin.deleteTable(TABLE_NAME); 122 }else{ 123 //该表enable,先disable,再删除 124 admin.disableTable(TABLE_NAME); 125 admin.deleteTable(TABLE_NAME); 126 } 127 admin.createTable(desc); 128 } 129 }*/ 130 131 }
具体改成什么了,以及为什么修改,源码里面说的很清楚,比如:
HBaseAdmin is no longer a client API. It is marked InterfaceAudience.Private indicating that
* this is an HBase-internal class as defined in
* https://hadoop.apache.org/docs/current/hadoop-project-dist/hadoop-common/InterfaceClassification.html
* There are no guarantees for backwards source / binary compatibility and methods or class can
* change or go away without deprecation.
* Use {@link Connection#getAdmin()} to obtain an instance of {@link Admin} instead of constructing
* an HBaseAdmin directly.
其他的自己关联源码自己看吧!
原文地址:https://www.cnblogs.com/double-orange/p/10209784.html
时间: 2024-10-12 23:27:56