HBase的java客户端测试(一)---DDL操作

时间:2021-08-26 19:53:11

测试准备

【首先同步时间:】

for node in CloudDeskTop master01 master02 slave01 slave02 slave03;do ssh $node "date -s '2017-12-30 21:32:30'";done

【slave各节点启动zookeeper集群:】

cd /software/zookeeper-3.4.10/bin/ && ./zkServer.sh start && cd - && jps

【master01启动HDFS集群:】

cd /software/ && start-dfs.sh && jps

【master01启动HBase:】

cd /software/hbase-1.2.6/bin && start-hbase.sh && jps

【master02上启动HBase:】

cd /software/hbase-1.2.6/bin && hbase-daemon.sh start master && jps

如有节点启动出现故障:
单独启动master:
cd /software/hbase-1.2.6/bin && hbase-daemon.sh start master && jps
单独启动regionserver:
cd /software/hbase-1.2.6/bin && hbase-daemon.sh start regionserver && jps
通过命令终端查看:
hbase(main):009:0> status
通过web终端查看:
http://master01的IP地址:16010/

【在主机CloudDeskTop导入java客户端开发所需jar包:】HBase1.2.6-All.zip

测试目标:

运用java代码编写程序操作HBase数据库,本次测试致力于对DDL语法的java客户端操作;

测试代码:大数据学习交流QQ群:217770236 让我们一起学习大数据

 package com.mmzs.bigdata.hbase.ddl;

 import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.TableNotFoundException;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable; /**
* DDL:
* createTable|disableTable|deleteTable|modifyTable|modifyColumn|getTableDescriptor
*
* 表空间——>表——>列族——>列(字段)——>列值(字段值)
*
* @author hadoop
*
*/
public class DDLMain {
/**
* 操作HBase集群的客户端
*/
private static Admin admin;//操作HBase集群的客户端 static{
//创建HBase配置
Configuration conf = HBaseConfiguration.create();
//要和hbase-site.xml中configuration配置的一样
conf.set("hbase.zookeeper.quorum", "slave01:2181,slave02:2181,slave03:2181");
//根据HBase配置获取集群路径
Connection conn;
try {
conn = ConnectionFactory.createConnection(conf);
admin = conn.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
} public static void main(String[] args) {
// createNamespace("mmzs02");
// try {
// createHTable("mmzs:myuser");
// } catch (IOException e) {
// e.printStackTrace();
// }
existHTable("mmzs:myuser");
// modifyHTableByAddFamily("mmzs:myuser","attach00");
// modifyHTableByDelFamily("mmzs:myuser","attach00");
// modifyHTableByModFamily("mmzs:myuser","base",5);
//deleteHTable("mmzs:myuser");
// iterateNameSpace(); } /**
* 创建表空间
* @param namespaceNameStr
* @throws IOException
*/
public static void createNamespace(String namespaceNameStr){
NamespaceDescriptor.Builder builder=NamespaceDescriptor.create(namespaceNameStr);
NamespaceDescriptor nd=builder.build();
try {
admin.createNamespace(nd);
} catch (IOException e) {
e.printStackTrace();
} } //删除表空间
public static void deleteNamespace(String namespaceNameStr){
try {
admin.deleteNamespace(namespaceNameStr);
} catch (IOException e) {
e.printStackTrace();
}
} //创建HBase表
public static void createHTable(String tabNameStr) throws IOException{
//转化为表名
TableName tabname=TableName.valueOf(tabNameStr);
//根据表名创建表结构
HTableDescriptor htd = new HTableDescriptor(tabname);
//定义列族名字“base”
HColumnDescriptor base = new HColumnDescriptor("base");
base.setMaxVersions(3);
// 定义列族名字为”extra“
HColumnDescriptor extra = new HColumnDescriptor("extra");
extra.setMaxVersions(3); htd.addFamily(base);
htd.addFamily(extra);
try {
admin.createTable(htd); } catch (IOException e) {
e.printStackTrace();
}
} /**
* 判断某表是否存在
* @param tabNameStr
*/
public static void existHTable(String tabNameStr){
TableName tabName=TableName.valueOf(tabNameStr);
Boolean flag = null;
try {
flag = admin.tableExists(tabName);
} catch (IOException e) {
e.printStackTrace();
}
if (flag) {
System.out.println(tabName+"存在");
} else {
System.out.println(tabName+"不存在");
}
} //增加一个列族
public static void modifyHTableByAddFamily(String tabNameStr,String familyName){
//转化为表名
TableName tabname=TableName.valueOf(tabNameStr);
try {
//根据表名得到表
HTableDescriptor htd = admin.getTableDescriptor(tabname);
//新建一个列族
HColumnDescriptor attach = new HColumnDescriptor(familyName);
attach.setMaxVersions(3);
//将新建的列族添加到表中
htd.addFamily(attach); //修改指定的表
admin.modifyTable(tabname, htd);
} catch (TableNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} //修改列族
public static void modifyHTableByModFamily(String tabNameStr,String familyName,Integer max){
//转化为表名
TableName tabname=TableName.valueOf(tabNameStr);
try {
//根据表名得到表
HTableDescriptor htd = admin.getTableDescriptor(tabname);
//得到指定的列族
HColumnDescriptor hcd =htd.getFamily(familyName.getBytes());
hcd.setMaxVersions(max); //修改表中的指定列族
admin.modifyColumn(tabname, hcd);
} catch (TableNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} //删除列族
public static void modifyHTableByDelFamily(String tabNameStr,String familyName){
TableName tabname=TableName.valueOf(tabNameStr);
try {
HTableDescriptor htd = admin.getTableDescriptor(tabname);
htd.removeFamily(familyName.getBytes()); //删除表中的指定列族
admin.modifyTable(tabname, htd);
} catch (TableNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} //遍历所有的表空间
public static void iterateNameSpace(){
try {
//取出所有的表空间的结构
NamespaceDescriptor[] nds=admin.listNamespaceDescriptors();
for(NamespaceDescriptor nd:nds){
//获取表空间的名字
String namespaceName=nd.getName();
//获取表空间下所有的表的结构
HTableDescriptor[] htds=admin.listTableDescriptorsByNamespace(namespaceName);
for(HTableDescriptor htd:htds){
//获取表的名字
String tabName=htd.getTableName().getNameAsString();
//获取表下的所有的列族的结构
HColumnDescriptor[] hcds=htd.getColumnFamilies();
for(HColumnDescriptor hcd:hcds){
//获取列族的名字
String familyName=hcd.getNameAsString();
//获取列族下的所有键值对
Map<ImmutableBytesWritable,ImmutableBytesWritable> keyVals=hcd.getValues();
//遍历当前列族下的所有键值对
Set<Entry<ImmutableBytesWritable, ImmutableBytesWritable>> entrys=keyVals.entrySet();
for(Entry<ImmutableBytesWritable, ImmutableBytesWritable> entry:entrys){
String key=new String(entry.getKey().get());
String value=new String(entry.getValue().get());
String info=new StringBuilder(namespaceName)
.append("\t")
.append(tabName)
.append("\t")
.append(familyName)
.append("\t")
.append(key)
.append(":")
.append(value)
.toString(); //输出结果
System.out.println(info);
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
} } //删除HBase表
public static void deleteHTable(String tabNameStr) {
TableName tableName = TableName.valueOf(tabNameStr); try {
//先禁用表
admin.disableTable(tableName);
//在删除表
admin.deleteTables(tabNameStr);
} catch (IOException e) {
e.printStackTrace();
}
} }

DDLMain

测试结果:

在命令端查看,查看方式,可参考:http://www.cnblogs.com/mmzs/p/8135327.html