hbase学习

package com.hbase;

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.*;
import org.apache.hadoop.hbase.util.Bytes;

public class HTest {

private static Configuration sConf;
private static Connection sCon;
private static Admin sAdmin;

public static void main(String[] args) throws IOException {


String[] s = { "f1", "f2", "f3" };
init();
createTable("table2", s);
putData("table2", "r2", "f1", "c1", "panpan");
Result res = getDataByRow("table2", "r1");
System.out.println(res.value());
getAllData("table2");
delete("table1");
deleteDatayRow("table2","r1");
close();
}

public static void init() throws IOException {

sConf = HBaseConfiguration.create();
sConf.set("root.dir", "dhfs://localhost:9000/hbase");
sCon = ConnectionFactory.createConnection(sConf);
sAdmin = sCon.getAdmin();
}


public static void close() {
try {
if (null != sAdmin) {
sAdmin.close();
}
if (sCon != null) {
sCon.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


public static void createTable(String tableName, String[] columnFamilys) throws IOException {


TableName _tableName = TableName.valueOf(tableName);
if (sAdmin.tableExists(_tableName)) {
System.out.println("table exist");
} else {
HTableDescriptor tableDesc = new HTableDescriptor(_tableName);
for (String columnFamily : columnFamilys)
tableDesc.addFamily(new HColumnDescriptor(columnFamily));
sAdmin.createTable(tableDesc);
System.out.println("create table success!");
}
}


// add data
public static void putData(String tableName, String rowKey, String columnFamily, String col, String value)
throws IOException {


Table table = sCon.getTable(TableName.valueOf(tableName));
Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(col), Bytes.toBytes(value));
table.put(put);
System.out.println("put data success");
table.close();
}


public static void getAllData(String tableName) throws IOException {


Table table = sCon.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
System.out.println("Scan: "+result);
}
}


public static Result getDataByRow(String tableName, String row) throws IOException {


Table table = sCon.getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(row));
Result result = table.get(get);
return result;
}


//delete row
public static void deleteDatayRow(String tableName,String row) throws IOException{

Table table = sCon.getTable(TableName.valueOf(tableName));
table.delete(new Delete(row.getBytes()));

}

//删除表
public static void delete(String tableName) throws IOException{
TableName _tableName = TableName.valueOf(tableName);
if(sAdmin.tableExists(_tableName)){
try {
 sAdmin.disableTable(_tableName);
 sAdmin.deleteTable(_tableName);
} catch (IOException e) {
 e.printStackTrace();
 System.out.println("Delete "+tableName+" 失败");
}
}else{
System.out.println("table not exist");
return ;
}
System.out.println("Delete "+tableName+" 成功");
}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值