蜘蛛侠java_Hbase用java基础操作

1 packageHbase;2

3 importjava.io.IOException;4 importjava.util.Iterator;5 importjava.util.List;6

7 importorg.apache.hadoop.conf.Configuration;8 importorg.apache.hadoop.hbase.HBaseConfiguration;9 importorg.apache.hadoop.hbase.HColumnDescriptor;10 importorg.apache.hadoop.hbase.HTableDescriptor;11 importorg.apache.hadoop.hbase.KeyValue;12 importorg.apache.hadoop.hbase.client.Delete;13 importorg.apache.hadoop.hbase.client.Get;14 importorg.apache.hadoop.hbase.client.HBaseAdmin;15 importorg.apache.hadoop.hbase.client.HTable;16 importorg.apache.hadoop.hbase.client.Put;17 importorg.apache.hadoop.hbase.client.Result;18 importorg.apache.hadoop.hbase.client.ResultScanner;19 importorg.apache.hadoop.hbase.client.Scan;20 importorg.apache.hadoop.hbase.util.Bytes;21

22 public classHbaseZH {23 //声明静态配置

24 public staticConfiguration conf;25 static{26 conf =HBaseConfiguration.create();27 conf.set("hbase.zookeeper.property.clientPort", "2181");28 conf.set("hbase.zookeeper.quorum", "192.168.121.132");29 conf.set("hbase.master", "192.168.121.132:60000");30 }31 /*

32 * 创建表33 * @tableName 表名34 * @family 列族列表35 */

36 public static void creatTable(String tableName, String[] family) throwsException {37 HBaseAdmin admin = newHBaseAdmin(conf);38 HTableDescriptor desc = newHTableDescriptor(tableName);39 for (int i = 0; i < family.length; i++) {40 desc.addFamily(newHColumnDescriptor(family[i]));41 }42 if(admin.tableExists(tableName)) {43 System.out.println("table Exists!");44 System.exit(0);45 } else{46 admin.createTable(desc);47 System.out.println("create table Success!");48 }49 }50 /*

51 * 为表添加数据(适合知道有多少列族的固定表)52 * @rowKey rowKey53 * @tableName 表名54 * @column1 第一个列族列表55 * @value1 第一个列的值的列表56 * @column2 第二个列族列表57 * @value2 第二个列的值的列表58 */

59 public static void addData(String tableName, String rowKey,String[] column1, String[] value1) throwsIOException {60 Put put = new Put(Bytes.toBytes(rowKey));//设置rowkey

61 HTable table = new HTable(conf, tableName);//获取表

62 HColumnDescriptor[] columnFamilies = table.getTableDescriptor().getColumnFamilies(); //获取所有的列族

63 for (int i = 0; i < columnFamilies.length; i++) {64 String familyName = columnFamilies[i].getNameAsString(); //获取列族名

65 if (familyName.equals("d")) { //article列族put数据

66 for (int j = 0; j < column1.length; j++) {67 put.add(Bytes.toBytes(familyName),Bytes.toBytes(column1[j]), Bytes.toBytes(value1[j]));68 }69 }70 }71 table.put(put);72 System.out.println("add data Success!");73 }74 /*

75 * 根据rwokey查询76 * @rowKey rowKey77 * @tableName 表名78 */

79 public static Result getResult(String tableName, String rowKey) throwsIOException {80 Get get = newGet(Bytes.toBytes(rowKey));81 HTable table = new HTable(conf, tableName);//获取表

82 Result result =table.get(get);83 for(KeyValue kv : result.list()) {84 System.out.println("family:" +Bytes.toString(kv.getFamily()));85 System.out.println("qualifier:" +Bytes.toString(kv.getQualifier()));86 System.out.println("value:" +Bytes.toString(kv.getValue()));87 System.out.println("Timestamp:" +kv.getTimestamp());88 System.out.println("-------------------------------------------");89 }90 returnresult;91 }92

93 /*

94 * 遍历查询hbase表95 * @tableName 表名96 */

97 public static void getResultScann(String tableName) throwsIOException {98 Scan scan = newScan();99 ResultScanner rs = null;100 HTable table = newHTable(conf, tableName);101 try{102 rs =table.getScanner(scan);103 for(Result r : rs) {104 for(KeyValue kv : r.list()) {105 System.out.println("family:" +Bytes.toString(kv.getFamily()));106 System.out.println("qualifier:" +Bytes.toString(kv.getQualifier()));107 System.out.println("value:" +Bytes.toString(kv.getValue()));108 System.out.println("timestamp:" +kv.getTimestamp());109 System.out.println("-------------------------------------------");110 }111 }112 } finally{113 rs.close();114 }115 }116

117 /*

118 * 查询表中的某一列119 * @tableName 表名120 * @rowKey rowKey121 */

122 public static void getResultByColumn(String tableName, String rowKey,String familyName, String columnName) throwsIOException {123 HTable table = newHTable(conf, tableName);124 Get get = newGet(Bytes.toBytes(rowKey));125 get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName)); //获取指定列族和列修饰符对应的列

126 Result result =table.get(get);127 for(KeyValue kv : result.list()) {128 System.out.println("family:" +Bytes.toString(kv.getFamily()));129 System.out.println("qualifier:" +Bytes.toString(kv.getQualifier()));130 System.out.println("value:" +Bytes.toString(kv.getValue()));131 System.out.println("Timestamp:" +kv.getTimestamp());132 System.out.println("-------------------------------------------");133 }134 }135 /*

136 * 更新表中的某一列137 * @tableName 表名138 * @rowKey rowKey139 * @familyName 列族名140 * @columnName 列名141 * @value 更新后的值142 */

143 public static voidupdateTable(String tableName, String rowKey,String familyName, String columnName, String value)144 throwsIOException {145 HTable table = newHTable(conf, tableName);146 Put put = newPut(Bytes.toBytes(rowKey));147 put.add(Bytes.toBytes(familyName), Bytes.toBytes(columnName),Bytes.toBytes(value));148 table.put(put);149 System.out.println("update table Success!");150 }151 /*

152 * 查询某列数据的多个版本153 * @tableName 表名154 * @rowKey rowKey155 * @familyName 列族名156 * @columnName 列名157 */

158 public static void getResultByVersion(String tableName, String rowKey,String familyName, String columnName) throwsIOException {159 HTable table = newHTable(conf, tableName);160 Get get = newGet(Bytes.toBytes(rowKey));161 get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName));162 get.setMaxVersions(5);163 Result result =table.get(get);164 for(KeyValue kv : result.list()) {165 System.out.println("family:" +Bytes.toString(kv.getFamily()));166 System.out.println("qualifier:" +Bytes.toString(kv.getQualifier()));167 System.out.println("value:" +Bytes.toString(kv.getValue()));168 System.out.println("Timestamp:" +kv.getTimestamp());169 System.out.println("-------------------------------------------");170 }171 List> results = table.get(get).list(); Iterator> it = results.iterator(); while(it.hasNext()) {172 System.out.println(it.next().toString()); }173 }174 /*

175 * 删除指定的列176 * @tableName 表名177 * @rowKey rowKey178 * @familyName 列族名179 * @columnName 列名180 */

181 public static void deleteColumn(String tableName, String rowKey,String falilyName, String columnName) throwsIOException {182 HTable table = newHTable(conf, tableName);183 Delete deleteColumn = newDelete(Bytes.toBytes(rowKey));184 deleteColumn.deleteColumns(Bytes.toBytes(falilyName),Bytes.toBytes(columnName));185 table.delete(deleteColumn);186 System.out.println(falilyName + ":" + columnName + "is deleted!");187 }188 /*

189 * 删除指定的列190 * @tableName 表名191 * @rowKey rowKey192 */

193 public static void deleteAllColumn(String tableName, String rowKey) throwsIOException {194 HTable table = newHTable(conf, tableName);195 Delete deleteAll = newDelete(Bytes.toBytes(rowKey));196 table.delete(deleteAll);197 System.out.println("all columns are deleted!");198 }199 /*

200 * 删除表201 * @tableName 表名202 */

203 public static void deleteTable(String tableName) throwsIOException {204 HBaseAdmin admin = newHBaseAdmin(conf);205 admin.disableTable(tableName);206 admin.deleteTable(tableName);207 System.out.println(tableName + "is deleted!");208 }209 public static void main(String[] args) throwsException {210 //创建表

211 String tableName = "ecpcibimbqm:quote"; String[] family = { "d","dd"};212 creatTable(tableName,family);213 //为表添加数据

214 String[] column1 = { "permid", "object", "subject"};215 String[] value1 = {"Head First HBase",216 "HBase is the Hadoop database. Use it when you need random, realtime read/write access to your Big Data.",217 "Hadoop,HBase,NoSQL"};218 String[] column2 = { "time", "data"};219 String[] value2 = { "nicholas", "lee"};220 addData(tableName, "Relationship", column1, value1);221 addData(tableName, "Admainstatus", column2, value2);222 //删除一列

223 deleteColumn(tableName, "Relationship", "d", "permid");224 //删除所有列

225 deleteAllColumn(tableName, "Admainstatus");226 //删除表

227 deleteTable(tableName);228 //查询

229 getResult(tableName, "Relationship");230 //查询某一列的值

231 getResultByColumn(tableName, "Relationship", "d", "object");232 updateTable(tableName, "Relationship", "d", "object","update");233 //遍历查询

234 getResultScann(tableName);235 //查询某列的多版本

236 getResultByVersion(tableName, "Relationship", "d", "subject");237 }238 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值