博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HBase操作一
阅读量:7307 次
发布时间:2019-06-30

本文共 5950 字,大约阅读时间需要 19 分钟。

1 package Hbase;  2   3 import java.io.IOException;  4 import org.apache.hadoop.conf.Configuration;  5 import org.apache.hadoop.hbase.Cell;  6 import org.apache.hadoop.hbase.CellUtil;  7 import org.apache.hadoop.hbase.HBaseConfiguration;  8 import org.apache.hadoop.hbase.client.Delete;  9 import org.apache.hadoop.hbase.client.Get; 10 import org.apache.hadoop.hbase.client.HTable; 11 import org.apache.hadoop.hbase.client.Put; 12 import org.apache.hadoop.hbase.client.Result; 13 import org.apache.hadoop.hbase.client.ResultScanner; 14 import org.apache.hadoop.hbase.client.Scan; 15 import org.apache.hadoop.hbase.util.Bytes; 16 import org.apache.hadoop.hdfs.protocol.proto.ClientNamenodeProtocolProtos.GetDataEncryptionKeyRequestProto; 17 import org.apache.hadoop.io.IOUtils; 18  19 public class HbaseOperation { 20  21     public static HTable getHTableByTableName(String tableName) throws IOException { 22         // get instance of default configuration 23         Configuration configuration = HBaseConfiguration.create(); 24         // get table instance 25         HTable table = new HTable(configuration, tableName); 26          27         return table; 28     } 29      30     public static void getData(String tableName) throws IOException{ 31         //String tableName = "user"; 32         HTable table = getHTableByTableName(tableName); 33         //create get with rowkey 34         Get rowkey = new Get(Bytes.toBytes("10001")); 35         //**************************************************** 36         //add column 37         rowkey.addColumn(// 38                 Bytes.toBytes("info"),// 39                 Bytes.toBytes("name") 40                 ); 41          42         rowkey.addColumn(// 43                 Bytes.toBytes("info"),// 44                 Bytes.toBytes("age") 45                 ); 46          47         //get data 48         Result result = table.get(rowkey); 49         //key : rewkey + cf +c +version 50         //value :value 51         for(Cell cell:result.rawCells()){ 52             System.out.println(// 53                Bytes.toString(CellUtil.cloneFamily(cell)) + ":" // 54                + Bytes.toString(CellUtil.cloneQualifier(cell)) + "-->" // 55                + Bytes.toString(CellUtil.cloneValue(cell)) 56             ); 57         } 58         //table close 59         table.close(); 60  61     } 62      63     public static void putData(String tableName) throws IOException { 64         //String tableName = "user"; 65         HTable table = getHTableByTableName(tableName); 66          67         Put put = new Put(Bytes.toBytes("10003")); 68         //add a column with value 69         put.add(// 70             Bytes.toBytes("info"),// 71             Bytes.toBytes("name"),// 72             Bytes.toBytes("wangwu")// 73             ); 74          75         put.add(// 76                 Bytes.toBytes("info"),// 77                 Bytes.toBytes("age"),// 78                 Bytes.toBytes("26")// 79                 ); 80          81         put.add(// 82                 Bytes.toBytes("info"),// 83                 Bytes.toBytes("address"),// 84                 Bytes.toBytes("tianjing")// 85                 ); 86          87         table.put(put); 88          89         table.close(); 90     } 91      92     public static void deleteData(String tableName) throws IOException { 93         HTable table = getHTableByTableName(tableName); 94         Delete delete = new Delete(Bytes.toBytes("10003")); 95         //delete a certain column  96 //        delete.deleteColumn(Bytes.toBytes("info"), // 97 //                Bytes.toBytes("address")); 98          99         //delete a familycolumn100                 delete.deleteFamily(Bytes.toBytes("info"));101                 102         table.delete(delete);103         table.close();104     }105 106     public static void main(String[] args) throws IOException {107         String tableName = "user";108 //        HTable table = getHTableByTableName(tableName);109 //        getData(tableName);110 //        putData(tableName);111 //        deleteData(tableName);112         113         HTable table = null;114         ResultScanner resultScanner = null;115         try{116             table = getHTableByTableName(tableName);117             118             Scan scan = new Scan();119             //the range of scan120             scan.setStartRow(Bytes.toBytes("10001"));121             scan.setStartRow(Bytes.toBytes("10004"));122             123             //scan the certain column or familycolumn124 //            scan.addColumn(family, qualifier);125 //            scan.addFamily(family);126             127             //another way to scan128             //Scan scan2 = new Scan(startRow, stopRow);129             130             //PrefixFilter131             //PageFilter132 //            scan.setFilter(filter);133             134 //            scan.setCacheBlocks(cacheBlocks);135 //            scan.setCaching(caching);136             137             resultScanner = table.getScanner(scan);138             for (Result result:resultScanner) {139                 System.out.println(Bytes.toString(result.getRow()));140                 //System.out.println(result);141                 142                 for(Cell cell:result.rawCells()){143                     System.out.println(//144                        Bytes.toString(CellUtil.cloneFamily(cell)) + ":" //145                        + Bytes.toString(CellUtil.cloneQualifier(cell)) + "-->" //146                        + Bytes.toString(CellUtil.cloneValue(cell))147                     );148                 }149                 System.out.println("------------------------------");150             }151         }catch(Exception e){152             e.printStackTrace();153         }finally{154             IOUtils.closeStream(resultScanner);155             IOUtils.closeStream(table);156         }157     }158 }

 

转载于:https://www.cnblogs.com/perfectdata/p/10136627.html

你可能感兴趣的文章
MySQL的字符集以及中文乱码问题
查看>>
Python条件控制语句与循环控制语句(四)
查看>>
thymeleaf模板的应用
查看>>
ADF_Starting系列3_使用ADF开发富Web应用程序之开发User Interface
查看>>
Curl命令简单使用
查看>>
MySQL数据库支持SSL连接
查看>>
Windows用mstsc(远程桌面)远程Ubuntu 12.04时无法显示Ubuntu桌面解决办法
查看>>
shell语言
查看>>
centos7下使用PlayOnLinux安装windows软件
查看>>
(2) mysqlbinlog 精确提取sql语句
查看>>
国内常用NTP服务器地址及IP
查看>>
python 使用pymssql连接sql server数据库
查看>>
消息通信库ZeroMQ 4.0.4安装指南
查看>>
mysql状态信息参数解析(show GLOBAL status)
查看>>
Android studio动态调试smali
查看>>
CentOS6.8 部署Tomcat+jenkins+git+maven+ant 持续集成
查看>>
4台VM安装CDH5大数据平台
查看>>
关于ORA-01031: insufficient privileges问题
查看>>
UGUI创建物体
查看>>
jemalloc 内存分配管理
查看>>