博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hbase用java基础操作
阅读量:4941 次
发布时间:2019-06-11

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

HbaseCreate--建表

1 package Hbase; 2  3 import java.io.IOException; 4  5  6 import org.apache.hadoop.conf.Configuration; 7 import org.apache.hadoop.hbase.HBaseConfiguration; 8 import org.apache.hadoop.hbase.HColumnDescriptor; 9 import org.apache.hadoop.hbase.HTableDescriptor;10 11 import org.apache.hadoop.hbase.MasterNotRunningException;12 import org.apache.hadoop.hbase.ZooKeeperConnectionException;13 import org.apache.hadoop.hbase.client.HBaseAdmin;14 15 public class HbaseCreate {16 17     public static Configuration configuration;18     static {19         configuration = HBaseConfiguration.create();20         configuration.set("hbase.zookeeper.property.clientPort", "2181");21         configuration.set("hbase.zookeeper.quorum", "192.168.121.132");22         configuration.set("hbase.master", "192.168.121.132:60000");23     }24 25     public static void main(String[] args) {26          createTable("ecpcibimbqm:instrument");27 }28     public static void createTable(String tableName) {29         System.out.println("start create table ......");30         try {31             HBaseAdmin hBaseAdmin = new HBaseAdmin(configuration);32             if (hBaseAdmin.tableExists(tableName)) {33                 hBaseAdmin.disableTable(tableName);34                 hBaseAdmin.deleteTable(tableName);35                 System.out.println(tableName + " is exist,detele....");36             }37             HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);38             tableDescriptor.addFamily(new HColumnDescriptor("d"));39             tableDescriptor.addFamily(new HColumnDescriptor("i"));40             hBaseAdmin.createTable(tableDescriptor);41         } catch (MasterNotRunningException e) {42             e.printStackTrace();43         } catch (ZooKeeperConnectionException e) {44             e.printStackTrace();45         } catch (IOException e) {46             e.printStackTrace();47         }48         System.out.println("end create table ......");49     }50 }

HbaseInsert--插入

1 package Hbase; 2  3 import java.io.IOException; 4  5  6 import org.apache.hadoop.conf.Configuration; 7 import org.apache.hadoop.hbase.HBaseConfiguration; 8  9 import org.apache.hadoop.hbase.client.HTable;10 import org.apache.hadoop.hbase.client.HTablePool;11 import org.apache.hadoop.hbase.client.Put;12 13 14 public class HbaseInsert {15 16     public static Configuration configuration;17     static {18         configuration = HBaseConfiguration.create();19         configuration.set("hbase.zookeeper.property.clientPort", "2181");20         configuration.set("hbase.zookeeper.quorum", "192.168.121.132");21         configuration.set("hbase.master", "192.168.121.132:60000");22     }23 24     public static void main(String[] args) throws IOException {25          insertData("ecpcibimbqm:instrument");26 }27      public static void insertData(String tableName) throws IOException {  28         System.out.println("start insert data ......");  29         //HTablePool pool = new HTablePool(configuration, 1000);  30         HTable table = new HTable(configuration, tableName);  31         Put put = new Put("Relationship".getBytes());32         put.add("d".getBytes(), "effectfrom".getBytes(), "2017".getBytes());33         //put.add("d:effectto".getBytes(), null, "2019".getBytes()); 34         try {  35             table.put(put);  36         } catch (IOException e) {  37             e.printStackTrace();  38         }  39         System.out.println("end insert data ......");  40     }  41 }

HbaseCI-建表加插入

1 package Hbase;  2   3 import java.io.IOException;  4   5 import org.apache.hadoop.conf.Configuration;  6 import org.apache.hadoop.hbase.HBaseConfiguration;  7 import org.apache.hadoop.hbase.HColumnDescriptor;  8 import org.apache.hadoop.hbase.HTableDescriptor;  9 import org.apache.hadoop.hbase.client.HBaseAdmin; 10 import org.apache.hadoop.hbase.client.HTable; 11 import org.apache.hadoop.hbase.client.Put; 12 import org.apache.hadoop.hbase.util.Bytes; 13  14 public class HbaseCI { 15     // 声明静态配置 16     public static Configuration configuration; 17     static { 18         configuration = HBaseConfiguration.create(); 19         configuration.set("hbase.zookeeper.property.clientPort", "2181"); 20         configuration.set("hbase.zookeeper.quorum", "192.168.121.132"); 21         configuration.set("hbase.master", "192.168.121.132:60000"); 22     } 23     /* 24      * 创建表 25      *  26      * @tableName 表名 27      *  28      * @family 列族列表 29      */ 30     public static void creatTable(String tableName, String[] family) throws Exception { 31         HBaseAdmin admin = new HBaseAdmin(configuration); 32         HTableDescriptor desc = new HTableDescriptor(tableName); 33         for (int i = 0; i < family.length; i++) { 34             desc.addFamily(new HColumnDescriptor(family[i])); 35         } 36         if (admin.tableExists(tableName)) { 37             System.out.println("table Exists!"); 38             System.exit(0); 39         } else { 40             admin.createTable(desc); 41             System.out.println("create table Success!"); 42         } 43     } 44  45     /* 46      * 为表添加数据(适合知道有多少列族的固定表) 47      *  48      * @rowKey rowKey 49      *  50      * @tableName 表名 51      *  52      * @column 第一个列族列表 53      *  54      * @value 第一个列的值的列表 55      *  56      * @column2 第二个列族列表 57      *  58      * @value2 第二个列的值的列表 59      */ 60     public static void addData(String tableName, String rowKey,String[] column, String[] value) 61             throws IOException { 62         Put put = new Put(Bytes.toBytes(rowKey));// 设置rowkey 63         HTable table = new HTable(configuration, Bytes.toBytes(tableName));// HTabel负责跟记录相关的操作如增删改查等// 64                                                                     // 获取表 65         HColumnDescriptor[] columnFamilies = table.getTableDescriptor().getColumnFamilies(); // 获取所有的列族 66         for (int i = 0; i < columnFamilies.length; i++) { 67             String familyName = columnFamilies[i].getNameAsString(); // 获取列族名 68             if (familyName.equals("d")) { // article列族put数据 69                 for (int j = 0; j < column.length; j++) { 70                     put.add(Bytes.toBytes(familyName), 71                             Bytes.toBytes(column[j]), Bytes.toBytes(value[j])); 72                 } 73             } 74         } 75         table.put(put); 76         System.out.println("add data Success!"); 77     } 78     public static void main(String[] args) throws Exception { 79  80         // 创建表 81         String tableName = "ecpcibimbqm:quote"; 82         String[] family = { "d" }; 83         creatTable(tableName, family); 84  85         // 为表添加数据 86  87         String[] column = { "permid", "subject"}; 88         String[] value = { 89                 "Head First HBase", 90                 "HBase is the Hadoop database. Use it when you need random, realtime read/write access to your Big Data." 91                 }; 92         String[] column1 = { "uuid", "objectject"}; 93         String[] value1 = { 94                 "212312312312312", 95                 "aaaaaaaaaaaaa" 96                 }; 97         addData("ecpcibimbqm:quote", "Relationship", column, value); 98         addData("ecpcibimbqm:quote", "Adminstatus", column1, value1); 99 100     }101 }

HbaseScan--查看整个表

1 package Hbase; 2  3 import java.io.IOException; 4  5  6 import org.apache.hadoop.conf.Configuration; 7 import org.apache.hadoop.hbase.HBaseConfiguration; 8  9 import org.apache.hadoop.hbase.KeyValue;10 11 import org.apache.hadoop.hbase.client.Get;12 13 import org.apache.hadoop.hbase.client.HTable;14 15 import org.apache.hadoop.hbase.client.Result;16 17 import org.apache.hadoop.hbase.util.Bytes;18 19 public class HbaseScan {20     public static Configuration configuration;21     static {22         configuration = HBaseConfiguration.create();23         configuration.set("hbase.zookeeper.property.clientPort", "2181");24         configuration.set("hbase.zookeeper.quorum", "192.168.121.132");25         configuration.set("hbase.master", "192.168.121.132:60000");26     }27  public static void selectByRowKey(String tablename,String rowKey) throws IOException{28         HTable table=new HTable(configuration,tablename);29         Get g = new Get(Bytes.toBytes(rowKey));30         Result r=table.get(g);31         for(KeyValue kv:r.raw()){32             System.out.println("family: "+ Bytes.toString(kv.getFamily()));33             System.out.println("column:"+ Bytes.toString(kv.getQualifier()));34             System.out.println("value:"+ Bytes.toString(kv.getValue()));35         }36     }37 38 public static void main(String[] args) throws Exception {39 40         selectByRowKey("ecpcibimbqm:quote","Relationship");41 42 }43 }

HbaseZH--hbase综合使用常用API

1 package Hbase;  2   3 import java.io.IOException;    4 import java.util.Iterator;    5 import java.util.List;    6     7 import org.apache.hadoop.conf.Configuration;    8 import org.apache.hadoop.hbase.HBaseConfiguration;    9 import org.apache.hadoop.hbase.HColumnDescriptor;   10 import org.apache.hadoop.hbase.HTableDescriptor;   11 import org.apache.hadoop.hbase.KeyValue;   12 import org.apache.hadoop.hbase.client.Delete;   13 import org.apache.hadoop.hbase.client.Get;   14 import org.apache.hadoop.hbase.client.HBaseAdmin;   15 import org.apache.hadoop.hbase.client.HTable;   16 import org.apache.hadoop.hbase.client.Put;   17 import org.apache.hadoop.hbase.client.Result;   18 import org.apache.hadoop.hbase.client.ResultScanner;   19 import org.apache.hadoop.hbase.client.Scan;   20 import org.apache.hadoop.hbase.util.Bytes;   21    22 public class HbaseZH {   23     // 声明静态配置   24     public static Configuration 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) throws Exception {   37         HBaseAdmin admin = new HBaseAdmin(conf);   38         HTableDescriptor desc = new HTableDescriptor(tableName);   39         for (int i = 0; i < family.length; i++) {   40             desc.addFamily(new HColumnDescriptor(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 rowKey  53      * @tableName 表名  54      * @column1 第一个列族列表  55      * @value1 第一个列的值的列表  56      * @column2 第二个列族列表  57      * @value2 第二个列的值的列表  58      */   59     public static void addData(String tableName, String rowKey,String[] column1, String[] value1) throws IOException {   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 rowKey  77      * @tableName 表名  78      */   79     public static Result getResult(String tableName, String rowKey) throws IOException {   80         Get get = new Get(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         return result;   91     }   92    93     /*  94      * 遍历查询hbase表  95      * @tableName 表名  96      */   97     public static void getResultScann(String tableName) throws IOException {   98         Scan scan = new Scan();   99         ResultScanner rs = null;  100         HTable table = new HTable(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 rowKey 121      */  122     public static void getResultByColumn(String tableName, String rowKey,String familyName, String columnName) throws IOException {  123         HTable table = new HTable(conf, tableName);  124         Get get = new Get(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 rowKey 139      * @familyName 列族名 140      * @columnName 列名 141      * @value 更新后的值 142      */  143     public static void updateTable(String tableName, String rowKey,String familyName, String columnName, String value)  144             throws IOException {  145         HTable table = new HTable(conf, tableName);  146         Put put = new Put(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 rowKey 155      * @familyName 列族名 156      * @columnName 列名 157      */  158     public static void getResultByVersion(String tableName, String rowKey,String familyName, String columnName) throws IOException {  159         HTable table = new HTable(conf, tableName);  160         Get get = new Get(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 rowKey 178 * @familyName 列族名 179 * @columnName 列名 180 */ 181 public static void deleteColumn(String tableName, String rowKey,String falilyName, String columnName) throws IOException { 182 HTable table = new HTable(conf, tableName); 183 Delete deleteColumn = new Delete(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 rowKey 192 */ 193 public static void deleteAllColumn(String tableName, String rowKey) throws IOException { 194 HTable table = new HTable(conf, tableName); 195 Delete deleteAll = new Delete(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) throws IOException { 204 HBaseAdmin admin = new HBaseAdmin(conf); 205 admin.disableTable(tableName); 206 admin.deleteTable(tableName); 207 System.out.println(tableName + "is deleted!"); 208 } 209 public static void main(String[] args) throws Exception { 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 }

hbase执行过程不执行MapReduce,直接java编译就可以

/usr/local/jdk1.7.0_25/bin/javac Hbase.java

/usr/local/jdk1.7.0_25/bin/java Hbase

转载于:https://www.cnblogs.com/jieran/p/9163307.html

你可能感兴趣的文章
C语言实现windows进程遍历
查看>>
X86逆向实战7:使用万能断点通杀
查看>>
java面试_数据库
查看>>
笨鸟都没有先飞怎么办。。。
查看>>
文字超过宽度显示省略号(js版)
查看>>
springmvc的作用:
查看>>
eclipse plugins
查看>>
760A 水
查看>>
VMware VCSA 6.0安装过程 (转)
查看>>
git使用stash存储相关操作
查看>>
《Hadoop权威指南》读书笔记1
查看>>
leetCode-数组:Remove Duplicates from Sorted Array
查看>>
LoadRunner截取字符串操作
查看>>
loadrunner解决在项目中的难点解决
查看>>
模块循环引用
查看>>
Python-----包和日志的使用
查看>>
聊聊、Spring 第二篇
查看>>
Java求三个数中的最大值
查看>>
PHP 7 的五大新特性
查看>>
暴力推荐2:硬盘分区丢失之DiskGenius
查看>>