标签:
1.使用hbase shell操作
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.MasterNotRunningException; 10 import org.apache.hadoop.hbase.ZooKeeperConnectionException; 11 import org.apache.hadoop.hbase.client.Delete; 12 import org.apache.hadoop.hbase.client.Get; 13 import org.apache.hadoop.hbase.client.HBaseAdmin; 14 import org.apache.hadoop.hbase.client.HTable; 15 import org.apache.hadoop.hbase.client.Put; 16 import org.apache.hadoop.hbase.client.Result; 17 import org.apache.hadoop.hbase.client.ResultScanner; 18 import org.apache.hadoop.hbase.client.Scan; 19 import org.apache.hadoop.hbase.util.Bytes; 20 21 /** 22 * 需要先引入包 23 * hbase-0.94.7-security 24 * 和lib下的包 25 * 26 * HBaseAdmin //管理HBase的,负责DDL操作 27 * 表自身的操作HTable 管理表中数据,主要负责DML操作 28 * 29 * @author Administrator 30 */ 31 32 public class HbaseTest { 33 34 public static final String TABLE_NAME="t2"; 35 public static final String ROW_KEY="rk1"; 36 37 public static void main(String[] args) throws Exception { 38 39 40 //ddl(TABLE_NAME, "f1"); 41 //dml(); 42 43 scan(); 44 45 46 47 48 } 49 50 51 52 53 public static void scan() throws IOException { 54 Configuration conf = getConfiguration(); 55 HTable hTable = new HTable(conf, TABLE_NAME); 56 57 58 Scan scan=new Scan(); 59 ResultScanner scanner = hTable.getScanner(scan); 60 61 //指定列族和列 62 //ResultScanner scanner = hTable.getScanner(Bytes.toBytes("f1"), Bytes.toBytes("c1")); 63 64 65 for (Result result : scanner) { 66 67 System.out.println(result); 68 69 } 70 71 //关闭 72 hTable.close(); 73 } 74 75 76 77 78 79 80 public static void dml() throws IOException { 81 Configuration conf = getConfiguration(); 82 HTable hTable = new HTable(conf, TABLE_NAME); 83 84 //行键 85 Put put=new Put(Bytes.toBytes(ROW_KEY)); 86 //列族,列 87 put.add(Bytes.toBytes("f1"), Bytes.toBytes("c1"), Bytes.toBytes(1)); 88 //插入 89 hTable.put(put); 90 91 //读取 92 Get get=new Get(Bytes.toBytes(ROW_KEY)); 93 Result result = hTable.get(get); 94 95 int valuestring = Bytes.toInt(result.getValue(Bytes.toBytes("f1"), Bytes.toBytes("c1"))); 96 97 System.out.println("value的值为:"+valuestring); 98 System.out.println(result); 99 100 //删除 101 // Delete delete=new Delete(Bytes.toBytes(ROW_KEY)); 102 103 // hTable.delete(delete); 104 105 //关闭 106 hTable.close(); 107 } 108 109 110 111 public static void ddl(String tableName,String...familyNames) throws Exception{ 112 113 114 Configuration conf = getConfiguration(); 115 116 //连接成功 117 HBaseAdmin hBaseAdmin = new HBaseAdmin(conf); 118 119 System.out.println(hBaseAdmin); 120 //进行操作 121 122 123 //指定表的名称 124 HTableDescriptor desc = new HTableDescriptor(tableName); 125 126 //列族相关操作 127 for (String fname : familyNames) { 128 //列族 129 HColumnDescriptor familyDesc=new HColumnDescriptor(fname); 130 131 desc.addFamily(familyDesc); 132 133 } 134 135 if(!hBaseAdmin.tableExists(tableName)){ 136 //创建表 137 hBaseAdmin.createTable(desc); 138 } 139 140 141 //查看是否有该表,验证是否创建成功,查询会返回一个数组 142 System.out.println("表是否存在:"+hBaseAdmin.tableExists(tableName)); 143 144 145 //判断表是否停用 146 //hBaseAdmin.isTableDisabled(tableName); 147 148 //hBaseAdmin.disableTable(TABLE_NAME); 149 //hBaseAdmin.deleteTable(TABLE_NAME); 150 151 152 //关闭服务器的连接 153 hBaseAdmin.close(); 154 155 156 } 157 158 159 160 public static Configuration getConfiguration() { 161 //添加了Hbase的资源 162 Configuration conf = HBaseConfiguration.create(); 163 164 //指定zookeeper的配置及机器 165 conf.setStrings("hbase.zookeeper.quorum", "hadoopsys"); 166 return conf; 167 } 168 169 }
标签:
原文地址:http://www.cnblogs.com/thinkpad/p/4690768.html