阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

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

102次阅读
没有评论

共计 17394 个字符,预计需要花费 44 分钟才能阅读完成。

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

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

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

package com.wan.hbase;

import java.io.IOException;

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.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
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.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;
import org.apache.hadoop.hbase.util.Bytes;

 

public class SimpleHBase {

 public static void main(String[] args) {
  Configuration configuration=HBaseConfiguration.create();
  String tableName=”student”;
  createTable(configuration, tableName);
//  addData(configuration, tableName);
//  getData(configuration, tableName);
//  getAllData(configuration, tableName);
//  deleteDate(configuration, tableName);
//  dropTable(configuration, tableName);
 
 }
 
 /**
  * create a new Table
  * @param configuration Configuration
  * @param tableName String,the new Table’s name
  * */
 public static void createTable(Configuration configuration,String tableName){
  HBaseAdmin admin;
  try {
   admin = new HBaseAdmin(configuration);
   if(admin.tableExists(tableName)){
    admin.disableTable(tableName);
    admin.deleteTable(tableName);
    System.out.println(tableName+”is exist ,delete ……”);
   }
   
   
   HTableDescriptor tableDescriptor=new HTableDescriptor(TableName.valueOf(tableName));
   tableDescriptor.addFamily(new HColumnDescriptor(“info”));
   tableDescriptor.addFamily(new HColumnDescriptor(“address”));
   admin.createTable(tableDescriptor);
   System.out.println(“end create table”);
  } catch (MasterNotRunningException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (ZooKeeperConnectionException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 
 }
 
 /**
  * Delete the existing table
  * @param configuration Configuration
  * @param tableName String,Table’s name
  * */
 public static void dropTable(Configuration configuration,String tableName){
  HBaseAdmin admin;
  try {
   admin = new HBaseAdmin(configuration);
   if(admin.tableExists(tableName)){
    admin.disableTable(tableName);
    admin.deleteTable(tableName);
    System.out.println(tableName+”delete success!”);
   }else{
    System.out.println(tableName+”Table does not exist!”);
   }
  } catch (MasterNotRunningException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (ZooKeeperConnectionException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 /**
  * insert a data
  * @param configuration Configuration
  * @param tableName String,Table’s name
  * */
 public static void addData(Configuration configuration,String tableName){
  HBaseAdmin admin;
  try {
   admin = new HBaseAdmin(configuration);
   if(admin.tableExists(tableName)){
    HTable table=new HTable(configuration, tableName);
    Put put=new Put(Bytes.toBytes(“zhangsan”));
    put.add(Bytes.toBytes(“info”), Bytes.toBytes(“age”), Bytes.toBytes(“28”));
    table.put(put);
    System.out.println(“add success!”);
   }else{
    System.out.println(tableName+”Table does not exist!”);
   }
  } catch (MasterNotRunningException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (ZooKeeperConnectionException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 /**
  * Delete a data
  * @param configuration Configuration
  * @param tableName String,Table’s name
  * */
 public static void deleteDate(Configuration configuration,String tableName){
  HBaseAdmin admin;
  try {
   admin=new HBaseAdmin(configuration);
   if(admin.tableExists(tableName)){
    HTable table=new HTable(configuration, tableName);
    Delete delete=new Delete(Bytes.toBytes(“zhangsan”));
    table.delete(delete);
    System.out.println(“delete success!”);
   }else{
    System.out.println(“Table does not exist!”);
   }
  } catch (MasterNotRunningException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (ZooKeeperConnectionException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 

 /**
  * get a data
  * @param configuration Configuration
  * @param tableName String,Table’s name
  * */
 public static void getData(Configuration configuration,String tableName){
  HTable table;
  try {
   table = new HTable(configuration, tableName);
   Get get=new Get(Bytes.toBytes(“zhangsan”));
   Result result=table.get(get);
 
   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))+” “);
   }
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 /**
  * insert all data
  * @param configuration Configuration
  * @param tableName String,Table’s name
  * */
 public static void getAllData(Configuration configuration,String tableName){
  HTable table;
  try {
   table=new HTable(configuration, tableName);
   Scan scan=new Scan();
   ResultScanner results=table.getScanner(scan);
   for(Result result:results){
    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))+” “);
    }
   }
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
   
 }
}

ant 脚本

<?xml version=”1.0″?> 
<project name=”HBaseProject” default=”run” basedir=”.”> 
<!– properies –> 
    <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”/> 
    <!– 定义 classpath –> 
    <path id=”master-classpath”> 
     <!– 这边指向的 jar 包就是 hbase 0.98 lib 目录下对应的 jar 包,当前项目是把这些 jar 包放在项目的 lib 目录下 –>
        <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.7″ includeantruntime=”false”> 
            <classpath refid=”master-classpath”/> 
        </javac> 
    </target> 
 
 <target name=”run” depends=”compile”>
        <java classname=”com.wan.hbase.SimpleHBase” classpathref=”run.path” fork=”true” >
        </java>
    </target>
 
    <!– 打包成 jar –> 
    <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

即可运行!

完整项目(包含 hbase 中 lib 目录下的 jar 包):

百度网盘免费下载地址:http://pan.baidu.com/s/1eQEG7jO

—————————————— 分割线 ——————————————

FTP 地址:ftp://ftp1.linuxidc.com

用户名:ftp1.linuxidc.com

密码:www.linuxidc.com

在 2014 年 LinuxIDC.com\5 月 \ 在集群中 Java 通过调用 API 操作 HBase 0.98

下载方法见 http://www.linuxidc.com/Linux/2013-10/91140.htm

—————————————— 分割线 ——————————————

编辑推荐

Hadoop+HBase 搭建云存储总结 PDF http://www.linuxidc.com/Linux/2013-05/83844.htm

HBase 结点之间时间不一致造成 regionserver 启动失败 http://www.linuxidc.com/Linux/2013-06/86655.htm

Hadoop+ZooKeeper+HBase 集群配置 http://www.linuxidc.com/Linux/2013-06/86347.htm

Hadoop 集群安装 &HBase 实验环境搭建 http://www.linuxidc.com/Linux/2013-04/83560.htm

基于 Hadoop 集群的 HBase 集群的配置 http://www.linuxidc.com/Linux/2013-03/80815.htm‘

Hadoop 安装部署笔记之 -HBase 完全分布模式安装 http://www.linuxidc.com/Linux/2012-12/76947.htm

单机版搭建 HBase 环境图文教程详解 http://www.linuxidc.com/Linux/2012-10/72959.htm

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

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

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

package com.wan.hbase;

import java.io.IOException;

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.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
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.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;
import org.apache.hadoop.hbase.util.Bytes;

 

public class SimpleHBase {

 public static void main(String[] args) {
  Configuration configuration=HBaseConfiguration.create();
  String tableName=”student”;
  createTable(configuration, tableName);
//  addData(configuration, tableName);
//  getData(configuration, tableName);
//  getAllData(configuration, tableName);
//  deleteDate(configuration, tableName);
//  dropTable(configuration, tableName);
 
 }
 
 /**
  * create a new Table
  * @param configuration Configuration
  * @param tableName String,the new Table’s name
  * */
 public static void createTable(Configuration configuration,String tableName){
  HBaseAdmin admin;
  try {
   admin = new HBaseAdmin(configuration);
   if(admin.tableExists(tableName)){
    admin.disableTable(tableName);
    admin.deleteTable(tableName);
    System.out.println(tableName+”is exist ,delete ……”);
   }
   
   
   HTableDescriptor tableDescriptor=new HTableDescriptor(TableName.valueOf(tableName));
   tableDescriptor.addFamily(new HColumnDescriptor(“info”));
   tableDescriptor.addFamily(new HColumnDescriptor(“address”));
   admin.createTable(tableDescriptor);
   System.out.println(“end create table”);
  } catch (MasterNotRunningException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (ZooKeeperConnectionException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 
 }
 
 /**
  * Delete the existing table
  * @param configuration Configuration
  * @param tableName String,Table’s name
  * */
 public static void dropTable(Configuration configuration,String tableName){
  HBaseAdmin admin;
  try {
   admin = new HBaseAdmin(configuration);
   if(admin.tableExists(tableName)){
    admin.disableTable(tableName);
    admin.deleteTable(tableName);
    System.out.println(tableName+”delete success!”);
   }else{
    System.out.println(tableName+”Table does not exist!”);
   }
  } catch (MasterNotRunningException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (ZooKeeperConnectionException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 /**
  * insert a data
  * @param configuration Configuration
  * @param tableName String,Table’s name
  * */
 public static void addData(Configuration configuration,String tableName){
  HBaseAdmin admin;
  try {
   admin = new HBaseAdmin(configuration);
   if(admin.tableExists(tableName)){
    HTable table=new HTable(configuration, tableName);
    Put put=new Put(Bytes.toBytes(“zhangsan”));
    put.add(Bytes.toBytes(“info”), Bytes.toBytes(“age”), Bytes.toBytes(“28”));
    table.put(put);
    System.out.println(“add success!”);
   }else{
    System.out.println(tableName+”Table does not exist!”);
   }
  } catch (MasterNotRunningException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (ZooKeeperConnectionException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 /**
  * Delete a data
  * @param configuration Configuration
  * @param tableName String,Table’s name
  * */
 public static void deleteDate(Configuration configuration,String tableName){
  HBaseAdmin admin;
  try {
   admin=new HBaseAdmin(configuration);
   if(admin.tableExists(tableName)){
    HTable table=new HTable(configuration, tableName);
    Delete delete=new Delete(Bytes.toBytes(“zhangsan”));
    table.delete(delete);
    System.out.println(“delete success!”);
   }else{
    System.out.println(“Table does not exist!”);
   }
  } catch (MasterNotRunningException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (ZooKeeperConnectionException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 

 /**
  * get a data
  * @param configuration Configuration
  * @param tableName String,Table’s name
  * */
 public static void getData(Configuration configuration,String tableName){
  HTable table;
  try {
   table = new HTable(configuration, tableName);
   Get get=new Get(Bytes.toBytes(“zhangsan”));
   Result result=table.get(get);
 
   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))+” “);
   }
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 /**
  * insert all data
  * @param configuration Configuration
  * @param tableName String,Table’s name
  * */
 public static void getAllData(Configuration configuration,String tableName){
  HTable table;
  try {
   table=new HTable(configuration, tableName);
   Scan scan=new Scan();
   ResultScanner results=table.getScanner(scan);
   for(Result result:results){
    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))+” “);
    }
   }
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
   
 }
}

ant 脚本

<?xml version=”1.0″?> 
<project name=”HBaseProject” default=”run” basedir=”.”> 
<!– properies –> 
    <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”/> 
    <!– 定义 classpath –> 
    <path id=”master-classpath”> 
     <!– 这边指向的 jar 包就是 hbase 0.98 lib 目录下对应的 jar 包,当前项目是把这些 jar 包放在项目的 lib 目录下 –>
        <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.7″ includeantruntime=”false”> 
            <classpath refid=”master-classpath”/> 
        </javac> 
    </target> 
 
 <target name=”run” depends=”compile”>
        <java classname=”com.wan.hbase.SimpleHBase” classpathref=”run.path” fork=”true” >
        </java>
    </target>
 
    <!– 打包成 jar –> 
    <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

即可运行!

完整项目(包含 hbase 中 lib 目录下的 jar 包):

百度网盘免费下载地址:http://pan.baidu.com/s/1eQEG7jO

—————————————— 分割线 ——————————————

FTP 地址:ftp://ftp1.linuxidc.com

用户名:ftp1.linuxidc.com

密码:www.linuxidc.com

在 2014 年 LinuxIDC.com\5 月 \ 在集群中 Java 通过调用 API 操作 HBase 0.98

下载方法见 http://www.linuxidc.com/Linux/2013-10/91140.htm

—————————————— 分割线 ——————————————

编辑推荐

Hadoop+HBase 搭建云存储总结 PDF http://www.linuxidc.com/Linux/2013-05/83844.htm

HBase 结点之间时间不一致造成 regionserver 启动失败 http://www.linuxidc.com/Linux/2013-06/86655.htm

Hadoop+ZooKeeper+HBase 集群配置 http://www.linuxidc.com/Linux/2013-06/86347.htm

Hadoop 集群安装 &HBase 实验环境搭建 http://www.linuxidc.com/Linux/2013-04/83560.htm

基于 Hadoop 集群的 HBase 集群的配置 http://www.linuxidc.com/Linux/2013-03/80815.htm‘

Hadoop 安装部署笔记之 -HBase 完全分布模式安装 http://www.linuxidc.com/Linux/2012-12/76947.htm

单机版搭建 HBase 环境图文教程详解 http://www.linuxidc.com/Linux/2012-10/72959.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-20发表,共计17394字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中