import com.yss.utils.BasicPropertites;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.;
import org.apache.hadoop.hbase.client.;
import org.apache.hadoop.hbase.io.compress.Compression;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
/**
-
没有hbase-site.xml,core-site.xml也可以正常执行
-
@author
-
@version 2019-03-25 10:25
-
describe:Hbase 1.2.1 的操作 ,对于java api操作hbase,直接看这个即可,eclipse上的示例(sxt代码)早已经过时,改方法也适用于hbase0.98版本的
-
方法目录
-
create 创建表操作
-
delete 删除表操作
-
listTables 查看已有表
-
put 插入数据,包含批量插入数据
-
delete 删除数据
-
get 查询数据
*/
public class HbasePutGetCreateDelete {private static Configuration configuration;
private static Connection connection;
private static Admin admin;public static void main(String[] args) throws IOException {
createTable(“t2”,new String[]{“cf1”,“cf2”});
HbasePutGetCreateDelete.putRow(“t2”, “rw1”, “cf1”, “q1”, “val1”);
HbasePutGetCreateDelete.getData(“t2”, “rw1”, “cf1”, “q1”);
HbasePutGetCreateDelete.deleRow(“t2”,“rw1”,“cf1”,“q1”);
HbasePutGetCreateDelete.deleteTable(“t2”);
}/**
-
初始化连接
-
hbase初始化需要三个对象
-
Configuration
-
Connection
-
Admin
*/
public static void init(){
//用HBaseConfiguration类来建立一个Configuration类。Configuration类用于加载需要连接HBase的各项配置。
configuration = HBaseConfiguration.create();
configuration.set(“hbase.zookeeper.quorum”,BasicPropertites.zookeeperQuorum());
configuration.set(“hbase.zookeeper.property.clientPort”,BasicPropertites.zookeeperClientPort());
configuration.set(“zookeeper.znode.parent”,BasicPropertites.zookeeperParent());try {
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
- 关闭连接, 关闭的时候需要按照创建的顺序关闭
- Admin
- Connection
*/
public static void close(){
try {
if(null != admin)
admin.close();
if(null != connection)
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
-
create 创建表操作
-
@param tableNmae 表名
-
@param cols 列族,该方法是将列族放在一个数组中,进行遍历
-
@throws IOException
*/
public static void createTable(String tableNmae,String[] cols) throws IOException {//初始化,建立建立连接,如果应用中需要多次操作hbase,可以将init方法拿出去,只进行一次初始化操作
init();//HBase中对表的定义需要用到两个类,一个是定义表名的TableName,另一个是定义表属性的HTableDescriptor。
//htable=new HTable(conf, TN);//这方法存在安全性的问题,现在已经不建议使用了,通常采用下面的方法
TableName tableName = TableName.valueOf(tableNmae);
if(admin.tableExists(tableName)){
System.out.println(“talbe is exists!”);
}else {
HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
for(String col:cols){
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(col);
/**
对表的参数设置都在列族这进行设置
比如把压缩模式修改成GZ,把最大版本数修改为ALL_VERSIONS,ALL_VERSIONS的值其实就是Integer.MAX_VALUE。
hColumnDescriptor.setCompactionCompressionType(Compression.Algorithm.GZ);
hColumnDescriptor.setMaxVersions(HConstants.ALL_VERSIONS);
*/hTableDescriptor.addFamily(hColumnDescriptor); } admin.createTable(hTableDescriptor);
}
close();
}
/**
- delete 删除表操作
- @param tableName 表名
- @throws IOException
*/
public static void deleteTable(String tableName) throws IOException {
init();
TableName tn = TableName.valueOf(tableName);
if (admin.tableExists(tn)) {
admin.disableTable(tn);
admin.deleteTable(tn);
}
close();
}
/**
- listTables 查看已有表
- @throws IOException
*/
public static void listTables() throws IOException {
init();
HTableDescriptor hTableDescriptors[] = admin.listTables();
for(HTableDescriptor hTableDescriptor :hTableDescriptors){
System.out.println(hTableDescriptor.getNameAsString());
}
close();
}
/**
-
put 插入数据,包含批量插入数据
-
@param tableName 表名
-
@param rowkey rowkey
-
@param colFamily 列族
-
@param col 列名
-
@param val 列值
-
@throws IOException
*/
public static void putRow(String tableName,String rowkey,String colFamily,String col,String val) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(Bytes.toBytes(rowkey));
put.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col), Bytes.toBytes(val));
table.put(put);//批量插入
// 当一部分数据插入成功,但是另一部分数据插入失败,比如某个RegionServer服务器出现了问题,这时会返回一个IOException,操作会被放弃。不过插入成功的数据不会被回滚,还是成功插入了。
// 插入失败的重试
// 对于插入失败的数据,服务器会尝试着再次去插入或者换一个
// RegionServer,当尝试的次数大于定义的最大次数会抛出
// RetriesExhaustedWithDetailsException异常,该异常包含了很多错误信息,包括有多少操作失败了,失败的原因以及服务器名和重试的次 数。
// 如果你定义了错误的列族,则只会尝试一次,因为如果连列族都错了,就没必要再继续尝试下去了
/* List putList = new ArrayList();
puts.add(put);
table.put(putList);*/
table.close();
close();
}
/**
- delete 删除数据
- @param tableName
- @param rowkey
- @param colFamily
- @param col
- @throws IOException
/
public static void deleRow(String tableName,String rowkey,String colFamily,String col) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(Bytes.toBytes(rowkey));
//删除指定列族
//delete.addFamily(Bytes.toBytes(colFamily));
//删除指定列
//delete.addColumn(Bytes.toBytes(colFamily),Bytes.toBytes(col));
table.delete(delete);
//批量删除
// HBase服务端在调用删除方法的时候,如果成功地执行了一个删除操作,就会把这个删除操作从你传入的deletes列表中删除。
// 所以你的操作列表会越来越短,如果所有的操作都成功了,你的列表会变空。执行完delete操作后你可以把deletes列表的长度打印出来,你会发现deletes列表的长度变为0了。
// 如果删除失败了,这个操作还是会保留在delete的传参deletes列表中,并且还同时会抛出一个异常,所以你可以捕捉异常后,再检查删除失败后剩下的这些Delete操作:
// 然后再对这些剩下的Delete对象进行下一步的操作。
/ List deleteList = new ArrayList();
deleteList.add(delete);
table.delete(deleteList);*/
table.close();
close();
}
/**
-
get 查询数据
-
@param tableName
-
@param rowkey
-
@param colFamily
-
@param col
-
@throws IOException
*/
public static void getData(String tableName,String rowkey,String colFamily,String col)throws IOException{
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowkey));
//获取指定列族数据
//get.addFamily(Bytes.toBytes(colFamily));
//获取指定列数据
//get.addColumn(Bytes.toBytes(colFamily),Bytes.toBytes(col));
Result result = table.get(get);
//调用方法,获取需要的数据
showCell(result);
/**也可以result.getValue
byte[] value = result.getValue(Bytes.toBytes(colFamily), Bytes.toBytes(col));
*/table.close();
close();
}
//格式化输出
public static void showCell(Result result){
Cell[] cells = result.rawCells();
for(Cell cell:cells){
System.out.println(“RowName:”+new String(CellUtil.cloneRow(cell))+" “);
System.out.println(“Timetamp:”+cell.getTimestamp()+” “);
System.out.println(“column Family:”+new String(CellUtil.cloneFamily(cell))+” “);
System.out.println(“row Name:”+new String(CellUtil.cloneQualifier(cell))+” “);
System.out.println(“value:”+new String(CellUtil.cloneValue(cell))+” ");
}
} -
}