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.

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

最新文章

  1. kaggle 竞赛之套路
  2. Android开发学习---sharedpreference的使用
  3. apache-flume-1.5.0-bin windows
  4. 《深度探索C++对象模型》3
  5. CGAffineTransformScale
  6. Android 部分属性学习
  7. JS中的for和for in循环
  8. 使用Elasticsearch、Logstash、Kibana与Redis(作为缓冲区)对Nginx日志进行收集(转)
  9. Qt5:Qt程序不在任务拦显示图标
  10. bzoj1861
  11. strncpy, strncpy_s
  12. 字符串--C++系列
  13. python基础(八)——多线程
  14. iOS8 UIAlertView键盘闪一下的问题
  15. .net 中的async,await理解
  16. 【delphi】delphi操作sqlite3
  17. Swagger从入门到精通
  18. 虚拟机VMware的安装
  19. JAVASCRIPT校验大全[转]
  20. 解决Maven出现Plugin execution not covered by lifecycle configuration 错误

热门文章

  1. 如何在 Mac上 安裝 .NET Core 2.1 ?
  2. dll 已注册 检索 COM 类工厂中 CLSID 为 {XXXX-XXXX-XXX-XXXXX-XXX} 的组件时失败,原因是出现以下错误: 80040154。
  3. 总结下Mysql分表分库的策略及应用
  4. MongoDB日期类型查询
  5. mysql不能保存中文
  6. 枚举getClass、getDeclaringClass区别
  7. nodejs+expressjs+ws实现了websocket即时通讯,服务器和客户端互相通信
  8. HDU 1527 取石子游戏(威佐夫博弈)
  9. 解决easyui combobox赋值boolean类型的值时,经常出现的内容显示的value而不是text的bug
  10. AndroidStudio安装、配置、测试