一、创建maven工程,并添加依赖包
<!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-client -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-common -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-common</artifactId>
<version>1.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-server -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.2.0</version>
</dependency>
二、创建hbase客户类进行测试
导包
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 org.junit.Test;
import java.io.IOException;
创建表操作
@Test
public void createTable() throws IOException {
Configuration conf= HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","lyq");
conf.set("hbase.zookeeper.property.clientPort","2181");
Connection conn= ConnectionFactory.createConnection(conf);
Admin admin = conn.getAdmin();
HTableDescriptor student = new HTableDescriptor(TableName.valueOf("student"));
student.addFamily(new HColumnDescriptor("info"));
student.addFamily(new HColumnDescriptor("score"));
admin.createTable(student);
}
判断表是否存在
@Test
public void isTableExists() throws IOException {
Configuration conf= HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","lyq");
conf.set("hbase.zookeeper.property.clientPort","2181");
Connection conn= ConnectionFactory.createConnection(conf);
Admin admin = conn.getAdmin();
System.out.println(admin.tableExists(TableName.valueOf("student2")));
}
向表中插入数据
@Test
public void putDataTable() throws IOException {
Configuration conf= HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","lyq");
conf.set("hbase.zookeeper.property.clientPort","2181");
Connection conn= ConnectionFactory.createConnection(conf);
Table student = conn.getTable(TableName.valueOf("student"));
Put put = new Put(Bytes.toBytes("1001"));
put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("name"),Bytes.toBytes("zhangsan"));
put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("sex"),Bytes.toBytes("female"));
put.addColumn(Bytes.toBytes("score"),Bytes.toBytes("course"),Bytes.toBytes("math"));
put.addColumn(Bytes.toBytes("score"),Bytes.toBytes("score"),Bytes.toBytes("90"));
student.put(put);
}
查看一条数据
@Test
public void getDataTable() throws IOException {
Configuration conf= HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","lyq");
conf.set("hbase.zookeeper.property.clientPort","2181");
Connection conn= ConnectionFactory.createConnection(conf);
Table student = conn.getTable(TableName.valueOf("student"));
Get get=new Get(Bytes.toBytes("1001"));
Result result=student.get(get);
Cell[] cells=result.rawCells();
for (Cell cell : cells) {
System.out.println("rowkey:"+Bytes.toString(CellUtil.cloneRow(cell)));
System.out.println("列族:"+Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("列名:"+Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("值:"+Bytes.toString(CellUtil.cloneValue(cell)));
System.out.println("-----------------------");
}
}
删除表操作
@Test
public void dropTable() throws IOException {
Configuration conf= HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","lyq");
conf.set("hbase.zookeeper.property.clientPort","2181");
Connection conn= ConnectionFactory.createConnection(conf);
Admin admin = conn.getAdmin();
admin.disableTable(TableName.valueOf("student"));
admin.deleteTable(TableName.valueOf("student"));
}