包依赖比较麻烦,找了好久,我用的CDH5.0 现将所依赖的包的列表清单如下:
public class EmployeeDao {
/**
* @param args
*/
public static Configuration configuration;
static {
configuration = new Configuration();
String filePath = "hbase-site.xml";
Path path = new Path(filePath);
configuration.addResource(path);
configuration = HBaseConfiguration.create(configuration);
}
//使用过滤器实现查询
public List QueryEmployeeByName(String name) throws Exception {
String rowKey = name;
HTable table = new HTable(configuration, "employee".getBytes());
Scan scan = new Scan();
// select ..... from .. where filter
scan.addColumn("info".getBytes(), "age".getBytes());
scan.addColumn("info".getBytes(), "sex".getBytes());
RowFilter filter = new RowFilter(CompareOp.EQUAL,
new RegexStringComparator(rowKey));
scan.setFilter(filter);
List list = new ArrayList();
ResultScanner rScanner = table.getScanner(scan);
for (Result rs : rScanner) {
Employee e = new Employee();
for (KeyValue kValue : rs.list()) {
if ("sex".equalsIgnoreCase(new String(kValue.getQualifier()))) {
e.setSex(new String(kValue.getValue()));
}
if ("age".equalsIgnoreCase(new String(kValue.getQualifier()))) {
e.setAge(Integer.parseInt((new String(kValue.getValue()))));
}
}
list.add(e);
}
return list;
}
// 插入一个单元格数据
public static void insertOneRow(String tableName, String rowkey,
String columnFamily, String column, String value) throws Exception {
HTable table = new HTable(configuration, tableName);
Put put = new Put(Bytes.toBytes(rowkey));
put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column),
Bytes.toBytes(value));
table.put(put);// 放入表
table.close();// 释放资源
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
EmployeeDao dao = new EmployeeDao();
List l = dao.QueryEmployeeByName("oftenlin");
for (Employee e : l) {
System.out.println("Name:oftenlin," + "Sex:" + e.getSex() + ",Age:"+ e.getAge());
}
//insertOneRow("employee","allen","info","scholl","shi yan mid school");
insertOneRow("employee","gold fly","info","scholl","shi yan mid school");
System.out.println("写入成功!");
}
}
hbase-site.xml 的清单
hbase.rootdir
hdfs://CentOS-cm:8020/hbase
hbase.client.write.buffer
2097152
hbase.client.pause
100
hbase.client.retries.number
35
hbase.client.scanner.caching
100
hbase.client.keyvalue.maxsize
10485760
hbase.rpc.timeout
60000
hbase.snapshot.enabled
true
hbase.security.authentication
simple
zookeeper.session.timeout
60000
zookeeper.znode.parent
/hbase
zookeeper.znode.rootserver
root-region-server
hbase.zookeeper.quorum
CentOS-server3,CentOS-server2,CentOS-server1
hbase.zookeeper.property.clientPort
2181