说明:
.第一部分为代码
.第二部分为工程pom文件 [java] view plain copy
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; /**
* Created by xuemin on 15/9/28.
*/
public class HBaseTest { public static Configuration configuration;
public static Connection connection;
public static Admin admin; public static void main(String[] args) throws IOException {
createTable("t2",new String[]{"cf1","cf2"});
insterRow("t2", "rw1", "cf1", "q1", "val1");
getData("t2", "rw1", "cf1", "q1");
scanData("t2", "rw1", "rw2");
deleRow("t2","rw1","cf1","q1");
deleteTable("t2");
} //初始化链接
public static void init(){
configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum","10.10.3.181,10.10.3.182,10.10.3.183");
configuration.set("hbase.zookeeper.property.clientPort","");
configuration.set("zookeeper.znode.parent","/hbase"); try {
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
} //关闭连接
public static void close(){
try {
if(null != admin)
admin.close();
if(null != connection)
connection.close();
} catch (IOException e) {
e.printStackTrace();
} } //建表
public static void createTable(String tableNmae,String[] cols) throws IOException { init();
TableName tableName = TableName.valueOf(tableNmae); if(admin.tableExists(tableName)){
System.out.println("talbe is exists!");
}else {
HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
for(String col:cols){
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(col);
hTableDescriptor.addFamily(hColumnDescriptor);
}
admin.createTable(hTableDescriptor);
}
close();
} //删表
public static void deleteTable(String tableName) throws IOException {
init();
TableName tn = TableName.valueOf(tableName);
if (admin.tableExists(tn)) {
admin.disableTable(tn);
admin.deleteTable(tn);
}
close();
} //查看已有表
public static void listTables() throws IOException {
init();
HTableDescriptor hTableDescriptors[] = admin.listTables();
for(HTableDescriptor hTableDescriptor :hTableDescriptors){
System.out.println(hTableDescriptor.getNameAsString());
}
close();
} //插入数据
public static void insterRow(String tableName,String rowkey,String colFamily,String col,String val) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(Bytes.toBytes(rowkey));
put.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col), Bytes.toBytes(val));
table.put(put); //批量插入
/* List<Put> putList = new ArrayList<Put>();
puts.add(put);
table.put(putList);*/
table.close();
close();
} //删除数据
public static void deleRow(String tableName,String rowkey,String colFamily,String col) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(Bytes.toBytes(rowkey));
//删除指定列族
//delete.addFamily(Bytes.toBytes(colFamily));
//删除指定列
//delete.addColumn(Bytes.toBytes(colFamily),Bytes.toBytes(col));
table.delete(delete);
//批量删除
/* List<Delete> deleteList = new ArrayList<Delete>();
deleteList.add(delete);
table.delete(deleteList);*/
table.close();
close();
} //根据rowkey查找数据
public static void getData(String tableName,String rowkey,String colFamily,String col)throws IOException{
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowkey));
//获取指定列族数据
//get.addFamily(Bytes.toBytes(colFamily));
//获取指定列数据
//get.addColumn(Bytes.toBytes(colFamily),Bytes.toBytes(col));
Result result = table.get(get); showCell(result);
table.close();
close();
} //格式化输出
public static void showCell(Result result){
Cell[] cells = result.rawCells();
for(Cell cell:cells){
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 static void scanData(String tableName,String startRow,String stopRow)throws IOException{
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
//scan.setStartRow(Bytes.toBytes(startRow));
//scan.setStopRow(Bytes.toBytes(stopRow));
ResultScanner resultScanner = table.getScanner(scan);
for(Result result : resultScanner){
showCell(result);
}
table.close();
close();
}
}
[html] view plain copy
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.</modelVersion> <groupId>td</groupId>
<artifactId>hbase_test</artifactId>
<version>1.0</version> <properties>
<project.build.sourceEncoding>UTF-</project.build.sourceEncoding>
<!-- <hadoop.version>2.6.-cdh5.4.5</hadoop.version>-->
<hbase.version>1.1.</hbase.version>
</properties> <dependencies>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>${hbase.version}</version>
</dependency>
</dependencies> </project>

转处:http://blog.csdn.net/rkjava/article/details/48789683

java 操作hbase1.2的更多相关文章

  1. java操作hbase1.3.1的增删改查

    我的eclipse程序在windows7机器上,hbase在linux机器上 1,首先在C:\Windows\System32\drivers\etc下面的HOSTS文件,加上linux 集群 2.直 ...

  2. Java操作Sqlite数据库-jdbc连接

    Java操作Sqlite数据库步骤: 1. 导入Sqlite jdbc 本文使用sqlite-jdbc-3.7.2.jar,下载地址 http://pan.baidu.com/s/1kVHAGdD 2 ...

  3. 【MongoDB for Java】Java操作MongoDB

    上一篇文章: http://www.cnblogs.com/hoojo/archive/2011/06/01/2066426.html介绍到了在MongoDB的控制台完成MongoDB的数据操作,通过 ...

  4. Java操作Oracle

    public class DBCon { // 数据库驱动对象 public static final String DRIVER = "oracle.jdbc.driver.OracleD ...

  5. JAVA操作ORACLE数据库的存储过程

    一.任务提出 JAVA操作oracle11g存储过程实验需要完成以下几个实例: 1.调用没有返回参数的过程(插入记录.更新记录) 2.有返回参数的过程 3.返回列表的过程 4.返回带分页的列表的过程. ...

  6. JAVA操作MongoDB数据库

    1. 首先,下载MongoDB对Java支持的驱动包 驱动包下载地址:https://github.com/mongodb/mongo-java-driver/downloads 2.Java操作Mo ...

  7. Java操作Session与Cookie

    1,Java操作Session Java操作Session非常简单,步骤如下 1.1,在servlet中通过request获取session HttpSession session = request ...

  8. JAVA操作COOKIE

    JAVA操作COOKIE 1.设置Cookie Cookie cookie = new Cookie("key", "value"); cookie.setMa ...

  9. [转]MongoDB for Java】Java操作MongoDB

    原文地址: MongoDB for Java]Java操作MongoDB 开发环境: System:Windows IDE:eclipse.MyEclipse 8 Database:mongoDB 开 ...

随机推荐

  1. class DELPHICLASS TObject

    class DELPHICLASS TObject    1.自己猜想:delphi,是windows平台的快速应用程序开发工具Rapid Application Development 简称RAD. ...

  2. Hive如何添加第三方JAR

    以加入elsaticsearch-hadoop-2.1.2.jar为例,讲述在Hive中加入第三方jar的几种方式. 1,在hive shell中加入 [hadoop@hadoopcluster78  ...

  3. form表单中enctype="multipart/form-data"的传值问题

    form表单中enctype="multipart/form-data"的传值问题!! Form表单中enctype="multipart/form-data" ...

  4. C# 7.0 新特性:本地方法

    C# 7.0:本地方法 VS 2017 的 C# 7.0 中引入了本地方法,本地方法是一种语法糖,允许我们在方法内定义本地方法.更加类似于函数式语言,但是,本质上还是基于面向对象实现的. 1. 本地方 ...

  5. 在Java环境上运行redis

    首先你得有Java环境,不多说,参考http://jingyan.baidu.com/article/f96699bb8b38e0894e3c1bef.html 下载redis驱动包 链接:http: ...

  6. ASP.NET没有魔法——ASP.NET MVC 与数据库之MySQL

    之前介绍了My Blog如何使用ADO.NET来访问SQL Server获取数据.本章将介绍如何使用My SQL来完成数据管理. 在使用My SQL之前需确保开发环境中安装了My SQL数据库和Con ...

  7. 解决-Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HO问题

    原因:因为你的编译工具(eclipse/Myeclipse...)没有添加jdk.添加M2_HOME的环境变量. 解决: ①:window->Preferences->java->I ...

  8. 启用事务操作,解决批量插入或更新sqlite,mssql等数据库耗时问题

    private void button1_Click(object sender, EventArgs e) { //Sqlite使用事务批量操作 极大的提高速度 DateTime starttime ...

  9. 详解python命名空间和作用域

    1.典型案例 先从几个典型的案例来看下名称空间及作用域对python代码运行的影响,请看下面几个代码实例及其执行结果,是否符合你的预期. 代码1:块作用域 if True: i = 1 print i ...

  10. 敏捷开发(2)-Scrum

    什么是SCRUM Scrum的英文意思是橄榄球运动的一个专业术语,表示“争球”的动作:把一个开发流程的名字取名为Scrum,我想你一定能想象出你的开发团队在开发一个项目时,大家像打橄榄球一样迅速.富有 ...