pom.xml
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-common</artifactId>
<version>1.1.2</version>
</dependency>
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.BinaryComparator;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.FamilyFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class HbaseTest {
Configuration conf=null;
private Connection connection=null;
private Table table=null;
//表管理员
private Admin admin=null;
@Before
public void init() throws IOException {
conf=HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","192.168.111.131");
conf.set("hbase.zookeeper.property.clientPort","2181");
connection = ConnectionFactory.createConnection(conf);
admin =connection.getAdmin();
}
@Test
public void test(){
System.out.println(connection);
System.out.println(admin);
}
@Test
public void createNameSpace(){
NamespaceDescriptor test = NamespaceDescriptor.create("test").build();
try {
admin.createNamespace(test);
// admin.deleteNamespace("test");
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void createTable() throws IOException {
TableName tableName=TableName.valueOf("test:student");
HTableDescriptor desc =new HTableDescriptor(tableName);
HColumnDescriptor family1=new HColumnDescriptor("info1");
HColumnDescriptor family2=new HColumnDescriptor("info2");
desc.addFamily(family1);
desc.addFamily(family2);
admin.createTable(desc);
}
@Test
public void deleteTable() throws IOException {
TableName tableName = TableName.valueOf("test:student");
admin.disableTable(tableName);
admin.deleteTable(tableName);
}
@Test
public void insertData() throws IOException {
table=connection.getTable(TableName.valueOf("test:student"));
// Put rowkey1 = new Put(Bytes.toBytes("rowkey1"));
// rowkey1.add(Bytes.toBytes("info1"),Bytes.toBytes("name"),Bytes.toBytes("zs"));
// rowkey1.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("name"),Bytes.toBytes("zs"));
// rowkey1.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("age"),Bytes.toBytes("23"));
// rowkey1.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("gender"),Bytes.toBytes("male"));
// rowkey1.addColumn(Bytes.toBytes("info2"),Bytes.toBytes("adress"),Bytes.toBytes("nanjing"));
// rowkey1.addColumn(Bytes.toBytes("info2"),Bytes.toBytes("name"),Bytes.toBytes("zb"));
// rowkey1.addColumn(Bytes.toBytes("info2"),Bytes.toBytes("class"),Bytes.toBytes("kb15"));
// table.put(rowkey1);
//
List<Put>list=new ArrayList<>();
Put put1 =new Put(Bytes.toBytes("rowkey2"));
put1.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("name"),Bytes.toBytes("zs"));
put1.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("age"),Bytes.toBytes("23"));
put1.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("gender"),Bytes.toBytes("male"));
put1.addColumn(Bytes.toBytes("info2"),Bytes.toBytes("adress"),Bytes.toBytes("nanjing"));
put1.addColumn(Bytes.toBytes("info2"),Bytes.toBytes("name"),Bytes.toBytes("zb"));
put1.addColumn(Bytes.toBytes("info2"),Bytes.toBytes("class"),Bytes.toBytes("kb15"));
Put put2=new Put(Bytes.toBytes("rowkey3"));
put2.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("name"),Bytes.toBytes("zs"));
put2.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("age"),Bytes.toBytes("23"));
put2.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("gender"),Bytes.toBytes("male"));
put2.addColumn(Bytes.toBytes("info2"),Bytes.toBytes("adress"),Bytes.toBytes("nanjing"));
put2.addColumn(Bytes.toBytes("info2"),Bytes.toBytes("name"),Bytes.toBytes("zb"));
put2.addColumn(Bytes.toBytes("info2"),Bytes.toBytes("class"),Bytes.toBytes("kb15"));
list.add(put1);
list.add(put2);
table.put(list);
}
@Test
public void delete() throws IOException{
table =connection.getTable(TableName.valueOf("test:student"));
// 删除 rowkey1
// Delete rowkey1=new Delete(Bytes.toBytes("rowkey1"));
// table.delete(rowkey1);
//删除rowkey2 的info2列族
// Delete delete=new Delete(Bytes.toBytes("rowkey2"));
// delete.addFamily(Bytes.toBytes("info2"));
// table.delete(delete);
//删除rowkey3 列族为info1下的gender列
Delete rowkey3 = new Delete(Bytes.toBytes("rowkey3"));
rowkey3.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("gender"));
table.delete(rowkey3);
}
@Test
public void getData() throws IOException {
table =connection.getTable(TableName.valueOf("kb15:student"));
Get get = new Get(Bytes.toBytes("rowkey1"));
Result result=table.get(get);
byte[] name = result.getValue(Bytes.toBytes("baseInfo"), Bytes.toBytes("name"));
byte[] age = result.getValue(Bytes.toBytes("baseInfo"), Bytes.toBytes("age"));
System.out.println(Bytes.toString(name));
System.out.println(Bytes.toString(age));
}
@Test
public void scanData() throws IOException {
table =connection.getTable(TableName.valueOf("kb15:student"));
Scan scan =new Scan();
scan.addColumn(Bytes.toBytes("baseInfo"),Bytes.toBytes("age"));
scan.addColumn(Bytes.toBytes("baseInfo"),Bytes.toBytes("name"));
scan.setStartRow(Bytes.toBytes("rowkey1"));
scan.setStopRow(Bytes.toBytes("rowkey3"));
ResultScanner scanner=table.getScanner(scan);
for (Result result:
scanner) {
byte[] name = result.getValue(Bytes.toBytes("baseInfo"), Bytes.toBytes("name"));
byte[] age = result.getValue(Bytes.toBytes("baseInfo"), Bytes.toBytes("age"));
byte[] row = result.getRow();
System.out.println(Bytes.toString(name));
System.out.println(Bytes.toString(age));
System.out.println(Bytes.toString(row));
}
}
@Test
public void getFilterRowKey() throws IOException {
table =connection.getTable(TableName.valueOf("kb15:student"));
Scan scan=new Scan();
FamilyFilter familyFilter=new FamilyFilter(CompareFilter.CompareOp.GREATER.EQUAL,
new BinaryComparator(Bytes.toBytes("baseInfo")));
scan.setFilter(familyFilter);
ResultScanner scanner = table.getScanner(scan);
for (Result result:
scanner) {
byte[] name = result.getValue(Bytes.toBytes("baseInfo"), Bytes.toBytes("name"));
byte[] age = result.getValue(Bytes.toBytes("baseInfo"), Bytes.toBytes("age"));
byte[] row = result.getRow();
System.out.println(Bytes.toString(name));
System.out.println(Bytes.toString(age));
System.out.println(Bytes.toString(row));
}
}
@After
public void close() throws IOException{
if(table!=null)
table.close();
if(connection!=null)
connection.close();
}
}
IDEA下实现Hbase增删改查
于 2021-11-22 18:46:08 首次发布