linux 下通过过 hbase 的Java api 操作hbase

hbase版本:0.98.5

hadoop版本:1.2.1

使用自带的zk

本文的内容是在集群中创建java项目调用api来操作hbase,主要涉及对hbase的创建表格,删除表格,插入数据,删除数据,查询一条数据,查询所有数据等操作。

具体流程如下:
1.创建项目
2.获取jar包到项目的lib目录下(这边试用的事hbase 0.98 lib目录下的所有jar包)
3.编写java程序
4.编写ant脚本

package test2;
import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HConnection;
import org.apache.hadoop.hbase.client.HConnectionManager;
import org.apache.hadoop.hbase.client.HTable;
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;

public class TestHBase {
    
    private HBaseAdmin admin = null;
    private Configuration conf = null;
    
    public TestHBase() throws Exception
    {
        conf = HBaseConfiguration.create();
       conf.addResource(new Path("/home/work/hbase_dev/test2/lib/hbase-site.xml"));
        //conf.set("hbase.zookeeper.quorum", "10.57.90.19");
        //conf.set("hbase.zookeeper.property.clientPort", "2181");
        admin = new HBaseAdmin(conf);
    }
    
    public void createTable (String tableName , String[] columnFamily) throws Exception
    {
        if (admin.tableExists(tableName))
        {
            System.out.println(tableName + "已存在");
            return ;
            //System.exit(0);
        }
        
        HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
        
        for (String colunm : columnFamily)
        {
            tableDescriptor.addFamily(new HColumnDescriptor(colunm));
        }
        
        admin.createTable(tableDescriptor);
        System.out.println("Create table successfully..");
    }
    
    public boolean deleteTable (String tableName)
    {
        try {
            if(admin.tableExists(tableName))
            {
                admin.disableTable(tableName);
                admin.deleteTable(tableName);
                System.out.println("drop table " + tableName);
            }
            return true;
        } catch (Exception e) {
            System.out.println("删除" + tableName + "失败");
            return false;
        }
    }
    
    public List getAllTables()
    {
        List<String> tables = null;
        if (admin != null)
        {
            try{
                HTableDescriptor[] allTables = admin.listTables();
                if(allTables.length > 0)
                {
                    tables = new ArrayList<String>();
                }
                
                for (HTableDescriptor tableDesc : allTables)
                {
                    tables.add(tableDesc.getNameAsString());
                    System.out.println(tableDesc.getNameAsString());
                }
            }catch(Exception ex)
            {
                ex.printStackTrace();
            }
        }
        
        return tables;
    }
    
    public boolean addOneRecord (String tableName , String key , String family , String column 
            , byte[] dataIn) throws Exception
    {
        HConnection connection = HConnectionManager.createConnection(conf);
        //HTable table = new HTable(hbaseConf, tableName);
        HTable table = (HTable)connection.getTable(tableName);
        Put put = new Put(key.getBytes());
        put.add(family.getBytes(), column.getBytes(), dataIn);
        try {
            table.put(put);
            System.out.println("插入数据条 " + key + "成功");
            return true;
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("插入数据条 " + key + "失败");
            return false;
        }
    }
    
    public void getValueFromKey (String tableName , String key)
    {
        try{
            HConnection conn = HConnectionManager.createConnection(conf);
            HTable table = (HTable) conn.getTable(tableName);
            Get get = new Get(key.getBytes());
            Result rs = table.get(get);
            if (rs.rawCells().length == 0)
            {
                System.out.println("不存在关键字为" + key + "的行...");
            }
            else
            {
                for (Cell cell : rs.rawCells())
                {
                    System.out.println(new String(CellUtil.cloneFamily(cell)) + 
                            "  " + new String(CellUtil.cloneQualifier(cell)) + "  " + new String(CellUtil.cloneValue(cell)));
                }
            }
        }
        catch(Exception ex)
        {
            System.out.println("查询失败");
            ex.printStackTrace();
        }
    }
    
    
    
    public void getAllData(String tableName) throws Exception
    {
        HConnection conn = HConnectionManager.createConnection(conf);
        HTable table = (HTable) conn.getTable(tableName);
        Scan scan = new Scan();
        ResultScanner rs = table.getScanner(scan);
        for (Result result : rs)
        {
            for (Cell cell : result.rawCells())
            {
                System.out.println("RowName: " + new String(CellUtil.cloneRow(cell)) + " ");
                System.out.println("Timetamp: " + cell.getTimestamp() + " ");
                System.out.println("Column family: " + new String(CellUtil.cloneFamily(cell)) + " ");
                System.out.println("row name: " + new String(CellUtil.cloneQualifier(cell)) + " ");
                System.out.println("value: " + new String(CellUtil.cloneValue(cell)) + " ");
            }
        }
    }
    
    public void deleteRecord(String tableName , String key)
    {
        try
        {
            HConnection conn = HConnectionManager.createConnection(conf);
            HTable table = (HTable)conn.getTable(tableName);
            Delete delete = new Delete(key.getBytes());
            
            table.delete(delete);
            System.out.println("删除" + key +"成功...");
        }
        catch(Exception ex)
        {
            System.out.println("删除数据失败...");
            ex.printStackTrace();
        }
    }
    

    public static void main(String[] args) throws Exception{
        // TODO Auto-generated method stub
        String[] column = {"family1" , "family2"};
        try {
            TestHBase hbase = new TestHBase();
            hbase.deleteTable("students");
            hbase.createTable("students", column);
            //hbase.getAllData("scores");
            hbase.addOneRecord("students", "id1", "family1", "name", "Jack".getBytes());
            hbase.addOneRecord("students", "id1", "family1", "grade", "gaosan".getBytes());
            //hbase.getAllTables();
            //hbase.getAllData("students");
            hbase.getValueFromKey("students", "id1");
            hbase.deleteRecord("students", "id1");
            hbase.addOneRecord("students", "id2", "family1", "name", "Holen".getBytes());
            hbase.getValueFromKey("students", "id2");
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        
        
        
    }

}

ant脚本

<?xml version="1.0"?>  
<project name="HBaseProject" default="run" basedir=".">  
    <property name="src.dir" value="src" />  
    <property name="report.dir" value="report" />  
    <property name="classes.dir" value="classes" />  
    <property name="lib.dir" value="lib" />  
    <property name="dist.dir" value="dist" />  
    <property name="doc.dir" value="doc"/>  
    <path id="master-classpath">  
        <fileset file="${lib.dir}/*.jar" />  
        <pathelement path="${classes.dir}"/>  
    </path>  

    <path id="run.path">  
        <path path="${classes.dir}"/>
        <path refid="master-classpath" />
    </path> 
    <target name="init" depends="clean">
        <mkdir dir="${classes.dir}"/>
        <mkdir dir="${dist.dir}"/>
    </target>  
    <target name="compile" depends="init" description="compile the source files">  

        <javac srcdir="${src.dir}" destdir="${classes.dir}" target="1.6" includeantruntime="false">  
            <classpath refid="master-classpath"/>  
        </javac>  
    </target>  

    <target name="run" depends="compile">
        <java classname="test2.TestHBase" classpathref="run.path" fork="true" >
        </java>
    </target>

    <target name="pack" depends="compile" description="make .jar file">  
        <mkdir dir="${dist.dir}" />  
        <jar destfile="${dist.dir}/hbaseproject.jar" basedir="${classes.dir}">  
            <exclude name="**/*Test.*" />  
            <exclude name="**/Test*.*" />  
        </jar>  
    </target> 


    <target name="clean" description="clean the project">
        <delete dir="${classes.dir}"></delete>
        <delete dir="${dist.dir}"></delete>
    </target>
</project>

最后把项目放在集群中,进入项目的根目录,执行命令:ant run

即可运行!

原文地址:https://www.cnblogs.com/zlingh/p/3936284.html