前提是hbase已经可以正常使用了。
maven依赖:
<dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>1.2.0</version> </dependency> <dependency> <groupId>jdk.tools</groupId> <artifactId>jdk.tools</artifactId> <version>1.6</version> <scope>system</scope> <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath> </dependency>
main class
package hbase; import java.io.File; import java.io.IOException; import java.util.List; 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.TableName; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.Delete; import org.apache.hadoop.hbase.client.Get; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.Table; import org.apache.hadoop.hbase.util.Bytes; public class HBaseClientTest extends ConnectionFactory { public static void main(String[] args) throws IOException { /* * 这个并没有什么用处,只是可以避免日志里出现一个错误(winutil.exe找不到)。而那个错误并不影响正常的hbase操作。 */ String hadoopHome=System.getenv("HADOOP_HOME"); String devDefault=new File("client/").getAbsolutePath(); System.setProperty("hadoop.home.dir", hadoopHome==null?devDefault:hadoopHome); long a=System.currentTimeMillis(); /* *hbase-site.xml放置到classpath里。 */ Configuration config = HBaseConfiguration.create(); System.out.println("get config ed:"+(System.currentTimeMillis()-a)/1000.0); a=System.currentTimeMillis(); // config.set("hbase.zookeeper.quorum","host10.ticloud"); // config.set("hbase.zookeeper.property.clientPort", "2222"); // config.addResource(new Path("D:\\hadoop\\hadoop_cfg\\", "hbase-site.xml")); // config.addResource(new Path("D:\\hadoop\\hbase_cfg\\", "core-site.xml")); Connection connection = ConnectionFactory.createConnection(config); System.out.println("get cnn ed:"+(System.currentTimeMillis()-a)/1000.0); a=System.currentTimeMillis(); Table table=null; try { table = connection.getTable(TableName.valueOf("t2")); System.out.println("get table ed:"+(System.currentTimeMillis()-a)/1000.0); a=System.currentTimeMillis(); // Use the table as needed, for a single operation and a single thread Put put=new Put(Bytes.toBytes("rowjava")); put.addColumn(Bytes.toBytes("c2"),Bytes.toBytes("col1"),Bytes.toBytes("value3_6")); // put.addColumn(Bytes.toBytes("c2"),Bytes.toBytes("col2"),Bytes.toBytes("value3_2")); // put.addColumn(Bytes.toBytes("c2"),Bytes.toBytes("col3"),Bytes.toBytes("value3_3")); // put.addColumn(Bytes.toBytes("c2"),Bytes.toBytes("col4"),Bytes.toBytes("value3_4")); System.out.println("get add ed:"+(System.currentTimeMillis()-a)/1000.0); a=System.currentTimeMillis(); table.put(put); System.out.println("get put ed:"+(System.currentTimeMillis()-a)/1000.0); a=System.currentTimeMillis(); long v=table.incrementColumnValue(Bytes.toBytes("rowjava"),Bytes.toBytes("c2"),Bytes.toBytes("cnt1"),1);//put incr不可混用 System.out.println(v); System.out.println("get incr ed:"+(System.currentTimeMillis()-a)/1000.0); a=System.currentTimeMillis(); Get get=new Get(Bytes.toBytes("rowjava")); Result r=table.get(get); Cell c=r.getColumnLatestCell(Bytes.toBytes("c2"),Bytes.toBytes("cnt1")); byte[] bs=CellUtil.cloneValue(c); System.out.println(Bytes.toLong(bs)); c=r.getColumnLatestCell(Bytes.toBytes("c2"),Bytes.toBytes("col1")); bs=CellUtil.cloneValue(c); System.out.println(Bytes.toString(bs)); Put p=new Put(Bytes.toBytes("rowjava")); p.addColumn(Bytes.toBytes("c2"),Bytes.toBytes("colCheck"),Bytes.toBytes("1")); boolean suc=table.checkAndPut(Bytes.toBytes("rowjava"), Bytes.toBytes("c2"),Bytes.toBytes("colCheck"),null, p); System.out.println("checkPut:"+suc);//true ,因为之前colCheck是不存在的 suc=table.checkAndPut(Bytes.toBytes("rowjava"), Bytes.toBytes("c2"),Bytes.toBytes("colCheck"),null, p); System.out.println("checkPut:"+suc);//false ,因为之前colCheck是存在的 Put p2=new Put(Bytes.toBytes("rowjava")); p2.addColumn(Bytes.toBytes("c2"),Bytes.toBytes("colCheck"),Bytes.toBytes("2")); suc=table.checkAndPut(Bytes.toBytes("rowjava"), Bytes.toBytes("c2"),Bytes.toBytes("colCheck"),Bytes.toBytes("1"), p2); System.out.println("checkPut:"+suc);//true ,因为之前colCheck是1 Delete del=new Delete(Bytes.toBytes("rowjava")); del.addColumn(Bytes.toBytes("c2"),Bytes.toBytes("colCheck"));//删除最新版本,删除后colCheck值2变成了1. table.delete(del); p2=new Put(Bytes.toBytes("rowjava")); p2.addColumn(Bytes.toBytes("c2"),Bytes.toBytes("colCheck"),Bytes.toBytes("2")); suc=table.checkAndPut(Bytes.toBytes("rowjava"), Bytes.toBytes("c2"),Bytes.toBytes("colCheck"),Bytes.toBytes("1"), p2); System.out.println("checkPut:"+suc);//true ,因为之前colCheck 2被删了 p2=new Put(Bytes.toBytes("rowjava")); p2.addColumn(Bytes.toBytes("c2"),Bytes.toBytes("colCheck"),Bytes.toBytes("3")); suc=table.checkAndPut(Bytes.toBytes("rowjava"), Bytes.toBytes("c2"),Bytes.toBytes("colCheck"),Bytes.toBytes("1"), p2); System.out.println("checkPut:"+suc);//false ,因为之前colCheck又是2了 del=new Delete(Bytes.toBytes("rowjava")); //不进行add,就是删除整行,全部单元格的全部版本。 del.addColumns(Bytes.toBytes("c2"),Bytes.toBytes("colCheck"));//删除colCheck单元格。全部的版本都删除 table.delete(del); } finally { if(table!=null)table.close();//逻辑处理完毕即可关闭 connection.close();//程序关闭时关闭,此对象可多线程共享使用 } } }
附件是没什么用的winutil.exe,可以避免一个日志提示:
Could not locate executable null\bin\winutils.exe in the Hadoop binaries.
相关推荐
java代码使用thrift2操作hbase示例,thrift2连接hbase添加数据,单条查找,删除数据,根据扫描器查找,修改数据等测试实例
hbase的java客户端api操作示例代码
本代码是java链接并操作hbase的实例代码,原本供同事参考所用,引用的jar包并没有经过详细分析,可能有冗余。
该资源为java客户端连接hbase集群,在windows客户端配置hadoop环境所需要用到的工具类,有需自取
HBase基本操作 增删改查 java代码 要使用须导入对应的jar包
使用Java API连接虚拟机HBase并进行数据库操作,Java源代码
本代码是java链接hbase数据库并对hbase进行增删改查操作的实例代码,包括批量操作
经过3天测试,总结出可运行成功的C#For HBase示例代码 经过3天测试,总结出可运行成功的C#For HBase示例代码
Hbase 可视化客户端工具(非phoenix连接),支持Hbase1.x,配置方便,类似PL/SQL,是一个HBase数据库的客户端数据管理软件。
HBASE的java版本的客户端,运行代码需要设定环境变量且打包成jar文件运行
HBase Java API操作数据库示例代码-HBaseDemo.rar HBase Java API操作数据库示例代码-HBaseDemo.rar HBase Java API操作数据库示例代码-HBaseDemo.rar
Java SpringBoot 连接 Hbase Demo 创建表 插入数据 列族 列 查询:全表、数据过滤 删除数据 删除表 Hbase 集群搭建:https://blog.csdn.net/weixin_42176639/article/details/131796472
在集群中创建java项目调用api来操作hbase,主要涉及对hbase的创建表格,删除表格,插入数据,删除数据,查询一条数据,查询所有数据等操作。 具体流程如下: 1.创建项目 2.获取jar包到项目的lib目录下(这边试用的事...
scala语言编写的spark streamming消费kafka数据存入hbase示例代码。打包成jar包可以在spark2.4下运行,测试环境是CDH6.2,运行没有问题。
hbase客户端下载,可以直接连接插入、查看 hbase客户端下载,可以直接连接插入、查看 hbase客户端下载,可以直接连接插入、查看
1、有多个工具可选择使用
Hbase笔记 —— 利用JavaAPI的方式操作Hbase数据库(往hbase的表中批量插入数据)
hbase示例 twitbase
java操作Hbase之Hbase专用过滤器PageFilter的使用源代码,附带全部所需源代码,欢迎下载学习。
java操作Hbase之从Hbase中读取数据写入hdfs中源码,附带全部所需jar包,欢迎下载学习。