在集群中java 通过调用API操作HBase 0.98
来源:互联网 发布:类似snapchat特效软件 编辑:程序博客网 时间:2024/04/29 10:30
在集群中java通过调用API操作HBase0.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 blocke.printStackTrace();} catch (ZooKeeperConnectionException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.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 blocke.printStackTrace();} catch (ZooKeeperConnectionException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.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 blocke.printStackTrace();} catch (ZooKeeperConnectionException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.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 blocke.printStackTrace();} catch (ZooKeeperConnectionException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.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 blocke.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 blocke.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中的jar包):http://download.csdn.net/detail/long1657/7162827
完整项目(包含hbase中lib目录下的jar包):http://pan.baidu.com/s/1eQwxX9C
0 0
- 在集群中java 通过调用API操作HBase 0.98
- 在集群中Java 通过调用API操作HBase 0.98
- 集群中通过Java操作Hbase 0.98
- hbase编程:通过Java api操作hbase
- 通过Java Api对Hbase进行操作
- Java API 与Hbase集群交互常用操作总结
- Java API 与Hbase集群交互常用操作总结
- 通过API远程操作HBase
- hbase java api操作
- hbase java api操作
- hbase Java Api 操作
- hbase java api操作
- Hbase-Java API操作
- Java API 操作Hbase
- Java API操作hbase
- java api操作 hbase
- java api操作hbase
- java api操作hbase
- Android App添加广告
- android ListView以及自定义adapter测试
- [ACM] poj 2484 A Funny Game(对称博弈)
- NULL与唯一性约束
- POJ 3159 Candies
- 在集群中java 通过调用API操作HBase 0.98
- 【Linux 入门笔记】命令行BASH的基本操作
- 整数的分划问题
- (基于Java)算法之合并排序
- 三点顺序(nyoj 68)
- 全面解析回溯法:算法框架与问题求解
- 安卓中的Context
- zoj 3327 Friend Number 模拟题
- android jni中C++与java互相调用小结