HBase1.0.1.1 API与原来有所不同

时间:2022-11-21 19:39:24
 package com.felix.hbaseapi_test;

 /*   
这是旧版的 API操作
*/
public class hbaseapifelix { public static final String TABLE_NAME = "testapi";
public static final String COLUMNFAMILY_NAME = "cf";
public static final String ROW_KEY = "rowkey1"; public static void main(String[] args) throws Exception {
Configuration conf = HBaseConfiguration.create();
HBaseAdmin admin=new HBaseAdmin(conf);
createtable(admin);
HTable htable=new HTable(conf, TABLE_NAME); put(htable,"age","25");
put(htable,"age","26");
put(htable,"age","27");
put(htable,"age","28"); //Get
Get get=new Get(ROW_KEY.getBytes());
htable.get(get); //scan
Scan scan=new Scan();
ResultScanner scanner = htable.getScanner(scan);
} private static void put(HTable htable,String column,String value) throws IOException {
Put put=new Put(ROW_KEY.getBytes());
put.addColumn(COLUMNFAMILY_NAME.getBytes(), column.getBytes(), value.getBytes());
htable.put(put);
} private static void createtable(HBaseAdmin admin) throws IOException {
HTableDescriptor desc = new HTableDescriptor(TABLE_NAME);
HColumnDescriptor family = new HColumnDescriptor(COLUMNFAMILY_NAME);
desc.addFamily(family);
family.setMaxVersions(3);
if (!admin.tableExists(TABLE_NAME)) {
//该表不存在,直接创建
admin.createTable(desc);
}else{
//该表存在,删除后再创建
if(!admin.isTableAvailable(TABLE_NAME)){
//该表disable,直接删除
admin.deleteTable(TABLE_NAME);
}else{
//该表enable,先disable,再删除
admin.disableTable(TABLE_NAME);
admin.deleteTable(TABLE_NAME);
}
admin.createTable(desc);
}
} }
 
 package com.felix.hbaseapi_test;

 import java.io.IOException;

 import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table; public class hbaseapifelix { public static final String TABLE_NAME = "testapi";
public static final String COLUMNFAMILY_NAME = "cf";
public static final String ROW_KEY = "rowkey1"; public static void main(String[] args) throws Exception {
Configuration conf = HBaseConfiguration.create();
//下面的配置,在configuration文件中都配置过了这里没必要配置,也不方便
//conf.set("hbase.rootdir", "hdfs://centos:9000/hbase");
//conf.set("hbase.zookeeper.quorum","centos");
//conf.set("hbase.zookeeper.property.clientPort", "2181"); Connection connection = ConnectionFactory.createConnection(conf);
Admin admin = connection.getAdmin();
Table table = connection.getTable(TableName.valueOf("user"));
TableName name = table.getName(); initBeforeCreate(admin, name);
createTable(admin, table); try {
Put put=new Put("rowkey".getBytes());
put.addColumn(COLUMNFAMILY_NAME.getBytes(), "age".getBytes(), "25".getBytes());
put.addColumn(COLUMNFAMILY_NAME.getBytes(), "age".getBytes(), "26".getBytes());
put.addColumn(COLUMNFAMILY_NAME.getBytes(), "age".getBytes(), "27".getBytes());
put.addColumn(COLUMNFAMILY_NAME.getBytes(), "age".getBytes(), "28".getBytes());
table.put(put);
} finally {
table.close();
connection.close();
} Get get = new Get(ROW_KEY.getBytes());
Result result = table.get(get); Scan scan=new Scan();
ResultScanner scanner = table.getScanner(scan); /* HBaseAdmin admin=new HBaseAdmin(conf);
createtable(admin); HTable htable=new HTable(conf, TABLE_NAME); put(htable,"age","25");
put(htable,"age","26");
put(htable,"age","27");
put(htable,"age","28"); //Get
Get get=new Get(ROW_KEY.getBytes());
htable.get(get); //scan
Scan scan=new Scan();
ResultScanner scanner = htable.getScanner(scan);*/
} private static void initBeforeCreate(Admin admin, TableName name)
throws IOException {
/*创建前存在就删除
* */
if(admin.tableExists(name)){
if(admin.isTableEnabled(name)){
admin.disableTable(name);
}
admin.deleteTable(name);
}
} private static void createTable(Admin admin, Table table)
throws IOException {
HTableDescriptor desc=new HTableDescriptor(table.getName());
HColumnDescriptor family=new HColumnDescriptor(COLUMNFAMILY_NAME);
family.setMaxVersions(3);
family.setMinVersions(0);
desc.addFamily(family);
admin.createTable(desc);
} /*private static void put(HTable htable,String column,String value) throws IOException {
Put put=new Put(ROW_KEY.getBytes());
put.addColumn(COLUMNFAMILY_NAME.getBytes(), column.getBytes(), value.getBytes());
htable.put(put);
} private static void createtable(HBaseAdmin admin) throws IOException {
HTableDescriptor desc = new HTableDescriptor(TABLE_NAME);
HColumnDescriptor family = new HColumnDescriptor(COLUMNFAMILY_NAME);
desc.addFamily(family);
family.setMaxVersions(3);
if (!admin.tableExists(TABLE_NAME)) {
//该表不存在,直接创建
admin.createTable(desc);
}else{
//该表存在,删除后再创建
if(!admin.isTableAvailable(TABLE_NAME)){
//该表disable,直接删除
admin.deleteTable(TABLE_NAME);
}else{
//该表enable,先disable,再删除
admin.disableTable(TABLE_NAME);
admin.deleteTable(TABLE_NAME);
}
admin.createTable(desc);
}
}*/ }

具体改成什么了,以及为什么修改,源码里面说的很清楚,比如:

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.

其他的自己关联源码自己看吧!