常用HBase操作
HBase是一个分布式、面向列的数据库,可以用来存储非结构化和半结构化的松散数据,具有高可靠、高性能、面向列、可伸缩的特性。通过行键(RowKey)、列族(ColumnFamily)、列(Column)、时间戳(TimeTape)四个维度对数据进行定位。
首先启动Hadoop:切换目录,cd /usr/local/hadoop,启动命令: ./sbin/start-dfs.sh
启动HBase:切换目录,cd /usr/local/hbase,启动命令: ./bin/start-hbase.sh
(若配置了PATH环境变量,可直接输入start-dfs.sh,start-hbase.sh 启动HBase)
Shell命令操作HBase
进入Shell界面:bin/hbase shell
创建表:
create 'Student' , 'name' , 'sex' , 'age' :student为表名,name、sex、age为列族
查看表:
list :列出HBase中有哪些表
describe ‘Student’ :查看表的具体信息
添加数据:
put 'Student' , '2017001' , 'name' , 'ZJ':向Student表添加学号为2017001,姓名为ZJ的一个数据,其行键为2017001。(HBase表中会有一个系统默认的属性作为行键,无需自行创建,默认为put命令操作中表名后的第一个数据。注意:一次只能为一个表的一行数据的一个列,也就是一个单元格添加一个数据,所以直接用shell命令插入数据效率很低,一般通过编程操作数据)
查看数据:
scan 'Student':查看Student表的全部数据
get 'Student' , '2017001':查看行键为2017001的一行数据
删除数据:
delete 'Student' ,'2017001' , 'name':删除Student表中行键为2017001的姓名(删除一个数据)
deleteall 'Student' , '2017001':删除一行数据
truncate 'Student':清除Student表的全部数据
删除表:有两步,第一步先让该表不可用,第二步删除表
①disable 'Student' ②drop 'Student'
退出对数据库表的操作:exit
Java API编程操作数据库
建立连接:
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory; public class Connect
{
public static Configuration conf;
public static Connection conn;
public Admin admin;
public void init() throws IOException
{
conf=HBaseConfiguration.create();
conf.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
conn=ConnectionFactory.createConnection(conf);
admin=conn.getAdmin();
}
public void close() throws IOException
{
if(admin!=null)
admin.close();
if(conn!=null)
conn.close();
}
}
Connect
创建表: (create)
public void createTable() throws IOException
{
init();
TableName tName=TableName.valueOf(name);
if(admin.tableExists(tName))
System.out.println("table is exists!!!");
else
{
HTableDescriptor descriptor=new HTableDescriptor(tName);
for(String str:colFamily)
{
HColumnDescriptor col=new HColumnDescriptor(str);
descriptor.addFamily(col);
}
admin.createTable(descriptor);
System.out.println("Create table success!");
}
close();
}
createTable
查看Hbase中的表:(list)
public void list() throws IOException
{
init();
HTableDescriptor descriptors[]=admin.listTables();
for(HTableDescriptor des:descriptors)
System.out.println(des.getNameAsString());
close();
}
list
添加一个数据:(put)
public void insertRow(String name,String rowKey,String colFamily, String col,String value) throws IOException
{
init();
Table table=conn.getTable(TableName.valueOf(name));
Put put=new Put(rowKey.getBytes());
put.addColumn(colFamily.getBytes(), col.getBytes(), value.getBytes());
table.put(put);
table.close();
close();
}
insertRow
查看表中的全部数据:(scan)
public void getData(String name) throws IOException
{
init();
Table table=conn.getTable(TableName.valueOf(name));
Scan scan=new Scan();
ResultScanner scanner=table.getScanner(scan);
for(Result result:scanner)
{
showCell(result);
System.out.println();
}
table.close();
close();
}
public void showCell(Result result)
{
Cell[] cells=result.rawCells();
for(Cell cell:cells)
{
System.out.println("rowKey(行键):"+new String(CellUtil.cloneRow(cell)));
System.out.println("column Family(列族):"+new String(CellUtil.cloneFamily(cell)));
System.out.println("column Name(列限定符):"+new String(CellUtil.cloneQualifier(cell)));
System.out.println("Value(值):"+new String(CellUtil.cloneValue(cell)));
System.out.println("timeTamp(时间戳):"+cell.getTimestamp());
}
}
getData
删除表:(disable、drop)
public void deleteTable(String name) throws IOException
{
init();
TableName tName=TableName.valueOf(name);
if(admin.tableExists(tName))
{
admin.disableTable(tName);
admin.deleteTable(tName);
}
close();
}
deleteTable
常用HBase操作的更多相关文章
- 实验3- 熟悉常用的 HBase 操作
石家庄铁道大学信息科学与技术学院 实验报告 2018年----2019年 第一学期 题目: 熟悉常用的 HBase ...
- HBase操作(Shell与Java API)
版权声明:本文为博主原创文章,未经博主允许不得转载. 转: http://blog.csdn.net/u013980127/article/details/52443155 下面代码在Hado ...
- Mysql常用表操作 | 单表查询
160905 常用表操作 1. mysql -u root -p 回车 输入密码 2. 显示数据库列表 show databases 3. 进入某数据库 use database data ...
- SNMP常用数据操作
SNMP常用数据操作 snmp编程中常见的数据类型基本上就是integer32/oct_str(字节数组)/counter64/timeticks/dateAndTime这些.很多其它的比如Truth ...
- 常用SQL操作(MySQL或PostgreSQL)与相关数据库概念
本文对常用数据库操作及相关基本概念进行总结:MySQL和PostgreSQL对SQL的支持有所不同,大部分SQL操作还是一样的. 选择要用的数据库(MySQL):use database_name; ...
- 总结Selenium自动化测试方法(四)WebDriver常用的操作
四.WebDriver常用的操作 1.控制浏览器操作 #控制浏览器的大小 self.driver.set_window_size(480,800) #控制浏览器返回 self.driver.back( ...
- MATLAB 常用形态学操作函数
常用形态学操作函数(转自:http://blog.sina.com.cn/s/blog_4c52e9e20100e5if.html) 1.dilate函数 该函数能够实现二值图像的膨胀操作,有以下形式 ...
- 关于Properties类常用的操作
import java.io.*;import java.util.Enumeration;import java.util.Properties;/** * 关于Properties类常用的操作 * ...
- Jedis对Redis的常用命令操作
本篇主要总结一些Jedis对Redis的常用命令操作: 1.对key操作命令 2.对String操作命令 3.对List操作命令 4.对Set操作命令 5.对Hash操作命令 6.排序操作指令 一.项 ...
随机推荐
- n的阶乘尾数有几个0
/* n!尾数有几个0 */ #include <iostream> using namespace std; void find0(int n); int find(int i,int ...
- [系列] go-gin-api 规划目录和参数验证(二)
目录 概述 规划目录结构 模型绑定和验证 自定义验证器 制定 API 返回结构 源码地址 go-gin-api 系列文章 概述 首先同步下项目概况: 上篇文章分享了,使用 go modules 初始化 ...
- 浅析ebtables的概念和一些基本应用
一.ebtables 是什么? ebtables和iptables类似,都是Linux系统下网络数据包过滤的配置工具. 为什么叫配置工具呢? 是因为他们只制定规则,具体的实施者是内核!也就是说 ...
- hibernate 报错com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException
解释:JavaBean的主键类型只能是int类型,因为在映射关系配置是自动增长的,String类型是不能自动增长的,所以会报错.
- Spring 核心技术(7)
接上篇:Spring 核心技术(6) version 5.1.8.RELEASE 1.6 定制 Bean 的特性 Spring Framework 提供了许多可用于自定义 bean 特性的接口.本节将 ...
- HDU 6430 Problem E. TeaTree(虚树)
Problem E. TeaTree Problem Description Recently, TeaTree acquire new knoledge gcd (Greatest Common D ...
- codeforces 402 D. Upgrading Array(数论+贪心)
题目链接:http://codeforces.com/contest/402/problem/D 题意:给出一个a串和素数串b .f(1) = 0; p为s的最小素因子如果p不属于b , 否则 . a ...
- 【LeetCode】524-通过删除字母匹配到字典里最长单词
题目描述 给定一个字符串和一个字符串字典,找到字典里面最长的字符串,该字符串可以通过删除给定字符串的某些字符来得到.如果答案不止一个,返回长度最长且字典顺序最小的字符串.如果答案不存在,则返回空字符串 ...
- CF979C Kuro and Walking Route(简单的dfs/树形dp)
题意:给出一个$n$个点,$n-1$条边的无向连通图,给出两个点$x,y$,经过$x$后的路径上就不能经过$y$,问可以走的路径$(u,v)$有多少条,($(u,v)$和$(v,u)$考虑为两条不同的 ...
- FreeSql (二十四)Linq To Sql 语法使用介绍
原本不支持 IQueryable 主要出于使用习惯的考虑,如果继承 IQueryable,编写代码的智能总会提示出现一堆你不想使用的方法(对不起,我有强迫症),IQueryable 自身提供了一堆没法 ...