相关依赖:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-protocol</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-common</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-mapreduce</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-zookeeper</artifactId>
<version>2.2.4</version>
</dependency>
代码:
package com.yuan;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class AppTest {
private Configuration configuration = null;
Connection connection = null;
@Before
public void setUp() throws Exception {
configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "192.168.220.128:2181");
connection = ConnectionFactory.createConnection(configuration);
System.out.println(" 开启连接");
}
@Test
public void exists() throws IOException {
boolean exists = connection.getAdmin().tableExists(TableName.valueOf("stu"));
System.out.println(exists);
}
@Test
public void create() throws IOException {
TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(TableName.valueOf("table2"));
for (int i = 0; i < 5; i++) {
ColumnFamilyDescriptor descriptor = ColumnFamilyDescriptorBuilder.of("yuan" + i);
builder.setColumnFamily(descriptor);
}
connection.getAdmin().createTable(builder.build());
System.out.println("表创建成功!");
}
@Test
public void enabletable() throws IOException {
connection.getAdmin().enableTable(TableName.valueOf("table2"));
System.out.println("已经启用!");
}
@Test
public void disabletable() throws IOException {
connection.getAdmin().disableTable(TableName.valueOf("table2"));
System.out.println("已禁用!");
}
@Test
public void delettable() throws IOException {
disabletable();
connection.getAdmin().deleteTable(TableName.valueOf("table2"));
System.out.println("删除成功!!");
}
@Test
public void listTable() throws IOException {
TableName[] tableNames = connection.getAdmin().listTableNames();
for (TableName name : tableNames) {
System.out.println(name);
}
}
@Test
public void addData() throws IOException {
Table table = connection.getTable(TableName.valueOf("table"));
Put put = new Put(Bytes.toBytes("row2"));
put.addColumn(Bytes.toBytes("c1"), Bytes.toBytes("c1"), Bytes.toBytes("hhaha"));
table.put(put);
table.close();
System.out.println("插入数据成功");
}
@Test
public void addSomeData() throws IOException {
Table table = connection.getTable(TableName.valueOf("table"));
Put put = null;
for (int i = 0; i < 100; i++) {
put = new Put(Bytes.toBytes("row" + i));
put.addColumn(Bytes.toBytes("c1"), Bytes.toBytes("c" + i), Bytes.toBytes("" + i));
table.put(put);
}
table.close();
System.out.println("插入数据成功");
}
@Test
public void getData() throws IOException {
Table table = connection.getTable(TableName.valueOf("table"));
Get get = new Get(Bytes.toBytes("row1"));
get.addColumn(Bytes.toBytes("c1"), Bytes.toBytes("c1"));
Result result = table.get(get);
for (Cell cell : result.rawCells()) {
System.out.println("行键:" + Bytes.toString(result.getRow()));
System.out.println("\t列族" + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("\t列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("\t值:" + Bytes.toString(CellUtil.cloneValue(cell)));
}
table.close();
}
@Test
public void getRow() throws IOException {
Table table = connection.getTable(TableName.valueOf("table"));
Get get = new Get(Bytes.toBytes("row1"));
Result result = table.get(get);
for (Cell cell : result.rawCells()) {
System.out.print("行键:" + Bytes.toString(result.getRow()));
System.out.print("\t列族" + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.print("\t列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.print("\t值:" + Bytes.toString(CellUtil.cloneValue(cell)));
System.out.println("\t时间戳:" + cell.getTimestamp());
}
table.close();
}
@Test
public void getAllRows() throws IOException {
Table table = connection.getTable(TableName.valueOf("table"));
Scan scan = new Scan();
scan.readAllVersions();
ResultScanner resultScanner = table.getScanner(scan);
for (Result result : resultScanner) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.print("行键:" + Bytes.toString(CellUtil.cloneRow(cell)));
System.out.print("\t列族" + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.print("\t列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("\t值:" + Bytes.toString(CellUtil.cloneValue(cell)));
}
}
table.close();
}
@Test
public void deletDate() throws IOException {
Table table = connection.getTable(TableName.valueOf("table"));
Delete delete = new Delete(Bytes.toBytes("row1"));
table.delete(delete);
table.close();
System.out.println("删除数据成功");
}
@Test
public void conut() throws IOException {
Table table = connection.getTable(TableName.valueOf("table"));
Scan scan = new Scan();
scan.readAllVersions();
int n = 0;
ResultScanner resultScanner = table.getScanner(scan);
for (Result res : resultScanner) {
n++;
}
System.out.println("共:"+n+" 条记录!");
}
@Test
public void deletSomeDate() throws IOException {
Table table = connection.getTable(TableName.valueOf("table"));
List<String> rows = new ArrayList<>();
rows.add("row0");
rows.add("row1");
rows.add("row2");
rows.add("row3");
rows.add("row4");
rows.add("row5");
rows.add("row6");
List<Delete> deleteList = new ArrayList<>();
for (String row : rows) {
Delete delete = new Delete(Bytes.toBytes(row));
deleteList.add(delete);
}
table.delete(deleteList);
table.close();
System.out.println("删除数据成功");
}
}