|的ascII最大
ctrl+shift+t查找类  ctrl+p显示提示

HBase API操作

依赖的jar包

<dependencies>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.3.</version>
</dependency> <dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.3.</version>
</dependency> </dependencies>
public class TestHbase {
//1.构建Configuration, Connection, Admin
//Configuration 持有了zk的信息,进而hbase集群的信息可以间接获得
public static Configuration conf;
//Connection hbase连接 借助配置信息 获得连接
public static Connection connection;
public static Admin admin;
static{ //为静态属性初始化,或者说辅助类初始化
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "hadoop101,hadoop102,hadoop103");
try {
connection = ConnectionFactory.createConnection(conf);
} catch (IOException e) {
e.printStackTrace();
}
//admin
try {
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
} }
//1.创建库
public static void createNS(String namespace) throws IOException {
//①构建 ns的描述器 声明库名
NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create(namespace).build();
//②创建库
try{
admin.createNamespace(namespaceDescriptor);
}catch (NamespaceExistException e){
System.out.println("该库已经存在!");
}
//③关资源
admin.close();
}
    //2.判断表是否存在
public static boolean isExists(String tableName) throws IOException {
boolean exists = admin.tableExists(TableName.valueOf(tableName));
System.out.println("exits:" + exists);
admin.close();
return exists;
}
    //3.创建表
public static void createTable(String tableName, String... info) throws IOException {
//①HTableDescriptor
HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
//②添加columnFamily 列族
for (String cf : info) {
hTableDescriptor.addFamily(new HColumnDescriptor(cf));
}
//③建表
admin.createTable(hTableDescriptor);
//④释放资源
admin.close();
}
    public static void deleteTable(String tableName) throws IOException {
//禁用并删除表
admin.disableTable(TableName.valueOf(tableName));
admin.deleteTable(TableName.valueOf(tableName));
admin.close();
}
    //5.插入数据  put 'student','1001','cf1:name','kris'
public static void insertData(String tableName, String rowkey, String column, String value) throws IOException {
//①获取table
Table table = connection.getTable(TableName.valueOf(tableName));
//②获得put
Put put = new Put(Bytes.toBytes(rowkey));//把String类型转成bytes类型
put.addColumn(Bytes.toBytes(column.split(":")[0]), Bytes.toBytes(column.split(":")[1]),
Bytes.toBytes(value));
table.put(put); //③添加数据
table.close();//④释放资源
}
//6.删除数据
public static void deleteData(String tableName, String... rowkey) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
for (String rk : rowkey) {
Delete del = new Delete(Bytes.toBytes(rk));//获得delete对象,其中持有要删除行的rowkey
table.delete(del);
}
table.close();
}
//7.查询
public static void queryAll(String tableName) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
ResultScanner results = table.getScanner(scan);
for (Result result : results) { //result对应一行数据
Cell[] cells = result.rawCells(); //获取一行的所有cells
for (Cell cell : cells) {
String rowkey = Bytes.toString(CellUtil.cloneRow(cell));//
String family = Bytes.toString(CellUtil.cloneFamily(cell));
String column = Bytes.toString(CellUtil.cloneQualifier(cell));
String value = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println("rowkey:" + rowkey + "\t" + family + ":" + column
+"\t" + value);
}
}
}
//8.查询单行
public static void getRow(String tableName, String rowkey) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowkey));
get.addColumn(Bytes.toBytes("cf2"), Bytes.toBytes("name"));
//get.addFamily(Bytes.toBytes("cf1")); //如果不追加列族,则查询所有列族
Result result = table.get(get);
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.println("查询单行");
String row = Bytes.toString(CellUtil.cloneRow(cell));
String family = Bytes.toString(CellUtil.cloneFamily(cell));
String column = Bytes.toString(CellUtil.cloneQualifier(cell));
String value = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println("row:" + row +"\t" + family + ":" + column + "\t" + value);
}
}

HBase API操作的更多相关文章

  1. 5.Hbase API 操作开发

    Hbase API 操作开发需要连接Zookeeper进行节点的管理控制 1.配置 HBaseConfiguration: 包:org.apache.hadoop.hbase.HBaseConfigu ...

  2. HBASE API操作问题总结

    org.apache.hadoop.hbase.MasterNotRunningException 在centos中查看,发现没有HMaster进程 解决方法: 1.启动hadoop后,需要等一段时间 ...

  3. Hbase——API操作

    1.判断表是否存在 public static boolean isTableExit(String tableName) throws IOException { // //获取配置文件信息 // ...

  4. Hbase Shell命令详解+API操作

    HBase Shell 操作 3.1 基本操作1.进入 HBase 客户端命令行,在hbase-2.1.3目录下 bin/hbase shell 2.查看帮助命令 hbase(main):001:0& ...

  5. 大数据技术之_11_HBase学习_02_HBase API 操作 + HBase 与 Hive 集成 + HBase 优化

    第6章 HBase API 操作6.1 环境准备6.2 HBase API6.2.1 判断表是否存在6.2.2 抽取获取 Configuration.Connection.Admin 对象的方法以及关 ...

  6. HBase 6、用Phoenix Java api操作HBase

    开发环境准备:eclipse3.5.jdk1.7.window8.hadoop2.2.0.hbase0.98.0.2.phoenix4.3.0 1.从集群拷贝以下文件:core-site.xml.hb ...

  7. HBase API 基础操作

    对于数据操作,HBase支持四类主要的数据操作,分别是: Put :增加一行,修改一行 Delete :删除一行,删除指定列族,删除指定column的多个版本,删除指定column的制定版本等 Get ...

  8. 使用IDEA操作Hbase API 报错:org.apache.hadoop.hbase.client.RetriesExhaustedException的解决方法:

     使用IDEA操作Hbase API 报错:org.apache.hadoop.hbase.client.RetriesExhaustedException的解决方法: 1.错误详情: Excepti ...

  9. Java API 操作HBase Shell

    HBase Shell API 操作 创建工程 本实验的环境实在ubuntu18.04下完成,首先在改虚拟机中安装开发工具eclipse. 然后创建Java项目名字叫hbase-test 配置运行环境 ...

随机推荐

  1. PHP--php时间差8个小时的问题

    自PHP5.0开始,用PHP获取系统时间时,时间比当前时间少8个小时.原因是PHP.ini中没有设置timezone时,PHP是使用的UTC时间,所以在中国时间要少8小时. 解决办法: 1.在PHP. ...

  2. 【原创】大数据基础之Hive(4)hive元数据库核心表结构

    1 dbs +-------+-----------------------+----------------------------------------------+------------+- ...

  3. [C]内存管理、内存泄露、堆栈

    原文地址:https://www.cnblogs.com/youthshouting/p/4280543.html,转载请注明源地址. 1.内存分配区间:         对于一个C语言程序而言,内存 ...

  4. Mvc 批量图片上传

    首先导入文件(官网上下载 kindeditor ): <link href="~/kindeditor-4.1.11-zh-CN/kindeditor/themes/default/d ...

  5. PHP use闭包函数

    <?php class Cart { //产品价格 const PRICE_BUTTER = 1.00; const PRICE_MILK = 3.00; const PRICE_EGGS = ...

  6. linux压缩与解压(持续更新)

    压缩 tar cvzf  w.tar.gz  xxx1 xxx2  对应解压:tar xvzf w.tar.gz

  7. python之dict与set实现原理之hash算法

    理解不透彻,下回分解 http://www.cnblogs.com/pengsixiong/p/5326893.html https://blog.csdn.net/zhao_crystal/arti ...

  8. 各数据库连接maven配置

    Derbydb driver maven dependency<dependency> <groupId>org.apache.derby</groupId> &l ...

  9. ?:,reverse,vector的基本小结

    #include <cstdio> //此代码为网上所复制 #include <iostream> #include <string> #include <s ...

  10. jQuery为div添加select和option

    简单描述:用jQuery给页面添加select下拉框,直接上图 总结:清楚明了^_^