(1)查看表操作
list
(2)显示当前服务器状态
status 'hostname'
(3)显示当前用户
whoami
(4)创建表
create 'table_name','columnFamily'
(5)向表中添加数据
put 'table_name','rowkey','columnFamily:column_name','value'
(6)查询表
scan 'table_name'
(7)查询表的指定范围
scan 'table_name',{STARTROW => 'rowkey', STOPROW => 'rowkey'}
(8)查看表结构
describe 'table_name'
(9)查看表是否存在
exists 'table_name'
(10)变更表结构信息
alter 'table_name',{Name=>columnFamily',key=>'value'}
(11)查看指定数据信息
# 查看具体的rowkey
get 'table_name',‘rowkey’
# 查看具体的列
get 'table_name',‘rowkey’,'columnFamily:column_name'
(11)删除指定数据
# 删除具体的列
delete 'table_name','rowkey','columnFamily:column_name'
# 删除具体的rowkey
deleteall 'table_name','rowkey'
(13)清空表
truncate 'table_name'
(14)删除表
disable 'table_name'
drop 'tablename'
(15)统计rowkey个数
count 'table_name'
(16)分区创建表
create 'table_name','columnFamily','partition',SPLITS => ['rowkey1','rowkey2']
create 'table_name','columnFamily','partition',SPLITS_FILE => 'file'
(1)配置windows下C:\Windows\System32\drivers\etc的hosts文件
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
192.168.138.130 Hadoop1
192.168.138.129 Hadoop2
192.168.138.128 Hadoop3
(2)将core-site.xml、hdfs-site.xml和hbase-site.xml文件放到IDEA中的resources下
(3)常见的API操作
package com.Hbase.util;
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;
/**
* HbaseOperation class
*
* @author Administration
* @date 2019/05/20
*/
public class HBaseOperation {
private static Configuration conf;
private static Connection connection;
static {
try {
//获取HBase的配置
conf = HBaseConfiguration.create();
//创建HBase连接
connection = ConnectionFactory.createConnection(conf);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @param tableName table name
* @return boolean
* @throws IOException
*/
//判断HBase中表是否存在
public static boolean isExist(String tableName) throws IOException {
//获取HBase的管理
HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
return admin.tableExists(TableName.valueOf(tableName));
}
/**
* @param tableName table name
* @param columnFamily column Family
* @throws IOException
*/
//在HBase中创建表
public static void createTable(String tableName, String... columnFamily) throws IOException {
//获取HBase的管理
HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
//创建描述器
HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
//遍历列簇
for (String s: columnFamily){
hTableDescriptor.addFamily(new HColumnDescriptor(s));
}
//创建表
admin.createTable(hTableDescriptor);
}
/**
* @param tableName table name
* @param rowKey row key
* @param columnFamily column Family
* @param columnName column Name
* @param value
* @throws IOException
*/
//向表中添加数据
public static void addData(String tableName, String rowKey, String columnFamily, String columnName, String value) throws IOException {
//获取表
Table table = connection.getTable(TableName.valueOf(tableName));
//添加数据put方式
Put put = new Put(rowKey.getBytes());
//指定列簇、列名和值
put.addColumn(columnFamily.getBytes(),columnName.getBytes(),value.getBytes());
table.put(put);
}
/**
* @param tableName table name
* @param rowKey row key
* @throws IOException
*/
//删除行数据
public static void deleteData(String tableName, String rowKey) throws IOException {
//获取表
Table table = connection.getTable(TableName.valueOf(tableName));
//删除数据delete方式
Delete delete = new Delete(rowKey.getBytes());
table.delete(delete);
}
/**
* @param tableName table name
* @param rowKey row key
* @throws IOException
*/
//删除多行数据
public static void deleteRow(String tableName, String... rowKey) throws IOException {
//获取表
Table table = connection.getTable(TableName.valueOf(tableName));
//初始化List
List<Delete> list = new ArrayList<Delete>();
//遍历行ID
for (String s: rowKey){
//删除数据delete方式
Delete delete = new Delete(s.getBytes());
//添加delete到List中
list.add(delete);
}
table.delete(list);
}
/**
* @param tableName table name
* @throws IOException
*/
//查看表
public static void scanTable(String tableName) throws IOException {
//获取表
Table table = connection.getTable(TableName.valueOf(tableName));
//实例化Scan
Scan scan = new Scan();
//获取查看表的结果
ResultScanner scanner = table.getScanner(scan);
//遍历查询结果
for (Result rs: scanner){
//获取单元格
Cell[] cells = rs.rawCells();
//遍历单元格
for (Cell c: cells){
System.out.println("rowKey: "+ Bytes.toString(CellUtil.cloneRow(c)));
System.out.println("column+cell: "+Bytes.toString(CellUtil.cloneFamily(c))
+":"+Bytes.toString(CellUtil.cloneQualifier(c)));
System.out.println("value: "+Bytes.toString(CellUtil.cloneValue(c)));
}
}
}
/**
* @param tableName table name
* @throws IOException
*/
//删除表
public static void deleteTable(String tableName) throws IOException {
//获取HBase的管理
HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
//设置表不可用
admin.disableTable(TableName.valueOf(tableName));
//删除表
admin.deleteTable(TableName.valueOf(tableName));
}
public static void main(String[] args) {
try {
System.out.println(isExist("hbase"));
createTable("Destiny","info1","info2");
addData("Destiny","Zhang","info1","name","Jabin");
deleteData("Destiny","Zhang");
deleteRow("Destiny","1001","1002");
scanTable("Destiny");
deleteTable("Destiny");
} catch (IOException e) {
e.printStackTrace();
}
}
}