import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class AdminTest {
public static void main(String[] args) throws IOException {
// System.out.println(AdminTest.TableisExist("stu"));
// AdminTest.addTable("test1","cf1","cf2");
// AdminTest.deleteTable("test2");
// AdminTest.addValue("stu");
// AdminTest.addValues("stu");
AdminTest.getData("stu");
}
//判断表是否存在
public static boolean TableisExist(String tablename) throws IOException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
conf.set("hbase.zookeeper.property.clientPort","2181");
Connection conn = ConnectionFactory.createConnection(conf);
HBaseAdmin admin = (HBaseAdmin) conn.getAdmin();
admin.close();
return admin.tableExists(tablename);
}
//添加一个表
public static void addTable(String tablename,String... cfs) throws IOException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
conf.set("hbase.zookeeper.property.clientPort","2181");
Connection conn = ConnectionFactory.createConnection(conf);
HBaseAdmin admin = (HBaseAdmin) conn.getAdmin();//获取客户端对象
//创建表描述器
HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tablename));
//循环添加列族信息
for (String cf : cfs) {
//添加列族描述器
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf);
hTableDescriptor.addFamily(hColumnDescriptor);
}
admin.createTable(hTableDescriptor);
admin.close();
}
//删除一个表
public static void deleteTable(String tablename) throws IOException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
conf.set("hbase.zookeeper.property.clientPort","2181");
Connection conn = ConnectionFactory.createConnection(conf);
HBaseAdmin admin = (HBaseAdmin) conn.getAdmin();//获取客户端对象
//删除表之前必选先让表下线
admin.disableTable(TableName.valueOf(tablename));
//删除表
admin.deleteTable(tablename);
admin.close();
}
//在表中插入一条数据
public static void addValue(String tablename) throws IOException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
conf.set("hbase.zookeeper.property.clientPort","2181");
Connection conn = ConnectionFactory.createConnection(conf);
Table table = conn.getTable(TableName.valueOf(tablename));
//插入数据之前指定更具体的RowKey
Put put = new Put(Bytes.toBytes("1001"));
//调用addColumn方法指定所要插入的列族,列修饰符
put.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("phone"),Bytes.toBytes("123"));
table.put(put);
table.close();
conn.close();
}
//在表中插入多条数据
public static void addValues(String tablename) throws IOException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
conf.set("hbase.zookeeper.property.clientPort","2181");
Connection conn = ConnectionFactory.createConnection(conf);
Table table = conn.getTable(TableName.valueOf(tablename));
List <Put> puts = new ArrayList<Put>();//利用集合向表中插入多条数据
Put put1 = new Put(Bytes.toBytes("1003"));
//调用addColumn方法指定所要插入的列族,列修饰符
put1.addColumn(Bytes.toBytes("info2"),Bytes.toBytes("addr"),Bytes.toBytes("beijing"));
Put put2 = new Put(Bytes.toBytes("1003"));
put2.addColumn(Bytes.toBytes("info2"),Bytes.toBytes("age"),Bytes.toBytes("18"));
puts.add(put1);
puts.add(put2);
table.put(puts);
table.close();
conn.close();
}
//查询现有表中的一行数据
public static void getData(String tablename) throws IOException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
conf.set("hbase.zookeeper.property.clientPort","2181");
Connection conn = ConnectionFactory.createConnection(conf);
Table table = conn.getTable(TableName.valueOf(tablename));
Get get = new Get(Bytes.toBytes("1003"));//获取RowKey为1003这一行的数据
get.setMaxVersions(2);//设置最大版本为2
get.addColumn(Bytes.toBytes("info2"),Bytes.toBytes("num"));
Result result = table.get(get);
for(Cell cell : result.rawCells()){
//分别获取键值,列族,列,值
System.out.println("RowKey: "+new String(CellUtil.cloneRow(cell)));
System.out.println("CF: "+new String(CellUtil.cloneFamily(cell)));
System.out.println("CN: "+new String(CellUtil.cloneQualifier(cell)));
System.out.println("Value: "+new String(CellUtil.cloneValue(cell)));
}
table.close();
conn.close();
}
}
Hbase中API的DML和DDL操作
猜你喜欢
转载自blog.csdn.net/weixin_44080445/article/details/107570596
今日推荐
周排行