IDEA新建项目,引入POM文件
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
</dependency>
新建配置文件
package com.zzj.config;
import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import java.io.IOException;
@Slf4j
public final class HbaseConfig {
private Admin admin;
private Configuration configuration;
private HBaseConfiguration hBaseConfiguration;
private String zkUrl = "axe1:2181,axe2:2181,axe3:2181";
private Connection connection;
public HbaseConfig() {
configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum",zkUrl);
//configuration.addResource(""); 可以使用hbase-site.xml代替,xml中含有具体的配置文件
try {
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
} catch (Exception e) {
e.printStackTrace();
}
}
public Admin getAdmin() {
return admin;
}
}
新建JavaTest
import com.zzj.config.HbaseConfig;
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.util.List;
import java.util.Random;
public class CrudTest {
@Test
public void create() throws Exception {
HbaseConfig hbaseConfig = new HbaseConfig();
Admin admin = hbaseConfig.getAdmin();
boolean exists = ((HBaseAdmin) admin).tableExists("test");
System.out.println(String.valueOf(exists));
exists = ((HBaseAdmin) admin).tableExists("test444");
System.out.println(String.valueOf(exists));
TableName name = TableName.valueOf("wdnmd");
HTableDescriptor table = new HTableDescriptor(name);
HColumnDescriptor c1 = new HColumnDescriptor("c1");
HColumnDescriptor c2 = new HColumnDescriptor("c2");
HColumnDescriptor c3 = new HColumnDescriptor("c3");
exists = ((HBaseAdmin) admin).tableExists("wdnmd");
if(exists){
System.out.println(String.valueOf(exists));
admin.addColumn(name,c1);
}else{
table.addFamily(c2);
table.addFamily(c3);
admin.createTable(table);
}
}
@Test
public void listTables() throws Exception {
HbaseConfig hbaseConfig = new HbaseConfig();
Admin admin = hbaseConfig.getAdmin();
TableName[] tableNames = admin.listTableNames();
for(TableName name:tableNames){
System.out.println(name.getNameAsString());
}
}
@Test
public void insert() throws Exception{
HbaseConfig hbaseConfig = new HbaseConfig();
Admin admin = hbaseConfig.getAdmin();
Configuration configuration = admin.getConfiguration();
Connection connection = admin.getConnection();
Table table = connection.getTable(TableName.valueOf("test1"));
HTableDescriptor tableDescriptor = table.getTableDescriptor();
HColumnDescriptor[] hColumnDescriptors = tableDescriptor.getColumnFamilies();
Put put = new Put(Bytes.toBytes("row1"));
for(HColumnDescriptor column:hColumnDescriptors){
if("c3".equals(column.getNameAsString())){
}
}
// put.addColumn("c2".getBytes(),"srgdr".getBytes(),"wrtrg".getBytes());
// put.addColumn("c3".getBytes(),"dsdgg".getBytes(),"阿桑的歌".getBytes());
// put.addColumn("c3".getBytes(),"wocaole".getBytes(),"dajiba".getBytes());
put.addColumn("f1".getBytes(),"f1_column".getBytes(),"f1_value".getBytes());
table.put(put);
table.close();
}
@Test
public void getColum() throws Exception {
//相当于关系数据库中的k-v查询中,根据key查询value,此处hbase的key时多个key合并的,key=rowKey+columnKey+qualifierKey,新增时,亦是如此
System.out.println("111111111111");
Get get = new Get(Bytes.toBytes("row1"));
//此处指定了特定的column和qualifier,在下面的result中 只查询出指定的结果,如果此处不指定column则会查询出第一个结果
//get.addColumn("f1".getBytes(),"srgdr".getBytes());
HbaseConfig hbaseConfig = new HbaseConfig();
Admin admin = hbaseConfig.getAdmin();
Connection connection = admin.getConnection();
Table table = connection.getTable(TableName.valueOf("test1"));
Result result = table.get(get);
String f = Bytes.toString(result.getRow());
String vl = Bytes.toString(result.value());
List<Cell> cells = result.getColumnCells("f1".getBytes(),"f1_qua".getBytes());
for(Cell cell : cells){
String family = Bytes.toString(CellUtil.cloneFamily(cell));
String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
String value = Bytes.toString(CellUtil.cloneValue(cell));
}
System.out.println(f);
System.out.println(vl);
}
@Test
public void delete() throws Exception{
Delete delete = new Delete(Bytes.toBytes("542131"));
HbaseConfig hbaseConfig = new HbaseConfig();
Admin admin = hbaseConfig.getAdmin();
Connection connection = admin.getConnection();
Table table = connection.getTable(TableName.valueOf("wdnmd"));
//table.delete(delete);//整个row全部删除
delete.addColumn("c2".getBytes(),"srgdr".getBytes());//删除指定的column
table.delete(delete);
}
@Test
public void scanner() throws Exception{
HbaseConfig hbaseConfig = new HbaseConfig();
Admin admin = hbaseConfig.getAdmin();
Connection connection = admin.getConnection();
Table table = connection.getTable(TableName.valueOf("scannerTable"));
Scan scan = new Scan();
ResultScanner results = table.getScanner(scan);
//没有条件的时候是全表扫描
for(Result result:results){
// List<Cell> cells = result.getColumnCells("f1".getBytes(),"f1_qua".getBytes());
List<Cell> cells = result.listCells();
for(Cell cell : cells){
String family = Bytes.toString(CellUtil.cloneFamily(cell));
String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
String value = Bytes.toString(CellUtil.cloneValue(cell));
//System.out.println(family+" "+qualifier +" " +value);
}
}
for(int i=0;i<100;i++){
String rowName = String.valueOf(System.currentTimeMillis());
Put put = new Put(Bytes.toBytes(rowName));
put.addColumn("testFamily".getBytes(),("f_"+rowName).getBytes(),(rowName+ new Random(1000).nextInt()).getBytes());
//table.put(put);
}
table.close();
scan = new Scan();
scan.setStartRow("1558340047343".getBytes());
scan.setStopRow ("1558340207922".getBytes());
table.getScanner(scan);
results = table.getScanner(scan);
int co =0 ;
for(Result result:results){
List<Cell> cells = result.listCells();
for(Cell cell : cells){
String family = Bytes.toString(CellUtil.cloneFamily(cell));
String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
String value = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println(++co);
System.out.println(family+" "+qualifier +" " +value);
}
}
}
}