Hbase表的管理
HBaseAdmin是一个类表示管理,通过这个类可执行管理角色。HBaseAdmin这个类属于org.apache.hadoop.hbase.client包。
- 创建HBaseAdim实例
- 获取表实例
- 创建列簇描述
- 向表描述中加载列簇
- 调用HBaseAdmin创建表
后面可以通过HBaseAdmin对象判断表是否存在。
可以new一个Put对象,将rowkey传进去,并用Put对象向表中添加列和value值。
1 package top.tianyz.hbase.api; 2 3 import org.apache.hadoop.conf.Configuration; 4 import org.apache.hadoop.hbase.HColumnDescriptor; 5 import org.apache.hadoop.hbase.HTableDescriptor; 6 import org.apache.hadoop.hbase.TableName; 7 import org.apache.hadoop.hbase.client.*; 8 import org.apache.hadoop.hbase.util.Bytes; 9 import org.junit.Before; 10 import org.junit.Test; 11 12 import java.io.BufferedReader; 13 import java.io.FileReader; 14 import java.io.IOException; 15 import java.util.ArrayList; 16 17 public class Demo03HbaseApi { 18 private HConnection connection; 19 private HBaseAdmin hBaseAdmin; 20 @Before 21 public void Before() { 22 Configuration configuration = new Configuration(); 23 24 configuration.set("hbase.zookeeper.quorum", "master:2181"); 25 26 try { 27 connection = HConnectionManager.createConnection(configuration); 28 hBaseAdmin = new HBaseAdmin(configuration); 29 30 System.out.println("zk连接建立成功!"); 31 } catch (IOException e) { 32 e.printStackTrace(); 33 } 34 } 35 36 //创建表 37 @Test 38 public void CreateTable() throws IOException { 39 40 //HTableDescriptor student = hBaseAdmin.getTableDescriptor(TableName.valueOf("student")); 41 HTableDescriptor student =new HTableDescriptor(TableName.valueOf("student")); 42 HColumnDescriptor info = new HColumnDescriptor("info"); 43 // info.setTimeToLive(10); 44 // info.setMaxVersions(10); 45 student.addFamily(info); 46 //创建表student 47 hBaseAdmin.createTable(student); 48 } 49 50 // 删除表 51 @Test 52 public void DropTable() { 53 try { 54 if (hBaseAdmin.tableExists("student")) { 55 hBaseAdmin.disableTable("student"); 56 hBaseAdmin.deleteTable("student"); 57 } 58 } catch (IOException e) { 59 e.printStackTrace(); 60 } 61 } 62 63 //添加一列 64 @Test 65 public void PutOne() throws IOException { 66 HTableInterface student = connection.getTable("student"); 67 68 Put put = new Put("001".getBytes()); 69 70 put.add("info".getBytes(), "name".getBytes(), "张三".getBytes()); 71 72 student.put(put); 73 } 74 // 增加多列 75 @Test 76 public void PutMore() throws IOException { 77 78 //批量插入数据 79 ArrayList<Put> puts = new ArrayList<Put>(); 80 81 try { 82 83 //获取表的实例 84 HTableInterface student = connection.getTable("student"); 85 86 87 BufferedReader bufferedReader = new BufferedReader(new FileReader("data/students.txt")); 88 89 String line; 90 while ((line = bufferedReader.readLine()) != null) { 91 92 String[] split = line.split(","); 93 94 String id = split[0]; 95 String name = split[1]; 96 Integer age = Integer.parseInt(split[2]); 97 String gender = split[3]; 98 String clazz = split[4]; 99 100 101 //构建put对象 一学号作为rowkey 102 Put put = new Put(id.getBytes()); 103 104 //增加列 105 put.add("info".getBytes(), "name".getBytes(), name.getBytes("utf-8")); 106 107 //Bytes.toBytes(age) 将基本数据类型转换成字节数组 108 put.add("info".getBytes(), "age".getBytes(), Bytes.toBytes(age)); 109 110 put.add("info".getBytes(), "gender".getBytes(), gender.getBytes()); 111 put.add("info".getBytes(), "clazz".getBytes(), clazz.getBytes()); 112 113 //将put放到集合中 114 puts.add(put); 115 116 } 117 118 119 //批量插入数据 120 student.put(puts); 121 122 } catch (Exception e) { 123 e.printStackTrace(); 124 } 125 } 126 127 @Test 128 public void get() { 129 try { 130 HTableInterface student = connection.getTable("student"); 131 132 //构建get对象 指定rowkey 133 Get get = new Get("1500100001".getBytes()); 134 135 //执行查询 返回 result 136 Result result = student.get(get); 137 138 //取出rowkey 139 String id = Bytes.toString(result.getRow()); 140 141 142 // 1、直接通过列名取值 如果列名不存在 返回对应类型的默认值 143 byte[] bytes = result.getValue("info".getBytes(), "name".getBytes()); 144 //通过String的构造函数 构建字符串 145 //String name = new String(bytes, "utf-8"); 146 147 //hbase 推荐方式 148 String name = Bytes.toString(bytes); 149 150 //int 类型 Bytes.toInt 转换 151 int age = Bytes.toInt(result.getValue("info".getBytes(), "age".getBytes())); 152 153 String gender = Bytes.toString(result.getValue("info".getBytes(), "gender".getBytes())); 154 String clazz = Bytes.toString(result.getValue("info".getBytes(), "clazz".getBytes())); 155 156 System.out.println(id + "\t" + name + "\t" + age + "\t" + gender + "\t" + clazz); 157 158 } catch (IOException e) { 159 e.printStackTrace(); 160 } 161 } 162 }
浙公网安备 33010602011771号