一、使用IDEA的maven工程,工程结构如下:

二、maven的依赖pom.xml文件

<?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.0</modelVersion><groupId>com.hbasetest</groupId><artifactId>HbaseTest</artifactId><version>1.0-SNAPSHOT</version><dependencies><!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-client --><dependency><groupId>org.apache.hbase</groupId><artifactId>hbase-client</artifactId><version>1.3.0</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-server --><dependency><groupId>org.apache.hbase</groupId><artifactId>hbase-server</artifactId><version>1.3.0</version></dependency></dependencies></project>

三、hbase-site.xml,在HBase集群的{HBASE_HOME}/conf目录下下载到本地,放到resources资源目录下

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?><configuration><!-- 设置namenode所在位置 通过rootdir设置 也就是设置hdfs中存放的路径 --><property><name>hbase.rootdir</name><value>hdfs://hd09-1:9000/hbase</value></property><!-- 是否开启集群 --><property><name>hbase.cluster.distributed</name><value>true</value></property><!-- 0.98 后的新变动,之前版本没有.port,默认端口为 60000 --><property><name>hbase.master.port</name><value>16000</value></property><!-- zookeeper集群的位置 --><property><name>hbase.zookeeper.quorum</name><value>hd09-1:2181,hd09-2:2181,hd09-3:2181</value></property><!-- hbase的元数据信息存储在zookeeper的位置 --><property><name>hbase.zookeeper.property.dataDir</name><value>/root/hd/zookeeper-3.4.10/zkData</value></property>
</configuration>

四、core-site.xml,在Hadoop集群的{HADOOP_HOME}/etc/hadoop目录下下载到本地,放到resources资源目录下

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?><!-- Put site-specific property overrides in this file. --><configuration><property><name>fs.defaultFS</name><value>hdfs://hd09-1:9000</value></property>
</configuration>

五、hdfs-site.xml,在Hadoop集群的{HADOOP_HOME}/etc/hadoop目录下下载到本地,放到resources资源目录下

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?><!-- Put site-specific property overrides in this file. --><configuration><property><name>dfs.namenode.name.dir</name><value>/root/hd/dfs/name</value></property><property><name>dfs.datanode.data.dir</name><value>/root/hd/dfs/data</value></property><property><name>dfs.namenode.secondary.http-address</name><value>hd09-2:50090</value></property>
</configuration>

六、修改本地 C:\Windows\System32\drivers\etc\hosts 文件,在文件最下面加上

192.168.146.132 hd09-1
192.168.146.133 hd09-2
192.168.146.134 hd09-3

七、HbaseAPI 类

package com.demo.hbase;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;
import java.util.ArrayList;
import java.util.List;public class HbaseAPI {//配置信息public static Configuration conf;//获取配置信息static {//alt + enterconf = HBaseConfiguration.create();}//1.判断一张表是否存在public static boolean isExist(String tableName) throws IOException {//对表操作需要使用HBaseAdminConnection connection = ConnectionFactory.createConnection(conf);//管理表HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();return admin.tableExists(TableName.valueOf(tableName));}//2.在HBase集群创建表  create 'user','info','info1'public static void createTable(String tableName,String... columnFamily) throws IOException {//对表操作需要用HBaseAdminConnection connection = ConnectionFactory.createConnection(conf);//管理表HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();//1.表如果存在  请输入其他表名if (isExist(tableName)){System.out.println("表已经存在,请输入其它表名");}else{//2.注意,创建表的话 需要创建一个描述器HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName));//3.创建列族for (String cf : columnFamily) {htd.addFamily(new HColumnDescriptor(cf));}//4.创建表
            admin.createTable(htd);System.out.println("表已创建成功!");}}//3.删除HBase中的表public static void deleteTable(String tableName) throws IOException{//对表操作需要使用HBaseAdminConnection connection = ConnectionFactory.createConnection(conf);//管理表HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();//1.如果表存在 删除 否则打印不存在//需要先指定表不可用 再删除if (isExist(tableName)){//2.指定不可用
            admin.disableTable(TableName.valueOf(tableName));admin.deleteTable(TableName.valueOf(tableName));}else {System.out.println("表不存在,请重新输入表名!");}}//4.添加数据put 'user','rowKey'public static void addRow(String tableName, String rowkey, String cf, String column, String value) throws IOException {//对表操作需要使用HBaseAdminConnection connection = ConnectionFactory.createConnection(conf);//拿到表对象Table t = connection.getTable(TableName.valueOf(tableName));HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();//1.用put方式加入数据Put p = new Put(Bytes.toBytes(rowkey));//2.加入数据
        p.addColumn(Bytes.toBytes(cf),Bytes.toBytes(column),Bytes.toBytes(value));t.put(p);}//5.删除表中一行数据public static void deleteRow(String tableName, String rowkey, String cf) throws IOException {//对表操作需要用HBaseAdminConnection connection = ConnectionFactory.createConnection(conf);//拿到表对象Table t = connection.getTable(TableName.valueOf(tableName));//1.根据rowkey删除数据Delete d = new Delete(Bytes.toBytes(rowkey));//2.删除
        t.delete(d);}//6.删除多行数据public static void deleteAll(String tableName, String... rowkeys) throws IOException {//对表操作需要用HBaseAdminConnection connection = ConnectionFactory.createConnection(conf);//拿到表对象Table t = connection.getTable(TableName.valueOf(tableName));//1.把delete封装到集合List<Delete> list = new ArrayList<Delete>();//2.遍历for (String row : rowkeys) {Delete d = new Delete(Bytes.toBytes(row));list.add(d);}t.delete(list);}//7.扫描表数据 scan全表扫描public static void scanAll(String tableName) throws IOException {//对表操作需要用HBaseAdminConnection connection = ConnectionFactory.createConnection(conf);//拿到表对象Table t = connection.getTable(TableName.valueOf(tableName));//1.实例scanScan s = new Scan();//2.拿到Scanner对象ResultScanner rs = t.getScanner(s);//3.遍历for (Result r : rs) {Cell[] cells = r.rawCells();//遍历具体数据for (Cell c : cells) {System.out.println("行键为:" + Bytes.toString(CellUtil.cloneRow(c)));System.out.println("列族为:" + Bytes.toString(CellUtil.cloneFamily(c)));System.out.println("值为:" + Bytes.toString(CellUtil.cloneValue(c)));}}}//8.扫描指定的数据public static void getRow(String tableName, String rowkey) throws IOException {//对表操作需要用HBaseAdminConnection connection = ConnectionFactory.createConnection(conf);//拿到表对象Table t = connection.getTable(TableName.valueOf(tableName));//1.扫描指定数据需要实例GetGet g = new Get(Bytes.toBytes(rowkey));//2.可加过滤条件g.addFamily(Bytes.toBytes("info"));Result rs = t.get(g);Cell[] cells = rs.rawCells();//3.遍历//遍历具体数据for (Cell c : cells) {System.out.println("行键为:" + Bytes.toString(CellUtil.cloneRow(c)));System.out.println("列族为:" + Bytes.toString(CellUtil.cloneFamily(c)));System.out.println("值为:" + Bytes.toString(CellUtil.cloneValue(c)));}}public static void main(String[] args) throws IOException {
//        System.out.println(isExist("emp11"));
//        createTable("zhaosi","henshuai","feichangshuai");
//        createTable("zhaosi","info");//        deleteTable("zhaosi");
//        createTable("yangmi","info");
//        addRow("yangmi","101","info","age","18");//        deleteRow("yangmi","101","info");
//        deleteAll("emp","1001","1002r");
//        scanAll("yangmi");getRow("lisi","102");}
}

转载于:https://www.cnblogs.com/areyouready/p/10091768.html

HBase简单API相关推荐

  1. HBase编程 API入门系列之HTable pool(6)

    HTable是一个比较重的对此,比如加载配置文件,连接ZK,查询meta表等等,高并发的时候影响系统的性能,因此引入了"池"的概念. 引入"HBase里的连接池" ...

  2. hbase java api样例(版本1.3.1,新API)

    hbase版本:1.3.1 目的:HBase新API的使用方法. 尝试并验证了如下几种java api的使用方法. 1.创建表 2.创建表(预分区) 3.单条插入 4.批量插入 5.批量插入(客户端缓 ...

  3. HBase–常用API操作篇

    2019独角兽企业重金招聘Python工程师标准>>> [常用到的几个类] 1. org.apache.hadoop.hbase.HBaseConfiguration 每一个hbas ...

  4. hbase java api 两种方式

    NoSQL Hbase JAVA API 实例一 导入架包: <dependency><groupId>org.apache.hbase</groupId>< ...

  5. 6 HBase java API访问HBase数据库

    HBase java API访问HBase数据库 package com.hunan.hbase_options;import org.apache.hadoop.conf.Configuration ...

  6. hbase rest api接口链接管理【golang语言版】

    # go-hbase-rest hbase rest api接口链接管理[golang语言版] 关于hbase的rest接口的详细信息可以到官网查看[http://hbase.apache.org/b ...

  7. Hadoop生态圈-Hbase的API常见操作

    Hadoop生态圈-Hbase的API常见操作 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.

  8. Hbase java API操作(模板代码)

    Hbase java API操作 1 创建maven工程 导入jar包 <repositories><repository><id>cloudera</id& ...

  9. ONE·一个简单API

    很喜欢韩寒的ONE·一个,喜欢每天的一幅图和一句话,所以制作了这个简单API,戳这里! 如果希望得到PC端的客户端,请猛戳这里~这是用VB写出来的一个简单的客户端小程序. 如果你希望在你的站点中引入它 ...

最新文章

  1. Apache Traffic Server 4.2.1/5.3.2上的坑!
  2. 找到一本不错的Linux电子书,附《Linux就该这么学》章节目录
  3. 用shell脚本计算日期的小函数们
  4. STM32中I2C总线上数据的读、写。
  5. dagger2 依赖注入
  6. 5、Power Query-抓取网页数据做漂亮的图表
  7. Hibernate Shards 数据的水平、垂直切割(一)- Hibernate测试环境
  8. python业余项目_学会这8个优秀 Python 库用于业余项目,将大大减少程序员耗费的精力...
  9. el图oracle,element-ui之el-image-viewer(图片查看器)
  10. 惠普HP CQ40 519TX XP系统安装以及XP驱动
  11. 【2023年中国法定节假日的订阅链接】
  12. .net是什么域名?域名注册需要实名制吗?
  13. 查找整数c语言程序,查找整数(示例代码)
  14. EditPlus格式化xml文档
  15. 云服务器deeplearning_吴恩达deeplearning.ai将于11月6日开放第四课,主讲卷积神经网络...
  16. 基于STM32F4的心电监护仪
  17. hdu 1276 士兵队列训练问题 (详解)
  18. 计算机是怎样运行的:从根儿上理解计算机
  19. linux centos 命令行 安装 teamviewers ___yyw合并版
  20. 图像仿射变换原理5:组合变换矩阵的OpenCV-Python实现

热门文章

  1. 盟军敢死队开发工具箱
  2. 年终总结既临近半年毕业感悟
  3. 我的第一个Android应用
  4. 【千锋教育】网络安全工程师-P1-day01-软件安装
  5. 江湖武侠浪迹天涯网站404页面源码
  6. 规划数据中心降低成本,需要了解的几个小技巧
  7. 如何禁用计算机防病毒程序,彻底禁用win10一些恶心的软件和功能(个人口味,可能再开启很费劲)...
  8. windows删除多余启动引导项
  9. java spring eventbus_Spring 事件:Application Event
  10. Curl windows下载地址