一、搭建开发环境:
a.创建java工程
1)src下创建 lib文件夹,构建路径
2)配置文件拷贝到src下
b.导HBase相关开发jar,1.3.1版本!!
在工程下创建文件夹lib,
解压D:\jar包\hbase-1.3.1\lib 共111个,构建路径
c.在 Windows 下开发、在 HDFS 上测试
需要将 HBase 的 hbase-site.xml、hdfs-site.xml、core-site.xml、log4j.properties
配置文件拷贝到项目的 src 目录下
1)将文件通过挂载盘从linux系统拷贝到window系统
2)远程链接工具
[hyxy@master hadoop]$ cp hbase-site.xml /mnt/hgfs/工具/
[hyxy@master hadoop]$ cp hdfs-site.xml /mnt/hgfs/工具/
[hyxy@master hadoop]$ cp /mnt/hgfs/工具/
3)在 Windows->System32->drivers->etc->hosts 文件中
(将文件复制出来修改),配置:
* 192.168.48.128 master
* 192.168.48.129 slave1
* 192.168.48.130 slave2
d.编写HBaseTool工具类
private static Configuration conf;
private static Connection conn;
static{
conf = HBaseConfiguration.create();
try {
conn = ConnectionFactory.createConnection(conf);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 创建名字空间
* @param namespace
* $>create_namespace 'ns1'
* 通过hbase shell 查看创建命名空间 list_namespace
*/
public static void createNS(String namespace){
Admin admin = null;
try {
admin = conn.getAdmin();
NamespaceDescriptor nsdesc = NamespaceDescriptor.create(namespace).build();
admin.createNamespace(nsdesc);
System.out.println("---创建成功!---");
} catch (IOException e) {
e.printStackTrace();
System.out.println("---创建失败!---");
}finally{
try {
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 查询命名空间hbase(main):003:0> list_namespace
*
*/
public static void listNS(){
Admin admin = null;
try {
admin = conn.getAdmin();
NamespaceDescriptor[] nss = admin.listNamespaceDescriptors();
for (NamespaceDescriptor ns : nss) {
System.out.println(ns);
}
System.out.println("---查询成功!---");
} catch (IOException e) {
e.printStackTrace();
System.out.println("---查询失败!---");
}finally{
try {
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 删除命名空间
* @param namespace
* $>drop_namespace 'ns1'
*/
public static void deleteNS(String namespace){
Admin admin = null;
try {
admin = conn.getAdmin();
admin.deleteNamespace(namespace);
System.out.println("---删除成功!---");
} catch (IOException e) {
e.printStackTrace();
System.out.println("---删除失败!---");
} finally{
try {
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 创建Table
* $>create 'ns1:student','f1'
*/
public static void createTable(String tablename,String colunmfamily){
Admin admin = null;
try {
admin = conn.getAdmin();
TableName tname = TableName.valueOf(tablename);
HTableDescriptor htdesc = new HTableDescriptor(tname);
HColumnDescriptor hcdesc = new HColumnDescriptor(colunmfamily);
htdesc.addFamily(hcdesc);
admin.createTable(htdesc);
System.out.println("---表创建成功!---");
} catch (IOException e) {
e.printStackTrace();
System.out.println("---表创建失败!---");
}finally{
try {
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//查看表 $>list
public static void getTable() {
try {
HTableDescriptor[] listtable = admin.listTables();
for(HTableDescriptor tables :listtable) {
System.out.println(tables);
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 删除Table
* $> disable 'ns1:student'
* $> drop 'ns1:student'
*/
public static void dropTable(String tablename){
Admin admin = null;
try {
admin = conn.getAdmin();
TableName tname = TableName.valueOf(tablename);
admin.disableTable(tname);
admin.deleteTable(tname);
System.out.println("---表删除成功!---");
} catch (IOException e) {
e.printStackTrace();
System.out.println("---表删除失败!---");
}finally{
try {
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 单行put
* $>put 'ns1:student','row-1','f1:name','zhangsan'
*/
//插入数据
public static void putTable(String tablename){
TableName tableName =TableName.valueOf(tablename);
try {
HTable table = new HTable(conf,tableName);
for(int i = 20; i<=23 ; i++) {
byte[] row = Bytes.toBytes("row"+i);
Put put = new Put(row);
byte[] columFamily = Bytes.toBytes("fA");
byte[] qualifier = Bytes.toBytes(String.valueOf(i));
byte[] value = Bytes.toBytes("value"+i);
put.addColumn(columFamily,qualifier,value);
table.put(put);
}
System.out.println("put 成功!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 多行put
*
*/
public static void puts(String tablename,List
try {
Table _table = conn.getTable(TableName.valueOf(tablename));
for (int i = 0; i < puts.size(); i++) {
_table.put(puts.get(i));
}
System.out.println("---插入成功!---");
} catch (IOException e) {
e.printStackTrace();
System.out.println("---插入失败!---");
}
}
测试:
public static void main(String[] args) throws IOException {
List
for (int i = 3; i < 100; i++) {
Put _put = new Put(Bytes.toBytes("row-"+i));
_put.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("name"), Bytes.toBytes("asa"+i));
_put.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("age"), Bytes.toBytes("23"));
_put.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("sex"), Bytes.toBytes("男"+i));
_put.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("class"), Bytes.toBytes("BD1707"+i));
_puts.add(_put);
}
HBaseTool.puts("ns1:t1", _puts);
}
HBase以表的方式存储数据。
表是由行和列构成的,列从属于某一个列族(column family)。
行和列的交叉点称之为cell,cell是版本化的,
cell的内容就是数据,cell中的数据是没有类型的,
全部是字节码形式存贮,是不可分割的字节数组。
/**
*
* $>get 'ns1:student','row-1','baseinfo:name'
*/
public static void getTableCell(String tablename){
try {
Table _table = conn.getTable(TableName.valueOf(tablename));
Get _get = new Get(Bytes.toBytes("row-1"));
Result rs = _table.get(_get);
byte[] val = rs.getValue("baseinfo".getBytes(),"name".getBytes());
System.out.println(Bytes.toString(val));
} catch (IOException e) {
e.printStackTrace();
System.out.println("---失败!---");
}
}
/**
* 查询单条记录通过rowkey查询 get 'tableName','rowKey'
* @param tablename
* @param rowkey
*/
public static void getone(String tablename,String rowkey){
try {
Table _table = conn.getTable(TableName.valueOf(tablename));
//get 对象首先指定rowkey
Get _get = new Get(Bytes.toBytes(rowkey));
Result rs = _table.get(_get);
List
for (Cell cell : cells) {
System.out.println("列簇为:"+Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("限定符为:"+Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("rowkey为:"+Bytes.toString(CellUtil.cloneRow(cell)));
System.out.println("value为:"+Bytes.toString(CellUtil.cloneValue(cell)));
}
System.out.println("---查询成功!---");
} catch (IOException e) {
e.printStackTrace();
System.out.println("---查询失败!---");
}
}
/**
* 查询单条记录,并返回指定Version数
* @param tablename
* @param rowkey
*/
public static void getone(String tablename,String rowkey,int version){
try {
Table _table = conn.getTable(TableName.valueOf(tablename));
Get _get = new Get(Bytes.toBytes(rowkey));
_get.setMaxVersions(version);
Result rs = _table.get(_get);
List
for (Cell cell : cells) {
System.out.println("列簇为:"+Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("限定符为:"+Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("rowkey为:"+Bytes.toString(CellUtil.cloneRow(cell)));
System.out.println("value为:"+Bytes.toString(CellUtil.cloneValue(cell)));
System.out.println("timestamp为:"+cell.getTimestamp());
}
System.out.println("---查询成功!---");
} catch (IOException e) {
e.printStackTrace();
System.out.println("---查询失败!---");
}
}
/**
* 按照hbase shell 格式输出
*
*/
private static void printOut(Result rs){
List
for (Cell cell : cells) {
String family = Bytes.toString(CellUtil.cloneFamily(cell));
String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
String rowkey = Bytes.toString(CellUtil.cloneRow(cell));
String value = Bytes.toString(CellUtil.cloneValue(cell));
long timestamp = cell.getTimestamp();
System.out.println(rowkey+" column="+family+":"+qualifier+", timestamp="+timestamp+", value="+value);
}
}
/**
* 查询所有
* @param tablename
* @param rowkey
*/
public static void findAll(String tablename){
try {
Table _table = conn.getTable(TableName.valueOf(tablename));
Scan scan = new Scan();
ResultScanner rsscan = _table.getScanner(scan);
Iterator
while (its.hasNext()) {
printOut(its.next());
System.out.println("---查询成功!---");
} catch (IOException e) {
e.printStackTrace();
System.out.println("---查询失败!---");
}
}
/**
* 查询startkey至endkey之间的数据
* @param tablename
* @param rowkey
findByRowkey("ns1:student","row20","row23");
*/
public static void findByRowkey(String tablename,String startkey,String endkey){
try {
Table _table = conn.getTable(TableName.valueOf(tablename));
Scan scan = new Scan(Bytes.toBytes(startkey),Bytes.toBytes(endkey));
ResultScanner rsscan = _table.getScanner(scan);
Iterator
while (its.hasNext()) {
printOut(its.next());
}
System.out.println("---查询成功!---");
} catch (IOException e) {
e.printStackTrace();
System.out.println("---查询失败!---");
}
}
// 按rowKey删除 $> deleteall 'ts1','33'
public static void deleteRowKey(String tableName,String rowkey) throws IOException {
Table _table = conn.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(Bytes.toBytes(rowkey));
_table.delete(delete);
}
/**
* 切分Table
* 查看HDFS split 'tablename','row-1'
*/
public static void splittable(String tablename){
Admin admin = null;
try {
admin = conn.getAdmin();
admin.split(TableName.valueOf(tablename),Bytes.toBytes("row-1"));
System.out.println("---切分成功!---");
} catch (IOException e) {
e.printStackTrace();
System.out.println("---切分失败!---");
}finally{
try {
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 切分region和table 相同
* split 'regionname','row-1'
*/
public static void splitregion(String regionname){
Admin admin = null;
try {
admin = conn.getAdmin();
admin.splitRegion(Bytes.toBytes(regionname),"row-nu".getBytes());
System.out.println("---切分成功!---");
} catch (IOException e) {
e.printStackTrace();
System.out.println("---切分失败!---");
}finally{
try {
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
合并region:
admin = conn.getAdmin();
admin.mergeRegions("ffd7e19bf6dabc990aa9da07cea32afc".getBytes(), "27e639cc8d5d43d4ff25d38c59a3bc1e".getBytes(), true);
移动region到指定节点:
admin = conn.getAdmin();
admin.move("ffd7e19bf6dabc990aa9da07cea32afc".getBytes(), "slave1,16020,1558184978689".getBytes());