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. python 查询Neo4j多节点的多层关系

    需求:查询出满足3人及3案有关系的集合 # -*- coding: utf-8 -*- from py2neo import Graph import psycopg2 # 二维数组查找 def fi ...

  2. 解决Ubuntu环境下在pycharm中导入tensorflow报错问题

    环境: Ubuntu 16.04LTS anacoda3-5.2.0 问题: ImportError: No module named tensorflow 原因:之前安装的tensorflow所用到 ...

  3. nodejs 操作 mysql

    1.安装插件 npm install mysql 2.调用代码 var mysql = require('mysql') var connection = mysql.createConnection ...

  4. dockerfile制作笔记

    dockerfile语法格式:   FROM: 基础镜像(就是在什么镜像上面做)   MAINTAINER: 镜像创建者信息(作者的信息)   EXPOSE: 开放的端口   ENV: 设置变量   ...

  5. 微信授权获取code/openid

    微信网页授权 如果用户在微信客户端中访问第三方网页,公众号可以通过微信网页授权机制,来获取用户基本信息,进而实现业务逻辑. 关于网页授权回调域名的说明 1.在微信公众号请求用户网页授权之前,开发者需要 ...

  6. 在 LaTeX 中同步缩放 TikZ 与其中的 node

    PGF/TikZ 是 Till Tantau 开发的基于 TeX 的绘图引擎.因其可以直接在 LaTeX 文稿中通过代码绘制向量图,所以是目前流行的 LaTeX 绘图解决方案之一. 在 tikzpic ...

  7. 查看jar包内容

    查看jar包内容 查看jar包内容的基本命令: jar tf jar-file 参数解释: The t option indicates that you want to view the table ...

  8. vue 父子组件渲染

    问题描述:父组件调用了一个子组件,传递了一个id的属性到子组件, 但是在子组件中将这个id的props属性赋值给了data里面定义的另外一个属性myId,并且写了watch监听这个id的props. ...

  9. Linux内核设计与实现 总结笔记(第十章)内核同步方法

    一.原子操作 原子操作可以保证指令以原子的方式执行----执行过程不被打断. 1.1 原子整数操作 针对整数的原子操作只能对atomic_t类型的数据进行处理. 首先,让原子函数只接收atomic_t ...

  10. 题解 P1433 【吃奶酪】

    这道题是一道著名的NP问题. 正解应该是DP,但我在这里讲一种近似算法--爬山. 希望某些dalao注意一下爬山与模拟退火的区别. 爬山是直往低处往高处爬,每次取大的,也就是一种贪心思想. 而模拟退火 ...