首先创建一个maven项目,然后再pom.xml文件中添加

   cloudera

   https://repository.cloudera.com/artifactory/cloudera-repos

   org.apache.hbase

   hbase-server

   1.2.4

   org.apache.hbase

   hbase-client

   1.2.4

   org.apache.hbase

   hbase

   1.2.4

   org.apache.hbase

   hbase-client

   1.2.4

   org.junit.jupiter

   junit-jupiter-api

   5.3.1

   compile

  修改表定义:

  package cn.bdqn.hbase;

  import org.apache.hadoop.conf.Configuration;

  import org.apache.hadoop.hbase.*;

  import org.apache.hadoop.hbase.HBaseConfiguration;

  import org.apache.hadoop.hbase.client.*;

  import org.apache.hadoop.hbase.regionserver.BloomType;

  import org.junit.Before;

  import org.junit.Test;

  public class Demo {

  Connectionconn=null;

  @Before

  ? ? public? void getConn()throws? Exception{

  Configuration conf=HBaseConfiguration.create();//会自动加载hbase-site.xml

  ? ? ? ? conf.set("hbase.zookeeper.quorum","pro01:2181,pro02:2181,pro03:2181,pro04:2181");

  conn=ConnectionFactory.createConnection(conf);

  }

  ? ? @Test

  ? ? public void? tetsCreate()throws? Exception{

  //构建连接对象

  ? ? ? ? ;

  //从连接中构造一个DDl操作器

  ? ? ? ? Admin admin=conn.getAdmin();

  //创建一个表定义描述对象

  ? ? ? ? HTableDescriptor hTableDescriptor=new HTableDescriptor(TableName.valueOf("userlmj_info"));

  //创建列族定义描述对象

  ? ? ? ? HColumnDescriptor hColumnDescriptor_1=new HColumnDescriptor("baselmj_info");

  HColumnDescriptor hColumnDescriptor_2=new HColumnDescriptor("extralmj_info");

  hColumnDescriptor_1.setMaxVersions(3);//设置列族中存储数据的最大版本数,默认是1

  //将列族定义信息对象放入表定义对象中

  ? ? ? ? hTableDescriptor.addFamily(hColumnDescriptor_1);

  hTableDescriptor.addFamily(hColumnDescriptor_2);

  //用ddl操作器对象:admin 来建表

  ? ? ? ? admin.createTable(hTableDescriptor);

  //关闭连接

  ? ? ? ? admin.close();

  conn.close();

  }

  ? ? @Test

  ? ? public void deleted()throws Exception{

  Admin admin=conn.getAdmin();

  //停用表

  ? ? ? ? admin.disableTable(TableName.valueOf("userlmj_info"));

  //删除表

  ? ? ? ? admin.deleteTable(TableName.valueOf("userlmj_info"));

  admin.close();

  conn.close();

  }

  ? ? @Test

  ? ? public void uodateser()throws Exception{

  Admin admin=conn.getAdmin();

  //取出旧的表定义信息

  ? ? ? ? HTableDescriptor tableDescriptor=admin.getTableDescriptor(TableName.valueOf("user_info"));

  //新构造一个列族定义

  ? ? ? ? HColumnDescriptor hColumnDescriptor=new HColumnDescriptor("other_info");

  hColumnDescriptor.setBloomFilterType(BloomType.ROW);//设置该列族的布隆过滤器类型

  //将列族添加到表定义中

  ? ? ? ? tableDescriptor.addFamily(hColumnDescriptor);

  admin.modifyTable(TableName.valueOf("user_info"),tableDescriptor);

  }

  }

  修改表数据:

  package cn.bdqn.hbase;

  import org.apache.hadoop.conf.Configuration;

  import org.apache.hadoop.hbase.Cell;

  import org.apache.hadoop.hbase.CellScanner;

  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.util.ArrayList;

  public class Demo2 {

  Connectionconn=null;

  @Before

  ? ? public? void getConn()throws? Exception{

  Configuration conf=HBaseConfiguration.create();//会自动加载hbase-site.xml

  ? ? ? ? conf.set("hbase.zookeeper.quorum","pro01:2181,pro02:2181,pro03:2181,pro04:2181");

  conn=ConnectionFactory.createConnection(conf);

  }

  ? ? @Test

  ? ? public? void testPut()throws? Exception{

  //获取一个指定操作表的table对象

  ? ? ? Table table=conn.getTable(TableName.valueOf("user_info"));

  //构造要插入的数据为一个put类型的对象

  ? ? ? ? Put put=new Put(Bytes.toBytes(007));

  put.addColumn(Bytes.toBytes("base_info"),Bytes.toBytes("username"),Bytes.toBytes("李明京"));

  put.addColumn(Bytes.toBytes("base_info"),Bytes.toBytes("age"),Bytes.toBytes(18));

  put.addColumn(Bytes.toBytes("extra_info"),Bytes.toBytes("addr"),Bytes.toBytes("北京海淀"));

  Put put2=new Put(Bytes.toBytes("005"));

  put2.addColumn(Bytes.toBytes("base_info"),Bytes.toBytes("username"),Bytes.toBytes("lmj"));

  put2.addColumn(Bytes.toBytes("base_info"),Bytes.toBytes("age"),Bytes.toBytes("19"));

  put2.addColumn(Bytes.toBytes("extra_info"),Bytes.toBytes("addr"),Bytes.toBytes("山西临汾"));

  Put put3=new Put(Bytes.toBytes("008"));

  put3.addColumn(Bytes.toBytes("base_info"),Bytes.toBytes("username"),Bytes.toBytes("laoli"));

  put3.addColumn(Bytes.toBytes("base_info"),Bytes.toBytes("age"),Bytes.toBytes("89"));

  put3.addColumn(Bytes.toBytes("extra_info"),Bytes.toBytes("addr"),Bytes.toBytes("山东菏泽"));

  ArrayList puts=new ArrayList();

  //插入数据

  ? ? ? ? puts.add(put2);

  puts.add(put);

  puts.add(put3);

  table.put(puts);

  table.close();

  conn.close();

  }

  ? ? @Test

  ? ? public void delete()throws Exception{

  Table table=conn.getTable(TableName.valueOf("user_info"));

  //构造一个对象封装要删除的信息

  ? ? ? ? Delete delete1=new Delete(Bytes.toBytes(007));

  Delete delete2=new Delete(Bytes.toBytes("008"));

  delete2.addColumn(Bytes.toBytes("extra_info"),Bytes.toBytes("addr"));

  ArrayList dels=new ArrayList();

  dels.add(delete1);

  dels.add(delete2);

  table.delete(dels);

  table.close();

  conn.close();

  }

  ? ? @Test

  ? ? public? void testPuts()throws? Exception{

  //获取一个指定操作表的table对象

  ? ? ? ? Table table=conn.getTable(TableName.valueOf("user_info"));

  //构造要插入的数据为一个put类型的对象

  ? ? ? ? Put put=new Put(Bytes.toBytes(007));

  put.addColumn(Bytes.toBytes("base_info"),Bytes.toBytes("username"),Bytes.toBytes("李明京"));

  put.addColumn(Bytes.toBytes("base_info"),Bytes.toBytes("age"),Bytes.toBytes(18));

  put.addColumn(Bytes.toBytes("extra_info"),Bytes.toBytes("addr"),Bytes.toBytes("北京海淀"));

  Put put2=new Put(Bytes.toBytes("005"));

  put2.addColumn(Bytes.toBytes("base_info"),Bytes.toBytes("username"),Bytes.toBytes("lmj"));

  put2.addColumn(Bytes.toBytes("base_info"),Bytes.toBytes("age"),Bytes.toBytes("19"));

  put2.addColumn(Bytes.toBytes("extra_info"),Bytes.toBytes("addr"),Bytes.toBytes("山西临汾"));

  Put put3=new Put(Bytes.toBytes("008"));

  put3.addColumn(Bytes.toBytes("base_info"),Bytes.toBytes("username"),Bytes.toBytes("laoli"));

  put3.addColumn(Bytes.toBytes("base_info"),Bytes.toBytes("age"),Bytes.toBytes("89"));

  put3.addColumn(Bytes.toBytes("extra_info"),Bytes.toBytes("addr"),Bytes.toBytes("山东菏泽"));

  ArrayList puts=new ArrayList();

  //插入数据

  ? ? ? ? puts.add(put2);

  puts.add(put);

  puts.add(put3);

  table.put(puts);

  table.close();

  conn.close();

  }

  ? ? @Test

  ? ? public? void? selects()throws Exception{

  Table table=conn.getTable(TableName.valueOf("user_info"));

  Get get=new Get("001".getBytes());

  Result result=table.get(get);

  //从结果中取用户指定的某个key的value

  ? ? ? ? byte[] value=result.getValue("base_info".getBytes(),"age".getBytes());

  System.out.println(value.toString());

  System.out.println("-----------");

  //遍历整行所有的单元格

  ? ? ? ? CellScanner cellScanner=result.cellScanner();

  while (cellScanner.advance()){

  Cell current=cellScanner.current();

  byte[] rowArray=current.getRowArray();

  byte[] familyArray=current.getFamilyArray();

  byte[] qualifierArray=current.getQualifierArray();

  byte[] valueArray=current.getValueArray();

  System.out.println("行键"+new String(rowArray,current.getRowOffset(),current.getRowLength()));

  System.out.println("列族名"+new String(familyArray,current.getFamilyOffset(),current.getFamilyLength()));

  System.out.println("列名"+new String(qualifierArray,current.getQualifierOffset(),current.getQualifierLength()));

  System.out.println("value"+new String(valueArray,current.getValueOffset(),current.getValueLength()));

  }

  table.close();

  conn.close();

  }

  @Test

  public? void? testPlus()throws? Exception{

  Table table=conn.getTable(TableName.valueOf("user_info"));

  ArrayList puts=new ArrayList();

  for (int i=0;i<1000;i++){

  Put put=new Put(Bytes.toBytes(""+i));

  put.addColumn(Bytes.toBytes("base_info"),Bytes.toBytes("username"),Bytes.toBytes("李明京"+i));

  put.addColumn(Bytes.toBytes("base_info"),Bytes.toBytes("age"),Bytes.toBytes(18+i));

  put.addColumn(Bytes.toBytes("extra_info"),Bytes.toBytes("addr"),Bytes.toBytes("北京海淀"));

  puts.add(put);

  }

  table.put(puts);

  table.close();

  conn.close();

  }

  @Test

  public? void hangjian()throws? Exception{

  Table table=conn.getTable(TableName.valueOf("user_info"));

  //包含起始行,不包含结束行,如果真的想查询末尾的话可以在后面添加一个不可见字节

  ? ? Scan scan=new Scan("10".getBytes(),"20\000".getBytes());

  ResultScanner scanner=table.getScanner(scan);

  Iterator iterator=scanner.iterator();

  while(iterator.hasNext()){

  Result next=iterator.next();

  while (next.advance()){

  Cell current=next.current();

  byte[] rowArray=current.getRowArray();

  byte[] familyArray=current.getFamilyArray();

  byte[] qualifierArray=current.getQualifierArray();

  byte[] valueArray=current.getValueArray();

  System.out.println("行键"+new String(rowArray,current.getRowOffset(),current.getRowLength()));

  System.out.println("列族名"+new String(familyArray,current.getFamilyOffset(),current.getFamilyLength()));

  System.out.println("列名"+new String(qualifierArray,current.getQualifierOffset(),current.getQualifierLength()));

  System.out.println("value"+new String(valueArray,current.getValueOffset(),current.getValueLength()));

  }

  System.out.println("---------------");

  }

  }

  }