hbase shell 指令
hbase shell 指令比较多,而且网上资料也很全,可以参考一下链接获取的自行百度、谷歌了解。
常用的 hbase shell 指令链接
hbase client 使用
1、在项目中加入maven 依赖
<!-- hbase client -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.2.2</version>
</dependency>
2、设置配置文件,获取hbase链接对象
public class HBaseUtil {
// 配置文件
private static Configuration conf;
// hbase 链接
private static Connection con;
// 初始化连接
static {
conf = HBaseConfiguration.create(); // 获得配制文件对象
conf.set("hbase.zookeeper.quorum", "192.168.66.3,192.168.66.4");
conf.set("hbase.zookeeper.property.clientPort", "2181");
try {
con = ConnectionFactory.createConnection(conf);// 获得连接对象
} catch (IOException e) {
e.printStackTrace();
}
}
// 获得连接
public static Connection getCon() {
if (con == null || con.isClosed()) {
try {
con = ConnectionFactory.createConnection(conf);
} catch (IOException e) {
e.printStackTrace();
}
}
return con;
}
// 关闭连接
public static void close() {
if (con != null) {
try {
con.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3、创建表
// 创建表
public static void createTable(String tableName, String... FamilyColumn) {
TableName tn = TableName.valueOf(tableName);
try {
Admin admin = HBaseUtil.getCon().getAdmin();
HTableDescriptor family = new HTableDescriptor(tn);
for (String fc : FamilyColumn) {
HColumnDescriptor hcd = new HColumnDescriptor(fc);
family.addFamily(hcd);
}
admin.createTable(family);
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
4、删除表
// 删除表
public static void dropTable(String tableName) {
TableName tn = TableName.valueOf(tableName);
try {
Admin admin = con.getAdmin();
admin.disableTable(tn);
admin.deleteTable(tn);
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
5、插入数据或更新数据
// 插入或者更新数据
public static boolean insert(String tableName, String rowKey,
String family, String qualifier, String value) {
try {
Table t = getCon().getTable(TableName.valueOf(tableName));
Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier),
Bytes.toBytes(value));
t.put(put);
return true;
} catch (IOException e) {
e.printStackTrace();
} finally {
HBaseUtil.close();
}
return false;
}
6、删除
// 删除一个数据
public static boolean del(String tableName, String rowKey, String family,
String qualifier) {
try {
Table t = getCon().getTable(TableName.valueOf(tableName));
Delete del = new Delete(Bytes.toBytes(rowKey));
if (qualifier != null) {
del.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
} else if (family != null) {
del.addFamily(Bytes.toBytes(family));
}
t.delete(del);
return true;
} catch (IOException e) {
e.printStackTrace();
} finally {
HBaseUtil.close();
}
return false;
}
//删除一行
public static boolean del(String tableName, String rowKey) {
return del(tableName, rowKey, null, null);
}
//删除一行下的一个列族
public static boolean del(String tableName, String rowKey, String family) {
return del(tableName, rowKey, family, null);
}
7、查询数据
//取到一个值
public static String byGet(String tableName, String rowKey, String family,
String qualifier) {
try {
Table t = getCon().getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowKey));
get.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
Result r = t.get(get);
return Bytes.toString(CellUtil.cloneValue(r.listCells().get(0)));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
//取到一个族列的值
public static Map<String, String> byGet(String tableName, String rowKey, String family) {
Map<String, String> result = null;
try {
Table t = getCon().getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowKey));
get.addFamily(Bytes.toBytes(family));
Result r = t.get(get);
List<Cell> cs = r.listCells();
result = cs.size() > 0 ? new HashMap<String, String>() : result;
for (Cell cell : cs) {
result.put(Bytes.toString(CellUtil.cloneQualifier(cell)), Bytes.toString(CellUtil.cloneValue(cell)));
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
//取到多个族列的值
public static Map<String, Map<String, String>> byGet(String tableName, String rowKey) {
Map<String, Map<String, String>> results = null;
try {
Table t = getCon().getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowKey));
Result r = t.get(get);
List<Cell> cs = r.listCells();
results = cs.size() > 0 ? new HashMap<String, Map<String, String>>() : results;
for (Cell cell : cs) {
String familyName = Bytes.toString(CellUtil.cloneFamily(cell));
if (results.get(familyName) == null) {
results.put(familyName, new HashMap<String, String>());
}
results.get(familyName).put(Bytes.toString(CellUtil.cloneQualifier(cell)), Bytes.toString(CellUtil.cloneValue(cell)));
}
} catch (IOException e) {
e.printStackTrace();
}
return results;
}