栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

基于Java开发HBase篇

基于Java开发HBase篇

import java.io.File;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.fs.Path;

import org.apache.hadoop.hbase.Cell;

import org.apache.hadoop.hbase.CellUtil;

import org.apache.hadoop.hbase.HColumnDescriptor;

import org.apache.hadoop.hbase.HTableDescriptor;

import org.apache.hadoop.hbase.TableName;

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

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

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

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

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

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

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

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

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

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

import org.apache.hadoop.hbase.filter.BinaryComparator;

import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;

import org.apache.hadoop.hbase.filter.FamilyFilter;

import org.apache.hadoop.hbase.filter.QualifierFilter;

import org.apache.hadoop.hbase.filter.RegexStringComparator;

import org.apache.hadoop.hbase.filter.RowFilter;

import org.apache.hadoop.hbase.filter.SubstringComparator;

import org.apache.hadoop.hbase.filter.ValueFilter;

import org.apache.hadoop.hbase.util.Bytes;

import com.clearspring.analytics.stream.membership.Filter;

public class HbaseSample {

private Configuration conf = null;

private Connection conn = null;

private Admin hbAdmin = null;

public void init() throws IOException {

        String proDir = System.getProperty("user.dir");

        System.out.println(proDir);

        String baseDir = proDir + File.separator + "conf";

        // 获取core-site.xml文件

        String corePath = baseDir + File.separator + "core-site.xml";

        // 获取hdfs-site.xml文件

        String hdfsPath = baseDir + File.separator + "hdfs-site.xml";

        // 获取hbase-site.xml文件

        String hbasePath = baseDir + File.separator + "hbase-site.xml";

        conf = new Configuration();

        conf.addResource(new Path(corePath));

        conf.addResource(new Path(hdfsPath));

        conf.addResource(new Path(hbasePath));

        //                conf.set("fs.defaultFS", "hdfs://hadoop:9000");

        conf.set("hbase.zookeeper.quorum", "hadoop:2181");

        // 创建链接

        conn = ConnectionFactory.createConnection(conf);

        hbAdmin = conn.getAdmin();

}

// 表创建

public void create() throws IOException {

TableName tableName = TableName.valueOf("mytest"); //定义表明

        if (!hbAdmin.tableExists(tableName)){

                //定义表对象

                HTableDescriptor tabledesc = new HTableDescriptor(tableName);

                // 定义列族对象

                HColumnDescriptor hbColumnDescriptor1 = new                HColumnDescriptor(Bytes.toBytes("info1"));// 定义列族对象

                // hbase数据类型只有Bytes类型 通过bytes.tobytes 方法进行封装 返回result 需要通过Bytes.toString()转化为String对象

                HColumnDescriptor hbColumnDescriptor2 = new                 HColumnDescriptor(Bytes.toBytes("info2"));

                // 列族是放到表中的,把两个列族添加到表中

                tabledesc.addFamily(hbColumnDescriptor1); // 在表中添加列族1

                tabledesc.addFamily(hbColumnDescriptor2); // 在表中添加列族2

                // 创建表

                hbAdmin.createTable(tabledesc);

        }

}

// 禁用表:可以通过list和exists命令查看,但scan扫描不到它

public void disable() throws IOException {

        TableName tableName = TableName.valueOf("mytest");

        hbAdmin.disableTable(tableName);

}

// 启用表

public void enable() throws IOException {

        TableName tableName = TableName.valueOf("mytest");

        hbAdmin.enableTable(tableName);

}

// 删除表

public void drop() throws IOException{

        TableName tableName = TableName.valueOf("mytest");

        hbAdmin.enableTable(tableName);

}

// 增加单元格

public void put() throws IOException {

        TableName tableName = TableName.valueOf("mytest");

        Table table = conn.getTable(tableName);

        // 定义行键

        Put put = new Put(Bytes.toBytes("aa"));

        // 定义列族

        put.addColumn(Bytes.toBytes("info2"), Bytes.toBytes("like"), Bytes.toBytes("apple"));

        table.put(put);

}

// 删除单元格

public void testDelete() throws IOException {

        TableName tableName = TableName.valueOf("mytest");

        Table table = conn.getTable(tableName);

        Delete delete = new Delete(Bytes.toBytes("aa"));

        delete.addColumn(Bytes.toBytes("info1"), Bytes.toBytes("name"));

        table.delete(delete);

}

// 查询数据 get

public void testGet() throws IOException{

        TableName tableName = TableName.valueOf("mytest");

        Table table = conn.getTable(tableName);

        Get get = new Get(Bytes.toBytes("aa")); // 指定行键

        get.addFamily(Bytes.toBytes("info1")); // 指定列族

        Result result = table.get(get);

        for (Cell cell : result.rawCells()) {

                System.out.print("行键"+Bytes.toString(CellUtil.cloneRow(cell))+"t");

                System.out.print("列族"+Bytes.toString(CellUtil.cloneFamily(cell))+"t");

                System.out.print("列名"+Bytes.toString(CellUtil.cloneQualifier(cell))+"t");

                System.out.println("值"+Bytes.toString(CellUtil.clonevalue(cell))+"t");

        }

}

// scan扫描

public void testScan() throws IOException{

        TableName tableName = TableName.valueOf("mytest");

        Table table = conn.getTable(tableName);

        Scan scan = new Scan(); // 指定扫描region当中的对象

        ResultScanner scanner = table.getScanner(scan); // 返回resultScanner的对象

        this.showResult(scanner);

}

private void showResult(ResultScanner scanner){

        for(Result result:scanner){

                Cell[] cells = result.rawCells();

                for(Cell cell: cells){

                        System.out.print("行键"+Bytes.toString(CellUtil.cloneRow(cell))+"t");

                        System.out.print("列族"+Bytes.toString(CellUtil.cloneFamily(cell))+"t");

                        System.out.print("列名"+Bytes.toString(CellUtil.cloneQualifier(cell))+"t");

                        System.out.println("值"+Bytes.toString(CellUtil.clonevalue(cell))+"t");

                }

        }

}

// 过滤filter

// rowkey过滤器扫描

public void filterScan () throws IOException{

        TableName tableName = TableName.valueOf("mytest");

        Table table = conn.getTable(tableName);

        Scan scan = new Scan();

        // 正则表达式匹配

        RowFilter filter = new RowFilter(CompareOp.EQUAL, new         RegexStringComparator("^[a]+$"));

        scan.setFilter(filter);

        // 过滤之后扫描输出

        this.showResult(table.getScanner(scan));

}

// CF列族过滤器

public void familyFilterScan() throws IOException{

        TableName tableName = TableName.valueOf("mytest");

        Table table = conn.getTable(tableName);

        Scan scan = new Scan();

        FamilyFilter familyFilter = new FamilyFilter(CompareOp.EQUAL, new         BinaryComparator(Bytes.toBytes("info1")));

        scan.setFilter(familyFilter);

        this.showResult(table.getScanner(scan));

}

// 列标识过滤

public void qualifierScan() throws IOException{

        TableName tableName = TableName.valueOf("mytest");

        Table table = conn.getTable(tableName);

        Scan scan = new Scan();

        QualifierFilter qualifierFilter = new QualifierFilter(CompareOp.EQUAL, new         BinaryComparator(Bytes.toBytes("name")));

        scan.setFilter(qualifierFilter);

        this.showResult(table.getScanner(scan));

}

// 值过滤

public void valueScan() throws IOException {

        TableName tableName = TableName.valueOf("mytest");

        Table table = conn.getTable(tableName);

        Scan scan = new Scan();

        ValueFilter valueFilter = new ValueFilter(CompareOp.EQUAL, new         SubstringComparator("18"));

        scan.setFilter(valueFilter);

        this.showResult(table.getScanner(scan));

}

public static void main(String[] args) throws IOException {

       // TODO Auto-generated method stub

        HbaseSample hbbaseSample = new HbaseSample();

        // 初始化

        hbbaseSample.init();

        // 创建表

        //                hbbaseSample.create();

        // 禁用表

        //                hbbaseSample.disable();

        // 启用表

        //                hbbaseSample.enable();

        // 添加单元格

        //                hbbaseSample.put();

        // 删除单元格

        //                hbbaseSample.testDelete();

        // 按行查询

        //                hbbaseSample.testGet();

        // 扫描

        //                hbbaseSample.testScan();

        // rowkey过滤器扫描

        //                hbbaseSample.filterScan();

        // CF列族过滤器

        //                hbbaseSample.familyFilterScan();

        // 列标识符过滤器

        //                hbbaseSample.qualifierScan();

        // 值过滤

        //                hbbaseSample.valueScan();

        }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/423052.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号