原帖地址: http://blog.csdn.net/nsrainbow/article/details/42649249 最新课程请关注原作者博客,获得更好的显示体验
4.0.0
org.crazycake
playhbase
0.0.1-SNAPSHOT
jar
playhbase
http://maven.apache.org
UTF-8
${basedir}/conf
false
hbase-site.xml
junit
junit
3.8.1
test
org.apache.hbase
hbase-client
0.98.4-hadoop2
maven-compiler-plugin
2.0.2
1.6
UTF-8
true
-nowarn
org.apache.maven.plugins
maven-shade-plugin
2.3
package
shade
hbase.cluster.distributed
true
hbase.zookeeper.quorum
host1,host2
hbase.zookeeper.property.clientPort
2181
package org.crazycake.playhbase;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;
public class CreateTable {
public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
Configuration config = HBaseConfiguration.create();
// 这边注释起来的是动态设定zookeeper参数的方法,如果你没有hbase-site.xml 或者想动态改变
// 可以采用动态方式设定
//
// config.set("hbase.zookeeper.quorum",
// "zookeepernode0,zookeepernode1,zookeepernode2");
//config.set("hbase.zookeeper.property.clientPort", "2181");
//config.set("hbase.cluster.distributed", "true");
// 使用配置文件创建一个 admin 对象
HBaseAdmin admin = new HBaseAdmin(config);
// 创建表
HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf("people"));
// 创建2个列簇
tableDescriptor.addFamily(new HColumnDescriptor("name"));
tableDescriptor.addFamily(new HColumnDescriptor("contactinfo"));
admin.createTable(tableDescriptor);
// 接下来搞点数据进去呗
String[][] people = {
{ "1", "Marcel", "Haddad", "[email protected]"},
{ "2", "Franklin", "Holtz", "[email protected]" },
{ "3", "Dwayne", "McKee", "[email protected]" },
{ "4", "Rae", "Schroeder", "[email protected]" },
{ "5", "Rosalie", "burton", "[email protected]"},
{ "6", "Gabriela", "Ingram", "[email protected]"} };
HTable table = new HTable(config, "people");
// 把这些数据插入到表里面
for (int i = 0; i< people.length; i++) {
//第一列做rowkey
Put person = new Put(Bytes.toBytes(people[i][0]));
//把 Marcel 放到 name 这个列簇的 first 这个字段去
person.add(Bytes.toBytes("name"), Bytes.toBytes("first"), Bytes.toBytes(people[i][1]));
person.add(Bytes.toBytes("name"), Bytes.toBytes("last"), Bytes.toBytes(people[i][2]));
person.add(Bytes.toBytes("contactinfo"), Bytes.toBytes("email"), Bytes.toBytes(people[i][3]));
table.put(person);
}
// 最后要记得提交和关闭表
table.flushCommits();
table.close();
}
}
tail -200f /var/log/hbase/hbase-hbase-master-host1.localdomain.log
hbase(main):003:0> scan 'people'
ROW COLUMN+CELL
1 column=contactinfo:email, timestamp=1421338694666, [email protected]
1 column=name:first, timestamp=1421338694666, value=Marcel
1 column=name:last, timestamp=1421338694666, value=Haddad
2 column=contactinfo:email, timestamp=1421338694932, [email protected]
2 column=name:first, timestamp=1421338694932, value=Franklin
2 column=name:last, timestamp=1421338694932, value=Holtz
3 column=contactinfo:email, timestamp=1421338694977, [email protected]
3 column=name:first, timestamp=1421338694977, value=Dwayne
3 column=name:last, timestamp=1421338694977, value=McKee
4 column=contactinfo:email, timestamp=1421338695034, [email protected]
4 column=name:first, timestamp=1421338695034, value=Rae
4 column=name:last, timestamp=1421338695034, value=Schroeder
5 column=contactinfo:email, timestamp=1421338695054, [email protected]
5 column=name:first, timestamp=1421338695054, value=Rosalie
5 column=name:last, timestamp=1421338695054, value=burton
6 column=contactinfo:email, timestamp=1421338695076, [email protected]
6 column=name:first, timestamp=1421338695076, value=Gabriela
6 column=name:last, timestamp=1421338695076, value=Ingram
6 row(s) in 0.3910 seconds
package org.crazycake.playhbase;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
/**
* 根据email 来搜索用户
* @author alexxiyang (https://github.com/alexxiyang)
*
*/
public class SearchByEmail {
public static void main(String[] args) throws IOException {
//创建配置
Configuration config = HBaseConfiguration.create();
// 打开表
HTable table = new HTable(config, "people");
//定义一系列要用到的列簇和列
// 定义列簇
byte[] contactFamily = Bytes.toBytes("contactinfo");
// 列
byte[] emailQualifier = Bytes.toBytes("email");
//列簇
byte[] nameFamily = Bytes.toBytes("name");
//列
byte[] firstNameQualifier = Bytes.toBytes("first");
byte[] lastNameQualifier = Bytes.toBytes("last");
// 创建一个正则表达式的比较器
RegexStringComparator emailFilter = new RegexStringComparator("[email protected]");
// 创建一个filter,把这个正则比较器传进去
SingleColumnValueFilter filter = new SingleColumnValueFilter(contactFamily, emailQualifier, CompareOp.EQUAL, emailFilter);
// 创建一个 scan对象
Scan scan = new Scan();
//把filter 传进去
scan.setFilter(filter);
// 开始查询,并获取结果
ResultScanner results = table.getScanner(scan);
// 遍历结果打印数据
for (Result result : results) {
String id = new String(result.getRow());
byte[] firstNameObj = result.getValue(nameFamily, firstNameQualifier);
String firstName = new String(firstNameObj);
byte[] lastNameObj = result.getValue(nameFamily, lastNameQualifier);
String lastName = new String(lastNameObj);
System.out.println(firstName + " " + lastName + " - ID: " + id);
byte[] emailObj = result.getValue(contactFamily, emailQualifier);
String email = new String(emailObj);
System.out.println(firstName + " " + lastName + " - " + email + " - ID: " + id);
}
//关闭结果
results.close();
//关闭表
table.close();
}
}
运行结果
Rosalie burton - ID: 5
Rosalie burton - [email protected] - ID: 5
package org.crazycake.playhbase;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HBaseAdmin;
/**
* 删除表
* @author alexxiyang (https://github.com/alexxiyang)
*
*/
public class DeleteTable {
public static void main(String[] args) throws IOException {
//创建配置
Configuration config = HBaseConfiguration.create();
// 建立 admin
HBaseAdmin admin = new HBaseAdmin(config);
// 先 disable 表,再delete
admin.disableTable("people");
admin.deleteTable("people");
}
}
hbase(main):004:0> list
TABLE
employee
employee2
student
users
4 row(s) in 4.8460 seconds
=> ["employee", "employee2", "student", "users"]
hbase(main):002:0> create 'users','info'
0 row(s) in 36.5110 seconds
=> Hbase::Table - users
hbase(main):003:0> list
TABLE
employee
employee2
student
users
4 row(s) in 0.4520 seconds
=> ["employee", "employee2", "student", "users"]
hbase(main):004:0> put 'users',1,'info:name','ted'
0 row(s) in 0.8350 seconds
hbase(main):005:0> scan 'users'
ROW COLUMN+CELL
1 column=info:name, timestamp=1421252020520, value=ted
1 row(s) in 0.3140 seconds
tail -200f /var/log/zookeeper/zookeeper.log