分享

在集群中Java 通过调用API操作HBase 0.98



问题导读:
Java如何调用API操作HBase?













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

具体流程如下:

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



  1. package com.wan.hbase;
  2. import java.io.IOException;
  3. import org.apache.Hadoop.conf.Configuration;
  4. import org.apache.hadoop.hbase.Cell;
  5. import org.apache.hadoop.hbase.CellUtil;
  6. import org.apache.hadoop.hbase.HBaseConfiguration;
  7. import org.apache.hadoop.hbase.HColumnDescriptor;
  8. import org.apache.hadoop.hbase.HTableDescriptor;
  9. import org.apache.hadoop.hbase.MasterNotRunningException;
  10. import org.apache.hadoop.hbase.TableName;
  11. import org.apache.hadoop.hbase.ZooKeeperConnectionException;
  12. import org.apache.hadoop.hbase.client.Delete;
  13. import org.apache.hadoop.hbase.client.Get;
  14. import org.apache.hadoop.hbase.client.HBaseAdmin;
  15. import org.apache.hadoop.hbase.client.HTable;
  16. import org.apache.hadoop.hbase.client.Put;
  17. import org.apache.hadoop.hbase.client.Result;
  18. import org.apache.hadoop.hbase.client.ResultScanner;
  19. import org.apache.hadoop.hbase.client.Scan;
  20. import org.apache.hadoop.hbase.util.Bytes;
  21. public class SimpleHBase {
  22. public static void main(String[] args) {
  23.   Configuration configuration=HBaseConfiguration.create();
  24.   String tableName="student";
  25.   createTable(configuration, tableName);
  26. //  addData(configuration, tableName);
  27. //  getData(configuration, tableName);
  28. //  getAllData(configuration, tableName);
  29. //  deleteDate(configuration, tableName);
  30. //  dropTable(configuration, tableName);
  31.   
  32. }
  33. /**
  34.   * create a new Table
  35.   * @param configuration Configuration
  36.   * @param tableName String,the new Table's name
  37.   * */
  38. public static void createTable(Configuration configuration,String tableName){
  39.   HBaseAdmin admin;
  40.   try {
  41.    admin = new HBaseAdmin(configuration);
  42.    if(admin.tableExists(tableName)){
  43.     admin.disableTable(tableName);
  44.     admin.deleteTable(tableName);
  45.     System.out.println(tableName+"is exist ,delete ......");
  46.    }
  47.    
  48.    
  49.    HTableDescriptor tableDescriptor=new HTableDescriptor(TableName.valueOf(tableName));
  50.    tableDescriptor.addFamily(new HColumnDescriptor("info"));
  51.    tableDescriptor.addFamily(new HColumnDescriptor("address"));
  52.    admin.createTable(tableDescriptor);
  53.    System.out.println("end create table");
  54.   } catch (MasterNotRunningException e) {
  55.    // TODO Auto-generated catch block
  56.    e.printStackTrace();
  57.   } catch (ZooKeeperConnectionException e) {
  58.    // TODO Auto-generated catch block
  59.    e.printStackTrace();
  60.   } catch (IOException e) {
  61.    // TODO Auto-generated catch block
  62.    e.printStackTrace();
  63.   }
  64.   
  65. }
  66. /**
  67.   * Delete the existing table
  68.   * @param configuration Configuration
  69.   * @param tableName String,Table's name
  70.   * */
  71. public static void dropTable(Configuration configuration,String tableName){
  72.   HBaseAdmin admin;
  73.   try {
  74.    admin = new HBaseAdmin(configuration);
  75.    if(admin.tableExists(tableName)){
  76.     admin.disableTable(tableName);
  77.     admin.deleteTable(tableName);
  78.     System.out.println(tableName+"delete success!");
  79.    }else{
  80.     System.out.println(tableName+"Table does not exist!");
  81.    }
  82.   } catch (MasterNotRunningException e) {
  83.    // TODO Auto-generated catch block
  84.    e.printStackTrace();
  85.   } catch (ZooKeeperConnectionException e) {
  86.    // TODO Auto-generated catch block
  87.    e.printStackTrace();
  88.   } catch (IOException e) {
  89.    // TODO Auto-generated catch block
  90.    e.printStackTrace();
  91.   }
  92. }
  93. /**
  94.   * insert a data
  95.   * @param configuration Configuration
  96.   * @param tableName String,Table's name
  97.   * */
  98. public static void addData(Configuration configuration,String tableName){
  99.   HBaseAdmin admin;
  100.   try {
  101.    admin = new HBaseAdmin(configuration);
  102.    if(admin.tableExists(tableName)){
  103.     HTable table=new HTable(configuration, tableName);
  104.     Put put=new Put(Bytes.toBytes("zhangsan"));
  105.     put.add(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("28"));
  106.     table.put(put);
  107.     System.out.println("add success!");
  108.    }else{
  109.     System.out.println(tableName+"Table does not exist!");
  110.    }
  111.   } catch (MasterNotRunningException e) {
  112.    // TODO Auto-generated catch block
  113.    e.printStackTrace();
  114.   } catch (ZooKeeperConnectionException e) {
  115.    // TODO Auto-generated catch block
  116.    e.printStackTrace();
  117.   } catch (IOException e) {
  118.    // TODO Auto-generated catch block
  119.    e.printStackTrace();
  120.   }
  121. }
  122. /**
  123.   * Delete a data
  124.   * @param configuration Configuration
  125.   * @param tableName String,Table's name
  126.   * */
  127. public static void deleteDate(Configuration configuration,String tableName){
  128.   HBaseAdmin admin;
  129.   try {
  130.    admin=new HBaseAdmin(configuration);
  131.    if(admin.tableExists(tableName)){
  132.     HTable table=new HTable(configuration, tableName);
  133.     Delete delete=new Delete(Bytes.toBytes("zhangsan"));
  134.     table.delete(delete);
  135.     System.out.println("delete success!");
  136.    }else{
  137.     System.out.println("Table does not exist!");
  138.    }
  139.   } catch (MasterNotRunningException e) {
  140.    // TODO Auto-generated catch block
  141.    e.printStackTrace();
  142.   } catch (ZooKeeperConnectionException e) {
  143.    // TODO Auto-generated catch block
  144.    e.printStackTrace();
  145.   } catch (IOException e) {
  146.    // TODO Auto-generated catch block
  147.    e.printStackTrace();
  148.   }
  149. }
  150. /**
  151.   * get a data
  152.   * @param configuration Configuration
  153.   * @param tableName String,Table's name
  154.   * */
  155. public static void getData(Configuration configuration,String tableName){
  156.   HTable table;
  157.   try {
  158.    table = new HTable(configuration, tableName);
  159.    Get get=new Get(Bytes.toBytes("zhangsan"));
  160.    Result result=table.get(get);
  161.    for(Cell cell:result.rawCells()){   
  162.     System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
  163.     System.out.println("Timetamp:"+cell.getTimestamp()+" ");
  164.     System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
  165.     System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
  166.     System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");
  167.    }
  168.   } catch (IOException e) {
  169.    // TODO Auto-generated catch block
  170.    e.printStackTrace();
  171.   }
  172. }
  173. /**
  174.   * insert all data
  175.   * @param configuration Configuration
  176.   * @param tableName String,Table's name
  177.   * */
  178. public static void getAllData(Configuration configuration,String tableName){
  179.   HTable table;
  180.   try {
  181.    table=new HTable(configuration, tableName);
  182.    Scan scan=new Scan();
  183.    ResultScanner results=table.getScanner(scan);
  184.    for(Result result:results){
  185.     for(Cell cell:result.rawCells()){   
  186.      System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
  187.      System.out.println("Timetamp:"+cell.getTimestamp()+" ");
  188.      System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
  189.      System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
  190.      System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");
  191.     }
  192.    }
  193.   } catch (IOException e) {
  194.    // TODO Auto-generated catch block
  195.    e.printStackTrace();
  196.   }
  197.    
  198. }
  199. }
复制代码





ant脚本

  1. <?xml version="1.0"?>  
  2. <project name="HBaseProject" default="run" basedir=".">  
  3. <!-- properies -->  
  4.     <property name="src.dir" value="src" />  
  5.     <property name="report.dir" value="report" />  
  6.     <property name="classes.dir" value="classes" />  
  7.     <property name="lib.dir" value="lib" />  
  8.     <property name="dist.dir" value="dist" />  
  9. <property name="doc.dir" value="doc"/>  
  10.     <!-- 定义classpath -->  
  11.     <path id="master-classpath">  
  12.      <!--这边指向的jar包就是hbase 0.98 lib目录下对应的jar包,当前项目是把这些jar包放在项目的lib目录下-->
  13.         <fileset file="${lib.dir}/*.jar" />  
  14.         <pathelement path="${classes.dir}"/>  
  15.     </path>  
  16. <path id="run.path">  
  17.   <path path="${classes.dir}"/>
  18.           <path refid="master-classpath" />
  19.       </path>
  20.     <!-- 初始化任务 -->  
  21.     <target name="init" depends="clean">
  22.      <mkdir dir="${classes.dir}"/>
  23.      <mkdir dir="${dist.dir}"/>
  24.     </target>  
  25.     <!-- 编译 -->  
  26.     <target name="compile" depends="init" description="compile the source files">  
  27.          
  28.         <javac srcdir="${src.dir}" destdir="${classes.dir}" target="1.7" includeantruntime="false">  
  29.             <classpath refid="master-classpath"/>  
  30.         </javac>  
  31.     </target>  
  32. <target name="run" depends="compile">
  33.         <java classname="com.wan.hbase.SimpleHBase" classpathref="run.path" fork="true" >
  34.         </java>
  35.     </target>
  36.     <!-- 打包成jar -->  
  37.     <target name="pack" depends="compile" description="make .jar file">  
  38.       <mkdir dir="${dist.dir}" />  
  39.         <jar destfile="${dist.dir}/hbaseproject.jar" basedir="${classes.dir}">  
  40.             <exclude name="**/*Test.*" />  
  41.             <exclude name="**/Test*.*" />  
  42.         </jar>  
  43.     </target>
  44. <target name="clean" description="clean the project">
  45.   <delete dir="${classes.dir}"></delete>
  46.   <delete dir="${dist.dir}"></delete>
  47. </target>
  48. </project>
复制代码


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

即可运行!

完整项目(包含hbase中lib目录下的jar包):
百度网盘免费下载地址:http://pan.baidu.com/s/1eQEG7jO




引用:http://www.linuxidc.com/Linux/2014-05/102028.htm

欢迎加入about云群90371779322273151432264021 ,云计算爱好者群,亦可关注about云腾讯认证空间||关注本站微信

没找到任何评论,期待你打破沉寂

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条