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; public class HbaseClients {
public static Connection connection;
public static void main(String[] args) throws Exception {
create("kgc","cf1","cf2","cf3");
//delete("kgc");
//scan("test02");
}
//初始化的参数,连接对象
static {
//conf
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "192.168.159.110");
conf.set("hbase.zookeeper.property.clientPort","");
//获取hbase链接对象ConnectionFactory
try {
connection = ConnectionFactory.createConnection(conf);
} catch (IOException e) {
e.printStackTrace();
}
}
//创建表
public static void create(String table,String ... familys) throws Exception {
//首先要拿到admin
Admin admin = connection.getAdmin();
//new
HTableDescriptor htable = new HTableDescriptor(TableName.valueOf(table));
//htable
for (String family : familys) {
htable.addFamily(new HColumnDescriptor(family));
}
admin.createTable(htable);
System.out.println("创建成功");
}
//删除表
public static void delete(String table) throws Exception {
Admin admin = connection.getAdmin();
//判断表是否存在
if(admin.tableExists(TableName.valueOf(table))){
//下线了
admin.disableTable(TableName.valueOf(table));
//删除
admin.deleteTable(TableName.valueOf(table));
System.out.println("删除成功");
}else{
System.out.println("对不起,表不存在");
}
}
//添加数据
public static void adddata(String table,String rowkey,String columnFamily,String column,String value) throws Exception {
Table table1 = connection.getTable(TableName.valueOf(table));
//NEW PUT
Put puts = new Put(Bytes.toBytes(rowkey));
//puts
puts.add(Bytes.toBytes(columnFamily),Bytes.toBytes(column),Bytes.toBytes(value));
table1.put(puts);
System.out.println("添加数据成功");
}
//删除一行(rowkey)数据
public static void delterow(String table,String rowkey) throws Exception {
Table table1 = connection.getTable(TableName.valueOf(table));
//new Delete
Delete deletes = new Delete(Bytes.toBytes(rowkey));
//删除
table1.delete(deletes);
System.out.println("删除一行数据成功");
}
//删除多行数据
public static void deleterows(String table,String ... rowkeys) throws Exception {
Table table1 = connection.getTable(TableName.valueOf(table));
//new List
ArrayList<Delete> list = new ArrayList<>();
for (String rowkey : rowkeys) {
list.add(new Delete(Bytes.toBytes(rowkey)));
}
table1.delete(list);
System.out.println("删除多行成功");
}
//scan扫描
public static void scan(String table) throws Exception {
Table table1 = connection.getTable(TableName.valueOf(table));
//new Scan
Scan scan = new Scan();
//results 就是所有的rowkey的集合
ResultScanner results = table1.getScanner(scan);
for (Result result : results) {
//cells 的集合
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.println(Bytes.toString(cell.getRow()));
System.out.println(Bytes.toString(cell.getFamily()));
System.out.println(Bytes.toString(cell.getQualifier()));
System.out.println(Bytes.toString(cell.getValue()));
}
}
}
//get
public static void get(String table,String rowkey) throws Exception {
Table table1 = connection.getTable(TableName.valueOf(table));
//new Get
Get get = new Get(Bytes.toBytes(rowkey));
Result result = table1.get(get);
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.println(Bytes.toString(cell.getRow()));
System.out.println(Bytes.toString(cell.getFamily()));
System.out.println(Bytes.toString(cell.getQualifier()));
System.out.println(Bytes.toString(cell.getValue()));
}
}
}

hbase的API的更多相关文章

  1. Hbase客户端API基础小结笔记(未完)

    客户端API:基础 HBase的主要客户端接口是由org.apache.hadoop.hbase.client包中的HTable类提供的,通过这个类,用户可以完成向HBase存储和检索数据,以及删除无 ...

  2. HBase伪分布式环境下,HBase的API操作,遇到的问题

    在hadoop2.5.2伪分布式上,安装了hbase1.0.1.1的伪分布式 利用HBase的API创建个testapi的表时,提示  Exception in thread "main&q ...

  3. 使用hbase的api创建表时出现的异常

    /usr/lib/jvm/java-7-openjdk-amd64/bin/java -Didea.launcher.port=7538 -Didea.launcher.bin.path=/usr/l ...

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

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

  5. HBase Python API

    HBase Python API HBase通过thrift机制可以实现多语言编程,信息通过端口传递,因此Python是个不错的选择 吐槽 博主在Mac上配置HBase,奈何Zoomkeeper一直报 ...

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

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

  7. HBase编程 API入门系列之create(管理端而言)(8)

    大家,若是看过我前期的这篇博客的话,则 HBase编程 API入门系列之put(客户端而言)(1) 就知道,在这篇博文里,我是在HBase Shell里创建HBase表的. 这里,我带领大家,学习更高 ...

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

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

  9. HBase编程 API入门系列之delete(客户端而言)(3)

    心得,写在前面的话,也许,中间会要多次执行,连接超时,多试试就好了. 前面的基础,如下 HBase编程 API入门系列之put(客户端而言)(1) HBase编程 API入门系列之get(客户端而言) ...

  10. HBase编程 API入门系列之get(客户端而言)(2)

    心得,写在前面的话,也许,中间会要多次执行,连接超时,多试试就好了. 前面是基础,如下 HBase编程 API入门系列之put(客户端而言)(1) package zhouls.bigdata.Hba ...

随机推荐

  1. Windows系统下安装MySQL详细教程(命令安装法)

    1.安装包下载. 下载地址:https://dev.mysql.com/downloads/mysql/ 点击下载之后,可以选择注册Oracle账号,也可以跳过直接下载. 下载完成后,选择一个磁盘内放 ...

  2. 源讯科技(中国)有限公司(Atos Worldline)

    源讯公司是欧洲***的IT服务公司,去年营收达到88亿欧元,在全球52个国家拥有77100名员工.Worldline为Atos(源讯)全资子公司,专注于金融支付领域.Worldline在B2B及B2C ...

  3. JS比较两个时间的时间差

    /** * 比较两个时间的时间差 * @param startTime 开始时间 * @param endTime 结束时间 * @demo compareTime(new Date('2019-12 ...

  4. 费用流+SPFA ||Luogu P3381【模板】最小费用最大流

    题面:[模板]最小费用最大流 代码: #include<cstdio> #include<cstring> #include<iostream> #include& ...

  5. Composer\Downloader\TransportException ... Failed to enable crypto,failed to open stream: operation failed

    failed to open stream: operation failed 错误详细信息: [Composer\Downloader\TransportException] The "h ...

  6. Python中numpy的应用

    #创建ndarray import numpy as np nd = np.array([2,4,6,'])#numpy中默认ndarray的所有元素的数据类型是相同,如果数据的类型不同,会统一为统一 ...

  7. CSS3边框 圆角效果 border-radius

    border-radius是向元素添加圆角边框 使用方法: border-radius:10px; /* 所有角都使用半径为10px的圆角 */ border-radius: 5px 5px 5px ...

  8. [ByteCTF 2019]EZCMS

    题目复现链接:https://buuoj.cn/challenges 参考链接:ByteCTF_2019&XNUCA_2019部分web题复现 一.知识点 1.源码泄露 访问www.zip获取 ...

  9. 【JavaScript】 命名空间污染解决

    闭包解决命名空间污染问题 var init = (function () { var name = "zhangsan", age = 12, sex = "male&q ...

  10. 阿里云服务器不能使用apt-get

    因为阿里云使用的是自己的源.所以在/etc/apt/sources.list中加上: deb cdrom:[Ubuntu 16.04.3 LTS _Xenial Xerus_ - Release am ...