HBase的java代码开发(详细代码)

第一步:创建maven工程,导入jar包

<repositories>
<repository>
    <id>cloudera</id>
    <url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
</repository>
</repositories>
<dependencies>
<dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-client</artifactId>
    <version>2.6.0-mr1-cdh5.14.0</version>
</dependency>
<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-client</artifactId>
    <version>1.2.0-cdh5.14.0</version>
</dependency>
<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-server</artifactId>
    <version>1.2.0-cdh5.14.0</version>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.14.3</version>
    <scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.0</version>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
            <encoding>UTF-8</encoding>
            <!-- <verbal>true</verbal>-->
        </configuration>
    </plugin>
    <!--将我们其他用到的一些jar包全部都打包进来 -->
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.4.3</version>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>shade</goal>
                </goals>
                <configuration>
                    <minimizeJar>false</minimizeJar>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>
</build>

第二步:开发javaAPI操作HBase表数据

1、创建表myuser

@Test
    public void createTable() throws IOException {
    //创建配置文件对象,并指定zookeeper的连接地址
    Configuration configuration = HBaseConfiguration.create();
    configuration.set("hbase.zookeeper.property.clientPort", "2181");
    configuration.set("hbase.zookeeper.quorum", "node01,node02,node03");
    //集群配置↓
    //configuration.set("hbase.zookeeper.quorum", "101.236.39.141,101.236.46.114,101.236.46.113");
    configuration.set("hbase.master", "node01:60000");

    Connection connection = ConnectionFactory.createConnection(configuration);
    Admin admin = connection.getAdmin();
    //通过HTableDescriptor来实现我们表的参数设置,包括表名,列族等等
    HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf("myuser"));
    //添加列族
    hTableDescriptor.addFamily(new HColumnDescriptor("f1"));
    //添加列族
    hTableDescriptor.addFamily(new HColumnDescriptor("f2"));
    //创建表
    boolean myuser = admin.tableExists(TableName.valueOf("myuser"));
    if(!myuser){
        admin.createTable(hTableDescriptor);
    }
    //关闭客户端连接
    admin.close();
}

2、向表中添加数据

/**
 * 插入数据
 */
@Test
public  void  addDatas() throws IOException {
    //获取连接
    Configuration configuration = HBaseConfiguration.create();
    configuration.set("hbase.zookeeper.quorum", "node01:2181,node02:2181");
    Connection connection = ConnectionFactory.createConnection(configuration);
    //获取表
    Table myuser = connection.getTable(TableName.valueOf("myuser"));
    //创建put对象,并指定rowkey
    Put put = new Put("0001".getBytes());
    put.addColumn("f1".getBytes(),"id".getBytes(), Bytes.toBytes(1));
    put.addColumn("f1".getBytes(),"name".getBytes(), Bytes.toBytes("张三"));
    put.addColumn("f1".getBytes(),"age".getBytes(), Bytes.toBytes(18));

    put.addColumn("f2".getBytes(),"address".getBytes(), Bytes.toBytes("地球人"));
    put.addColumn("f2".getBytes(),"phone".getBytes(), Bytes.toBytes("15874102589"));
    //插入数据
    myuser.put(put);
    //关闭表
    myuser.close();

}

3、查询数据

初始化一批数据到HBase当中用于查询
@Test
public void insertBatchData() throws IOException {

    //获取连接
    Configuration configuration = HBaseConfiguration.create();
    configuration.set("hbase.zookeeper.quorum", "node01:2181,node02:2181");
    Connection connection = ConnectionFactory.createConnection(configuration);
    //获取表
    Table myuser = connection.getTable(TableName.valueOf("myuser"));
    //创建put对象,并指定rowkey
    Put put = new Put("0002".getBytes());
    put.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(1));
    put.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("曹操"));
    put.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(30));
    put.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
    put.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("沛国谯县"));
    put.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("16888888888"));
    put.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("helloworld"));

    Put put2 = new Put("0003".getBytes());
    put2.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(2));
    put2.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("刘备"));
    put2.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(32));
    put2.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
    put2.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("幽州涿郡涿县"));
    put2.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("17888888888"));
    put2.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("talk is cheap , show me the code"));


    Put put3 = new Put("0004".getBytes());
    put3.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(3));
    put3.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("孙权"));
    put3.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(35));
    put3.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
    put3.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("下邳"));
    put3.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("12888888888"));
    put3.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("what are you 弄啥嘞!"));

    Put put4 = new Put("0005".getBytes());
    put4.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(4));
    put4.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("诸葛亮"));
    put4.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(28));
    put4.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
    put4.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("四川隆中"));
    put4.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("14888888888"));
    put4.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("出师表你背了嘛"));

    Put put5 = new Put("0005".getBytes());
    put5.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(5));
    put5.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("司马懿"));
    put5.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(27));
    put5.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
    put5.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("哪里人有待考究"));
    put5.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("15888888888"));
    put5.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("跟诸葛亮死掐"));


    Put put6 = new Put("0006".getBytes());
    put6.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(5));
    put6.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("xiaobubu—吕布"));
    put6.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(28));
    put6.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
    put6.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("内蒙人"));
    put6.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("15788888888"));
    put6.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("貂蝉去哪了"));

    List<Put> listPut = new ArrayList<Put>();
    listPut.add(put);
    listPut.add(put2);
    listPut.add(put3);
    listPut.add(put4);
    listPut.add(put5);
    listPut.add(put6);

    myuser.put(listPut);
    myuser.close();
}

按照rowkey进行查询获取所有列的所有值

1.查询主键rowkey为0003的人
/**
 * 查询数据,按照主键id进行查询
 */
@Test
public  void searchData() throws IOException {
    Configuration configuration = HBaseConfiguration.create();
    configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
    Connection connection = ConnectionFactory.createConnection(configuration);
    Table myuser = connection.getTable(TableName.valueOf("myuser"));

    Get get = new Get(Bytes.toBytes("0003"));
    Result result = myuser.get(get);
    Cell[] cells = result.rawCells();
    //获取所有的列名称以及列的值
    for (Cell cell : cells) {
        //注意,如果列属性是int类型,那么这里就不会显示
        System.out.println(Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength()));
        System.out.println(Bytes.toString(cell.getValueArray(),cell.getValueOffset(),cell.getValueLength()));
    }

    myuser.close();

}

2.按照rowkey查询指定列族下面的指定列的值
/**
 * 通过rowkey查询指定列族下面的指定列的值
 */
@Test
public void searchData2() throws IOException {
    //获取连接
    Configuration configuration = HBaseConfiguration.create();
    configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
    Connection connection = ConnectionFactory.createConnection(configuration);
    Table myuser = connection.getTable(TableName.valueOf("myuser"));
    //通过rowKey进行查询
    Get get = new Get("0003".getBytes());
    get.addColumn("f1".getBytes(),"id".getBytes());

    Result result = myuser.get(get);
    System.out.println(Bytes.toInt(result.getValue("f1".getBytes(), "id".getBytes())));
    System.out.println(Bytes.toInt(result.getValue("f1".getBytes(), "age".getBytes())));
    System.out.println(Bytes.toString(result.getValue("f1".getBytes(), "name".getBytes())));
    myuser.close();

}

3.通过startRowKey和endRowKey进行扫描
/**
 * 通过startRowKey和endRowKey进行扫描查询
 */
@Test
public  void scanRowKey() throws IOException {
    //获取连接
    Configuration configuration = HBaseConfiguration.create();
    configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
    Connection connection = ConnectionFactory.createConnection(configuration);
    Table myuser = connection.getTable(TableName.valueOf("myuser"));
    Scan scan = new Scan();
    scan.setStartRow("0004".getBytes());
    scan.setStopRow("0006".getBytes());
    ResultScanner resultScanner = myuser.getScanner(scan);
    for (Result result : resultScanner) {
        //获取rowkey
        System.out.println(Bytes.toString(result.getRow()));
        //遍历获取得到所有的列族以及所有的列的名称
        KeyValue[] raw = result.raw();
        for (KeyValue keyValue : raw) {
            //获取所属列族
            System.out.println(Bytes.toString(keyValue.getFamilyArray(),keyValue.getFamilyOffset(),keyValue.getFamilyLength()));
            System.out.println(Bytes.toString(keyValue.getQualifierArray(),keyValue.getQualifierOffset(),keyValue.getQualifierLength()));
        }
        //指定列族以及列打印列当中的数据出来
        System.out.println(Bytes.toInt(result.getValue("f1".getBytes(), "id".getBytes())));
        System.out.println(Bytes.toInt(result.getValue("f1".getBytes(), "age".getBytes())));
        System.out.println(Bytes.toString(result.getValue("f1".getBytes(), "name".getBytes())));
    }
    myuser.close();
}
4.通过scan进行全表扫描
/**
 * 全表扫描
 */
@Test
public void scanAllData() throws IOException {
    //获取连接
    Configuration configuration = HBaseConfiguration.create();
    configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
    Connection connection = ConnectionFactory.createConnection(configuration);
    Table myuser = connection.getTable(TableName.valueOf("myuser"));

    Scan scan = new Scan();
    ResultScanner resultScanner = myuser.getScanner(scan);
    for (Result result : resultScanner) {
        //获取rowkey
        System.out.println(Bytes.toString(result.getRow()));

        //指定列族以及列打印列当中的数据出来
        System.out.println(Bytes.toInt(result.getValue("f1".getBytes(), "id".getBytes())));
        System.out.println(Bytes.toInt(result.getValue("f1".getBytes(), "age".getBytes())));
        System.out.println(Bytes.toString(result.getValue("f1".getBytes(), "name".getBytes())));
    }
    myuser.close();
}

过滤器查询

过滤器的类型很多,但是可以分为两大类——比较过滤器,专用过滤器
过滤器的作用是在服务端判断数据是否满足条件,然后只将满足条件的数据返回给客户端;

hbase过滤器的比较运算符:

在这里插入图片描述

Hbase过滤器的专用过滤器(指定比较机制):

在这里插入图片描述

1、比较过滤器

1、rowKey过滤器RowFilter

通过RowFilter过滤比rowKey 0003小的所有值出来

/**
 * hbase行键过滤器RowFilter
 */
@Test
public  void rowKeyFilter() throws IOException {
    //获取连接
    Configuration configuration = HBaseConfiguration.create();
    configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
    Connection connection = ConnectionFactory.createConnection(configuration);
    Table myuser = connection.getTable(TableName.valueOf("myuser"));

    Scan scan = new Scan();
    RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.LESS_OR_EQUAL, new BinaryComparator(Bytes.toBytes("0003")));

    scan.setFilter(rowFilter);
    ResultScanner resultScanner = myuser.getScanner(scan);
    for (Result result : resultScanner) {
        //获取rowkey
        System.out.println(Bytes.toString(result.getRow()));

        //指定列族以及列打印列当中的数据出来
        System.out.println(Bytes.toInt(result.getValue("f1".getBytes(), "id".getBytes())));
        System.out.println(Bytes.toInt(result.getValue("f1".getBytes(), "age".getBytes())));
        System.out.println(Bytes.toString(result.getValue("f1".getBytes(), "name".getBytes())));
    }
    myuser.close();

}
2、列族过滤器FamilyFilter

查询比f2列族小的所有的列族内的数据

/**
 * hbase列族过滤器FamilyFilter
 */
@Test
public  void familyFilter() throws IOException {
    //获取连接
    Configuration configuration = HBaseConfiguration.create();
    configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
    Connection connection = ConnectionFactory.createConnection(configuration);
    Table myuser = connection.getTable(TableName.valueOf("myuser"));
    Scan scan = new Scan();
    FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.LESS, new SubstringComparator("f2"));
    scan.setFilter(familyFilter);
    ResultScanner resultScanner = myuser.getScanner(scan);
    for (Result result : resultScanner) {
        //获取rowkey
        System.out.println(Bytes.toString(result.getRow()));
        //指定列族以及列打印列当中的数据出来
        System.out.println(Bytes.toInt(result.getValue("f1".getBytes(), "id".getBytes())));
        System.out.println(Bytes.toInt(result.getValue("f1".getBytes(), "age".getBytes())));
        System.out.println(Bytes.toString(result.getValue("f1".getBytes(), "name".getBytes())));
    }
    myuser.close();
}
3、列过滤器QualifierFilter

只查询name列的值

/**
 * hbase列过滤器
 */
@Test
public  void qualifierFilter() throws IOException {
    //获取连接
    Configuration configuration = HBaseConfiguration.create();
    configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
    Connection connection = ConnectionFactory.createConnection(configuration);
    Table myuser = connection.getTable(TableName.valueOf("myuser"));
    Scan scan = new Scan();
    QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("name"));
    scan.setFilter(qualifierFilter);
    ResultScanner resultScanner = myuser.getScanner(scan);
    for (Result result : resultScanner) {
        //获取rowkey
        System.out.println(Bytes.toString(result.getRow()));
        //指定列族以及列打印列当中的数据出来
    //    System.out.println(Bytes.toInt(result.getValue("f1".getBytes(), "id".getBytes())));
        System.out.println(Bytes.toString(result.getValue("f1".getBytes(), "name".getBytes())));
    }
    myuser.close();
}
4、列值过滤器ValueFilter

查询所有列当中包含8的数据

/**
 * hbase值过滤器
 * 查询包含8的列值
 */
@Test
public  void valueFilter() throws IOException {
    //获取连接
    Configuration configuration = HBaseConfiguration.create();
    configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
    Connection connection = ConnectionFactory.createConnection(configuration);
    Table myuser = connection.getTable(TableName.valueOf("myuser"));
    Scan scan = new Scan();
    ValueFilter valueFilter = new ValueFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("8"));

    scan.setFilter(valueFilter);
    ResultScanner resultScanner = myuser.getScanner(scan);
    for (Result result : resultScanner) {
        //获取rowkey
        System.out.println(Bytes.toString(result.getRow()));
        //指定列族以及列打印列当中的数据出来
        //    System.out.println(Bytes.toInt(result.getValue("f1".getBytes(), "id".getBytes())));
        System.out.println(Bytes.toString(result.getValue("f2".getBytes(), "phone".getBytes())));
    }
    myuser.close();
}

2、专用过滤器

1、单列值过滤器 SingleColumnValueFilter

SingleColumnValueFilter会返回满足条件的整列值的所有字段

/**
 * 单列值过滤器,返回满足条件的整行数据
 */
@Test
public void singleColumnFilter() throws IOException {
    //获取连接
    Configuration configuration = HBaseConfiguration.create();
    configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
    Connection connection = ConnectionFactory.createConnection(configuration);
    Table myuser = connection.getTable(TableName.valueOf("myuser"));
    Scan scan = new Scan();
    SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter("f1".getBytes(), "name".getBytes(), CompareFilter.CompareOp.EQUAL, "刘备".getBytes());
    scan.setFilter(singleColumnValueFilter);
    ResultScanner resultScanner = myuser.getScanner(scan);
    for (Result result : resultScanner) {
        //获取rowkey
        System.out.println(Bytes.toString(result.getRow()));
        //指定列族以及列打印列当中的数据出来
        System.out.println(Bytes.toInt(result.getValue("f1".getBytes(), "id".getBytes())));
        System.out.println(Bytes.toString(result.getValue("f1".getBytes(), "name".getBytes())));
        System.out.println(Bytes.toString(result.getValue("f2".getBytes(), "phone".getBytes())));
    }
    myuser.close();
}
2、列值排除过滤器SingleColumnValueExcludeFilter

与SingleColumnValueFilter相反,会排除掉指定的列,其他的列全部返回

3、rowkey前缀过滤器PrefixFilter

查询以00开头的所有前缀的rowkey

/**
 * 行键前缀过滤器
 */
@Test
public void preFilter() throws IOException {

    //获取连接
    Configuration configuration = HBaseConfiguration.create();
    configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
    Connection connection = ConnectionFactory.createConnection(configuration);
    Table myuser = connection.getTable(TableName.valueOf("myuser"));
    Scan scan = new Scan();
    PrefixFilter prefixFilter = new PrefixFilter("00".getBytes());
    scan.setFilter(prefixFilter);
    ResultScanner resultScanner = myuser.getScanner(scan);
    for (Result result : resultScanner) {
        //获取rowkey
        System.out.println(Bytes.toString(result.getRow()));
        //指定列族以及列打印列当中的数据出来
        System.out.println(Bytes.toInt(result.getValue("f1".getBytes(), "id".getBytes())));
        System.out.println(Bytes.toString(result.getValue("f1".getBytes(), "name".getBytes())));
        System.out.println(Bytes.toString(result.getValue("f2".getBytes(), "phone".getBytes())));
    }
    myuser.close();

}
4、分页过滤器PageFilter

通过pageFilter实现分页过滤器

@Test
public void pageFilter2() throws IOException {
    //获取连接
    Configuration configuration = HBaseConfiguration.create();
    configuration.set("hbase.zookeeper.quorum", "node01:2181,node02:2181,node03:2181");
    Connection connection = ConnectionFactory.createConnection(configuration);
    Table myuser = connection.getTable(TableName.valueOf("myuser"));
    int pageNum = 3;
    int pageSize = 2;
    Scan scan = new Scan();
    if (pageNum == 1) {
        PageFilter filter = new PageFilter(pageSize);
        scan.setStartRow(Bytes.toBytes(""));
        scan.setFilter(filter);
        scan.setMaxResultSize(pageSize);
        ResultScanner scanner = myuser.getScanner(scan);
        for (Result result : scanner) {
            //获取rowkey
            System.out.println(Bytes.toString(result.getRow()));
            //指定列族以及列打印列当中的数据出来
          //System.out.println(Bytes.toInt(result.getValue("f1".getBytes(), "id".getBytes())));
            System.out.println(Bytes.toString(result.getValue("f1".getBytes(), "name".getBytes())));
            //System.out.println(Bytes.toString(result.getValue("f2".getBytes(), "phone".getBytes())));
        }

    }else{
        String startRowKey ="";
        PageFilter filter = new PageFilter((pageNum - 1) * pageSize + 1  );
        scan.setStartRow(startRowKey.getBytes());
        scan.setMaxResultSize((pageNum - 1) * pageSize + 1);
        scan.setFilter(filter);
        ResultScanner scanner = myuser.getScanner(scan);
        for (Result result : scanner) {
            byte[] row = result.getRow();
            startRowKey =  new String(row);
        }
        Scan scan2 = new Scan();
        scan2.setStartRow(startRowKey.getBytes());
        scan2.setMaxResultSize(Long.valueOf(pageSize));
        PageFilter filter2 = new PageFilter(pageSize);
        scan2.setFilter(filter2);

        ResultScanner scanner1 = myuser.getScanner(scan2);
        for (Result result : scanner1) {
            byte[] row = result.getRow();
            System.out.println(new String(row));
        }
    }
    myuser.close();
}
3、多过滤器综合查询FilterList

需求:使用SingleColumnValueFilter查询f1列族,name为刘备的数据,并且同时满足rowkey的前缀以00开头的数据(PrefixFilter)

/**
 * 多过滤器组合使用
 */
@Test
public void manyFilter() throws IOException {
    //获取连接
    Configuration configuration = HBaseConfiguration.create();
    configuration.set("hbase.zookeeper.quorum", "node01:2181,node02:2181,node03:2181");
    Connection connection = ConnectionFactory.createConnection(configuration);
    Table myuser = connection.getTable(TableName.valueOf("myuser"));
    Scan scan = new Scan();
    FilterList filterList = new FilterList();

    SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter("f1".getBytes(), "name".getBytes(), CompareFilter.CompareOp.EQUAL, "刘备".getBytes());
    PrefixFilter prefixFilter = new PrefixFilter("00".getBytes());
    filterList.addFilter(singleColumnValueFilter);
    filterList.addFilter(prefixFilter);
    scan.setFilter(filterList);
    ResultScanner scanner = myuser.getScanner(scan);
    for (Result result : scanner) {
        //获取rowkey
        System.out.println(Bytes.toString(result.getRow()));
        //指定列族以及列打印列当中的数据出来
     // System.out.println(Bytes.toInt(result.getValue("f1".getBytes(), "id".getBytes())));
        System.out.println(Bytes.toString(result.getValue("f1".getBytes(), "name".getBytes())));
        //System.out.println(Bytes.toString(result.getValue("f2".getBytes(), "phone".getBytes())));
    }
    myuser.close();

}
5、根据rowkey删除数据
/**
 * 删除数据
 */
@Test
public  void  deleteByRowKey() throws IOException {
    //获取连接
    Configuration configuration = HBaseConfiguration.create();
    configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
    Connection connection = ConnectionFactory.createConnection(configuration);
    Table myuser = connection.getTable(TableName.valueOf("myuser"));
    Delete delete = new Delete("0001".getBytes());
    myuser.delete(delete);
    myuser.close();
}

6、删除表操作

@Test
public void  deleteTable() throws IOException {
    //获取连接
    Configuration configuration = HBaseConfiguration.create();
    configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
    Connection connection = ConnectionFactory.createConnection(configuration);
    Admin admin = connection.getAdmin();
    admin.disableTable(TableName.valueOf("myuser"));
    admin.deleteTable(TableName.valueOf("myuser"));
    admin.close();
}
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱纹身的big数据

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值