DDL
判断表是否存在
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
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.HBaseAdmin;
import java.io.IOException;
public class test {
private static Connection connection = null;
private static Admin admin = null;
static{
try {
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "hadoop01,hadoop02,hadoop03");
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
public static boolean isTableExist(String tableName) throws IOException {
boolean exists = admin.tableExists(TableName.valueOf(tableName));
return exists;
}
public static void close() throws IOException {
if(admin != null){
admin.close();
}
if(connection!=null){
connection.close();
}
}
public static void main(String[] args) throws IOException {
System.out.println(isTableExist("Stu"));
close();
}
}
创建表
public static void createTable(String tableName, String... cfs) throws IOException {
if (cfs.length <= 0) {
System.out.println("请设置列族信息!");
return;
}
if (isTableExist(tableName)) {
System.out.println(tableName + "表已存在!");
return;
}
HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
for (String cf : cfs) {
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf);
hTableDescriptor.addFamily(hColumnDescriptor);
}
admin.createTable(hTableDescriptor);
}
创建命名空间
public static void createNameSpace(String ns){
NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create(ns).build();
try {
admin.createNamespace(namespaceDescriptor);
}catch (NamespaceExistException e){
System.out.println(ns+"命名空间已存在!");
}
catch (IOException e) {
e.printStackTrace();
}
}
删除表
public static void dropTable(String tableName) throws IOException {
if(!isTableExist(tableName)){
System.out.println(tableName+"表不存在");
return;
}
admin.disableTable(TableName.valueOf(tableName));
admin.deleteTable(TableName.valueOf(tableName));
}
DML
向表插入数据
public static void putData(String tableName, String rowKey, String cf, String cn, String value) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(Bytes.toBytes(cf),Bytes.toBytes(cn),Bytes.toBytes(value));
table.put(put);
table.close();
}
获取数据
public static void getData(String tableName,String rowKey,String cf,String cn) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowKey));
get.addFamily(Bytes.toBytes(cf));
get.addColumn(Bytes.toBytes(cf),Bytes.toBytes(cn));
get.setMaxVersions(5);
Result result = table.get(get);
for (Cell cell : result.rawCells()) {
System.out.println("CF:"+Bytes.toString(cloneFamily(cell))+
",CN:"+Bytes.toString(CellUtil.cloneQualifier(cell))+
",Value:"+Bytes.toString(CellUtil.cloneValue(cell))
);
}
table.close();
}
扫描表
public static void scanTable(String tableName) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
ResultScanner resultScanner = table.getScanner(scan);
for(Result result:resultScanner){
for (Cell cell : result.rawCells()) {
System.out.println("CF:"+Bytes.toString(cloneFamily(cell))+
",CN:"+Bytes.toString(CellUtil.cloneQualifier(cell))+
",Value:"+Bytes.toString(CellUtil.cloneValue(cell))
);
}
}
table.close();
}
删除数据
public static void deleteData(String tableName,String rowKey,String cf,String cn) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(Bytes.toBytes(rowKey));
delete.addColumns(Bytes.toBytes(cf),Bytes.toBytes(cn));
table.delete(delete);
table.close();
}