HBase简单通讯代码

首先,就要说一下配置问题了。HBase客户端的配置有两种方式,一种是通过配置文件,另一种是通过代码设置。

1、配置文件方式
配置文件名称为hbase-site.xml,该文件必须放置到CLASS_PATH下面才会有效,文件示例如下:
hbase-site.xml

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
	<property>
		<name>hbase.rootdir</name>
		<value>hdfs://hadoop-master:9000/hbase</value>
	</property>
	<property>
		<name>hbase.cluster.distributed</name>
		<value>true</value>
	</property>
	<property>
		<name>hbase.master</name>
		<value>hdfs://hadoop-master:60000</value>
	</property>
	<property>
		<name>hbase.zookeeper.quorum</name>
		<value>hadoop-master,hadoop-slave01,hadoop-slave02</value>
	</property>
</configuration>

2、通过代码配置方式

        Configuration hbaseConfig = HBaseConfiguration.create();
        hbaseConfig.setInt("timeout", 120000);
        hbaseConfig.set("hbase.master", "hdfs://hadoop-master:60000");
        hbaseConfig.set("hbase.zookeeper.quorum", "hadoop-master,hadoop-slave01,hadoop-slave02");
        hbaseConfig.setInt("hbase.zookeeper.property.clientPort", 2181);
        hbaseConfig.setInt("hbase.client.retries.number", 1);

3、然后是java代码,都是简单操作,就不多说了哦
TestHBase.java

package com.neohope.hbase.test;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

public class TestHBase {

    /*
    * 获取Connection
    */
    protected static Configuration hbaseConfig;
    static {
        hbaseConfig = HBaseConfiguration.create();
        hbaseConfig.setInt("timeout", 120000);
        hbaseConfig.set("hbase.master", "hdfs://hadoop-master:60000");
        hbaseConfig.set("hbase.zookeeper.quorum", "hadoop-master,hadoop-slave01,hadoop-slave02");
        hbaseConfig.setInt("hbase.zookeeper.property.clientPort", 2181);
        hbaseConfig.setInt("hbase.client.retries.number", 1);
    }

    /*
    * 获取Connection
    */
    protected static Connection GetConnection() throws IOException {
        return ConnectionFactory.createConnection(hbaseConfig);
    }

    /*
    * 获取Admin
    */
    protected static Admin GetAdmin() throws IOException {
        return GetConnection().getAdmin();
    }

    /*
    * 创建表
    *
    * @tableName 表名
    *
    * @family 列族名
    */
    public static void CreateTable(String tableName, String[] family)
            throws Exception {
        Admin admin = GetAdmin();
        HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName));
        for (String aFamily : family) {
            desc.addFamily(new HColumnDescriptor(aFamily));
        }

        if (admin.tableExists(TableName.valueOf(tableName))) {
            System.out.println(tableName + " exists!");
        } else {
            admin.createTable(desc);
            System.out.println(tableName + " is created!");
        }
    }

    /*
     * 删除表
     *
     * @tableName 表名
     */
    public static void DeleteTable(String tableName) throws IOException {
        Admin admin = GetAdmin();
        admin.disableTable( TableName.valueOf(tableName));
        admin.deleteTable( TableName.valueOf(tableName));
        System.out.println(tableName + " is deleted!");
    }

    /*
    * 新增或更新多列数据
    *
    * @rowKey rowKey
    *
    * @tableName 表名
    *
    *
    */
    public static void PutData( String tableName, String rowKey, String[] columnFamilies, String[] columns, String[] values )
            throws IOException {
        Table table = GetConnection().getTable(TableName.valueOf(tableName));
        Put put = new Put(Bytes.toBytes(rowKey));

        for (int i=0;i< columnFamilies.length; i++) {
            put.addColumn(Bytes.toBytes(columnFamilies[i]), Bytes.toBytes(columns[i]), Bytes.toBytes(values[i]));
        }
        table.put(put);
        System.out.println("put data is succeed!");
    }

    /*
     * 新增或更新一列数据
     *
     * @tableName 表名
     *
     * @rowKey rowKey
     *
     * @familyName 列族名
     *
     * @columnName 列名
     *
     * @value 更新后的值
     */
    public static void PutData(String tableName, String rowKey,
                                   String familyName, String columnName, String value)
            throws IOException {
        Table table = GetConnection().getTable( TableName.valueOf(tableName));
        Put put = new Put(Bytes.toBytes(rowKey));
        put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName),
                Bytes.toBytes(value));
        table.put(put);
        System.out.println("put data is succeed!");
    }

    /*
     * 根据rowKey查询
     *
     * @rowKey rowKey
     *
     * @tableName 表名
     */
    public static Result GetData(String tableName, String rowKey)
            throws IOException {
        Get get = new Get(Bytes.toBytes(rowKey));
        Table table = GetConnection().getTable( TableName.valueOf(tableName));
        Result result = table.get(get);
        if(result.size()>0) {
            for (Cell cell : result.listCells()) {
                System.out.println("family:" + Bytes.toString(CellUtil.cloneFamily(cell)));
                System.out.println("qualifier:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
                System.out.println("value:" + Bytes.toString(CellUtil.cloneValue(cell)));
                System.out.println("Timestamp:" + cell.getTimestamp());
                System.out.println("-------------------------------------------");
            }
        }
        return result;
    }

    /*
     * 查询表中的某一列
     *
     * @tableName 表名
     *
     * @rowKey rowKey
     */
    public static void GetDataByColumn(String tableName, String rowKey,
                                         String familyName, String columnName) throws IOException {
        Table table = GetConnection().getTable( TableName.valueOf(tableName));
        Get get = new Get(Bytes.toBytes(rowKey));
        get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName));
        Result result = table.get(get);
        if(result.size()>0) {
            for (Cell cell : result.listCells()) {
                System.out.println("family:" + Bytes.toString(CellUtil.cloneFamily(cell)));
                System.out
                        .println("qualifier:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
                System.out.println("value:" + Bytes.toString(CellUtil.cloneValue(cell)));
                System.out.println("Timestamp:" + cell.getTimestamp());
                System.out.println("-------------------------------------------");
            }
        }
    }

    /*
     * 查询某列数据的多个版本
     *
     * @tableName 表名
     *
     * @rowKey rowKey
     *
     * @familyName 列族名
     *
     * @columnName 列名
     */
    public static void GetDataByVersion(String tableName, String rowKey,
                                          String familyName, String columnName, int maxVersions) throws IOException {
        Table table = GetConnection().getTable( TableName.valueOf(tableName));
        Get get = new Get(Bytes.toBytes(rowKey));
        get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName));
        get.setMaxVersions(maxVersions);
        Result result = table.get(get);
        for (Cell cell : result.listCells()) {
            System.out.println("family:" + Bytes.toString(CellUtil.cloneFamily(cell)));
            System.out.println("qualifier:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
            System.out.println("value:" + Bytes.toString(CellUtil.cloneValue(cell)));
            System.out.println("Timestamp:" + cell.getTimestamp());
            System.out.println("-------------------------------------------");
        }
    }

    /*
     * 遍历表
     *
     * @tableName 表名
     */
    public static void ScanData(String tableName) throws IOException {
        Scan scan = new Scan();
        ResultScanner rs = null;
        Table table = GetConnection().getTable( TableName.valueOf(tableName));
        try {
            rs = table.getScanner(scan);
            for (Result r : rs) {
                for (Cell cell : r.listCells()) {
                    System.out.println("row:" + Bytes.toString(CellUtil.cloneRow(cell)));
                    System.out.println("family:"
                            + Bytes.toString(CellUtil.cloneFamily(cell)));
                    System.out.println("qualifier:"
                            + Bytes.toString(CellUtil.cloneQualifier(cell)));
                    System.out
                            .println("value:" + Bytes.toString(CellUtil.cloneValue(cell)));
                    System.out.println("timestamp:" + cell.getTimestamp());
                    System.out.println("-------------------------------------------");
                }
                System.out.println("===========================================");
            }
        } finally {
            rs.close();
        }
    }

    /*
     * 遍历查询hbase表
     *
     * @tableName 表名
     *
     * @startRowKey 起始RowKey
     *
     * @stopRowKey 结束RowKey
     */
    public static void ScanDataByRange(String tableName, String startRowKey,
                                      String stopRowKey) throws IOException {
        Scan scan = new Scan();
        scan.setStartRow(Bytes.toBytes(startRowKey));
        scan.setStopRow(Bytes.toBytes(stopRowKey));
        ResultScanner rs = null;
        Table table = GetConnection().getTable( TableName.valueOf(tableName));
        try {
            rs = table.getScanner(scan);
            for (Result r : rs) {
                for (Cell cell : r.listCells()) {
                    System.out.println("row:" + Bytes.toString(CellUtil.cloneRow(cell)));
                    System.out.println("family:"
                            + Bytes.toString(CellUtil.cloneFamily(cell)));
                    System.out.println("qualifier:"
                            + Bytes.toString(CellUtil.cloneQualifier(cell)));
                    System.out
                            .println("value:" + Bytes.toString(CellUtil.cloneValue(cell)));
                    System.out.println("timestamp:" + cell.getTimestamp());
                    System.out
                            .println("-------------------------------------------");
                }
            }
        } finally {
            rs.close();
        }
    }

    /*
     * 删除列
     *
     * @tableName 表名
     *
     * @rowKey rowKey
     *
     * @familyName 列族名
     *
     * @columnName 列名
     */
    public static void DeleteColumn(String tableName, String rowKey,
                                    String familyName, String columnName) throws IOException {
        Table table = GetConnection().getTable( TableName.valueOf(tableName));
        Delete deleteColumn = new Delete(Bytes.toBytes(rowKey));
        deleteColumn.addColumns(Bytes.toBytes(familyName),
                Bytes.toBytes(columnName));
        table.delete(deleteColumn);
        System.out.println(familyName + ":" + columnName + "is deleted!");
    }

    /*
     * 删除行
     *
     * @tableName 表名
     *
     * @rowKey rowKey
     */
    public static void DeleteRow(String tableName, String rowKey)
            throws IOException {
        Table table = GetConnection().getTable( TableName.valueOf(tableName));
        Delete deleteAll = new Delete(Bytes.toBytes(rowKey));
        table.delete(deleteAll);
        System.out.println("row is deleted!");
    }

    public static void main(String[] args) throws Exception {
        // 设置hadoop native的位置
        System.setProperty("hadoop.home.dir","D:\\MyProjects\\HadoopNative\\VS2013\\x86");
        //System.setProperty("HBASE_CONF_DIR","D:\\MyProjects\\IDEA14\\TestHBase\\etc\\hbase");

        // 创建表
        String tableName = "score";
        String[] family = { "student", "class", "teacher" };
        //CreateTable(tableName, family);

        // 增加数据
        String rowkey01 = "c001_s001";
        String[] columnFamilies01 = { "student", "student", "student","class", "class", "class","class","teacher", "teacher" };
        String[] columns01 = { "sid", "sname", "ssex","cid","cname", "croom", "cscore","tid", "tname" };
        String[] values01 = { "s001", "zhangsan", "male","c001","guoxue", "A3087", "90","t001", "Dr. K"};
        //PutData(tableName,rowkey01,columnFamilies01,columns01, values01);

        String rowkey02 = "c001_s002";
        String[] columnFamilies02 = { "student", "student", "student","class", "class", "class","class","teacher", "teacher" };
        String[] columns02 = { "sid", "sname", "ssex","cid","cname", "croom", "cscore","tid", "tname" };
        String[] values02 = { "s002", "lisi", "male","c001","guoxue", "A3087", "80","t001", "Dr. K"};
        //PutData(tableName,rowkey02,columnFamilies02,columns02, values02);

        String rowkey03 = "c001_s003";
        String[] columnFamilies03 = { "student", "student", "student","class", "class", "class","class","teacher", "teacher" };
        String[] columns03 = { "sid", "sname", "ssex","cid","cname", "croom", "cscore","tid", "tname" };
        String[] values03 = { "s003", "wangwu", "male","c001","guoxue", "A3087", "75","t001", "Dr. K"};
        //PutData(tableName,rowkey03,columnFamilies03,columns03, values03);

        String rowkey04 = "c002_s001";
        String[] columnFamilies04 = { "student", "student", "student","class", "class", "class","class","teacher", "teacher" };
        String[] columns04 = { "sid", "sname", "ssex","cid","cname", "croom", "cscore","tid", "tname" };
        String[] values04 = { "s001", "zhangsan", "male","c002","math", "B508", "90","t002", "Dr. S"};
        //PutData(tableName,rowkey04,columnFamilies04,columns04, values04);

        //查询数据
        //GetData(tableName,rowkey04);
        //GetDataByColumn(tableName,rowkey04,"student","sname");

        //新增、更新列
        //GetDataByColumn(tableName,rowkey01,"teacher","tname");
        //PutData(tableName,rowkey01,"teacher","tname", "Dr. K");
        //GetDataByColumn(tableName,rowkey01,"teacher","tname");

        //查询多个版本的数据
        PutData(tableName,rowkey01,"teacher","tname", "Dr. A");
        PutData(tableName,rowkey01,"teacher","tname", "Dr. B");
        PutData(tableName, rowkey01,"teacher","tname", "Dr. K");
        GetDataByVersion(tableName,rowkey01,"teacher","tname",3);

        //全表扫描数据
        //ScanData(tableName);

        //按范围扫描数据
        //ScanDataByRange(tableName,rowkey01,rowkey03);

        //删除列
        //GetDataByColumn(tableName,rowkey01,"teacher","cscore");
        //DeleteColumn(tableName, rowkey01, "teacher", "cscore");
        //GetDataByColumn(tableName,rowkey01,"teacher","cscore");

        //删除行
        //GetData(tableName,rowkey02);
        //DeleteRow(tableName,rowkey02);
        //GetData(tableName,rowkey02);

        // 删除表
        //DeleteTable(tableName);

    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

*