测试准备
【首先同步时间:】
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包:】
测试目标:
运用java代码编写程序操作HBase数据库,本次测试致力于对DDL语法的java客户端操作;
:大数据学习交流QQ群: 让我们一起学习大数据
1 package com.mmzs.bigdata.hbase.ddl; 2 3 import java.io.IOException; 4 import java.util.Map; 5 import java.util.Map.Entry; 6 import java.util.Set; 7 8 import org.apache.hadoop.conf.Configuration; 9 import org.apache.hadoop.hbase.HBaseConfiguration; 10 import org.apache.hadoop.hbase.HColumnDescriptor; 11 import org.apache.hadoop.hbase.HTableDescriptor; 12 import org.apache.hadoop.hbase.NamespaceDescriptor; 13 import org.apache.hadoop.hbase.TableName; 14 import org.apache.hadoop.hbase.TableNotFoundException; 15 import org.apache.hadoop.hbase.client.Admin; 16 import org.apache.hadoop.hbase.client.Connection; 17 import org.apache.hadoop.hbase.client.ConnectionFactory; 18 import org.apache.hadoop.hbase.io.ImmutableBytesWritable; 19 20 /** 21 * DDL: 22 * createTable|disableTable|deleteTable|modifyTable|modifyColumn|getTableDescriptor 23 * 24 * 表空间——>表——>列族——>列(字段)——>列值(字段值) 25 * 26 * @author hadoop 27 * 28 */ 29 public class DDLMain { 30 /** 31 * 操作HBase集群的客户端 32 */ 33 private static Admin admin;//操作HBase集群的客户端 34 35 static{ 36 //创建HBase配置 37 Configuration conf = HBaseConfiguration.create(); 38 //要和hbase-site.xml中configuration配置的一样 39 conf.set("hbase.zookeeper.quorum", "slave01:2181,slave02:2181,slave03:2181"); 40 //根据HBase配置获取集群路径 41 Connection conn; 42 try { 43 conn = ConnectionFactory.createConnection(conf); 44 admin = conn.getAdmin(); 45 } catch (IOException e) { 46 e.printStackTrace(); 47 } 48 } 49 50 public static void main(String[] args) { 51 // createNamespace("mmzs02"); 52 // try { 53 // createHTable("mmzs:myuser"); 54 // } catch (IOException e) { 55 // e.printStackTrace(); 56 // } 57 existHTable("mmzs:myuser"); 58 // modifyHTableByAddFamily("mmzs:myuser","attach00"); 59 // modifyHTableByDelFamily("mmzs:myuser","attach00"); 60 // modifyHTableByModFamily("mmzs:myuser","base",5); 61 //deleteHTable("mmzs:myuser"); 62 // iterateNameSpace(); 63 64 65 66 } 67 68 /** 69 * 创建表空间 70 * @param namespaceNameStr 71 * @throws IOException 72 */ 73 public static void createNamespace(String namespaceNameStr){ 74 NamespaceDescriptor.Builder builder=NamespaceDescriptor.create(namespaceNameStr); 75 NamespaceDescriptor nd=builder.build(); 76 try { 77 admin.createNamespace(nd); 78 } catch (IOException e) { 79 e.printStackTrace(); 80 } 81 82 } 83 84 //删除表空间 85 public static void deleteNamespace(String namespaceNameStr){ 86 try { 87 admin.deleteNamespace(namespaceNameStr); 88 } catch (IOException e) { 89 e.printStackTrace(); 90 } 91 } 92 93 94 //创建HBase表 95 public static void createHTable(String tabNameStr) throws IOException{ 96 //转化为表名 97 TableName tabname=TableName.valueOf(tabNameStr); 98 //根据表名创建表结构 99 HTableDescriptor htd = new HTableDescriptor(tabname);100 //定义列族名字“base”101 HColumnDescriptor base = new HColumnDescriptor("base");102 base.setMaxVersions(3);103 // 定义列族名字为”extra“104 HColumnDescriptor extra = new HColumnDescriptor("extra");105 extra.setMaxVersions(3);106 107 htd.addFamily(base);108 htd.addFamily(extra);109 try {110 admin.createTable(htd);111 112 } catch (IOException e) {113 e.printStackTrace();114 }115 }116 117 /**118 * 判断某表是否存在119 * @param tabNameStr120 */121 public static void existHTable(String tabNameStr){122 TableName tabName=TableName.valueOf(tabNameStr);123 Boolean flag = null;124 try {125 flag = admin.tableExists(tabName);126 } catch (IOException e) {127 e.printStackTrace();128 }129 if (flag) {130 System.out.println(tabName+"存在");131 } else {132 System.out.println(tabName+"不存在");133 }134 }135 136 //增加一个列族137 public static void modifyHTableByAddFamily(String tabNameStr,String familyName){138 //转化为表名139 TableName tabname=TableName.valueOf(tabNameStr);140 try {141 //根据表名得到表142 HTableDescriptor htd = admin.getTableDescriptor(tabname);143 //新建一个列族144 HColumnDescriptor attach = new HColumnDescriptor(familyName);145 attach.setMaxVersions(3);146 //将新建的列族添加到表中147 htd.addFamily(attach);148 149 //修改指定的表150 admin.modifyTable(tabname, htd);151 } catch (TableNotFoundException e) {152 e.printStackTrace();153 } catch (IOException e) {154 e.printStackTrace();155 }156 }157 158 //修改列族159 public static void modifyHTableByModFamily(String tabNameStr,String familyName,Integer max){160 //转化为表名161 TableName tabname=TableName.valueOf(tabNameStr);162 try {163 //根据表名得到表164 HTableDescriptor htd = admin.getTableDescriptor(tabname);165 //得到指定的列族166 HColumnDescriptor hcd =htd.getFamily(familyName.getBytes());167 hcd.setMaxVersions(max);168 169 //修改表中的指定列族170 admin.modifyColumn(tabname, hcd);171 } catch (TableNotFoundException e) {172 e.printStackTrace();173 } catch (IOException e) {174 e.printStackTrace();175 }176 }177 178 //删除列族179 public static void modifyHTableByDelFamily(String tabNameStr,String familyName){180 TableName tabname=TableName.valueOf(tabNameStr);181 try {182 HTableDescriptor htd = admin.getTableDescriptor(tabname);183 htd.removeFamily(familyName.getBytes());184 185 //删除表中的指定列族186 admin.modifyTable(tabname, htd);187 } catch (TableNotFoundException e) {188 e.printStackTrace();189 } catch (IOException e) {190 e.printStackTrace();191 }192 }193 194 195 //遍历所有的表空间196 public static void iterateNameSpace(){197 try {198 //取出所有的表空间的结构199 NamespaceDescriptor[] nds=admin.listNamespaceDescriptors();200 for(NamespaceDescriptor nd:nds){201 //获取表空间的名字202 String namespaceName=nd.getName();203 //获取表空间下所有的表的结构204 HTableDescriptor[] htds=admin.listTableDescriptorsByNamespace(namespaceName);205 for(HTableDescriptor htd:htds){206 //获取表的名字207 String tabName=htd.getTableName().getNameAsString();208 //获取表下的所有的列族的结构209 HColumnDescriptor[] hcds=htd.getColumnFamilies();210 for(HColumnDescriptor hcd:hcds){211 //获取列族的名字212 String familyName=hcd.getNameAsString();213 //获取列族下的所有键值对214 MapkeyVals=hcd.getValues();215 //遍历当前列族下的所有键值对216 Set > entrys=keyVals.entrySet();217 for(Entry entry:entrys){218 String key=new String(entry.getKey().get());219 String value=new String(entry.getValue().get());220 String info=new StringBuilder(namespaceName)221 .append("\t")222 .append(tabName)223 .append("\t")224 .append(familyName)225 .append("\t")226 .append(key)227 .append(":")228 .append(value)229 .toString();230 231 //输出结果232 System.out.println(info);233 }234 }235 }236 }237 } catch (IOException e) {238 e.printStackTrace();239 }240 241 }242 243 //删除HBase表244 public static void deleteHTable(String tabNameStr) { 245 TableName tableName = TableName.valueOf(tabNameStr);246 247 try {248 //先禁用表249 admin.disableTable(tableName);250 //在删除表251 admin.deleteTables(tabNameStr);252 } catch (IOException e) {253 e.printStackTrace();254 }255 }256 257 }
测试结果:
在命令端查看,查看方式,可参考: