Hbase ---- 初识 与 环境搭建
Hbase(二) shell操作
Hbase(三)Java API
Hbase(四)MR on Hbase & Hbase的 架构
Hbase(五) Hbase 的读写流程
Hbase完结篇 Hbase 集群搭建以及相关操作
<dependency>
<groupId>org.apache.hbasegroupId>
<artifactId>hbase-clientartifactId>
<version>1.2.4version>
dependency>
private Connection connection;
private Admin admin;
@Before
public void getAdmin() throws Exception {
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum", "HadoopNode00");
conf.set("hbase.zookeeper.property.clientPort", "2181");
connection = ConnectionFactory.createConnection(conf);
admin = connection.getAdmin();
}
@After
public void close() throws Exception {
admin.close();
connection.close();
}
@Test
public void createNameSpace() throws Exception {
NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create("hadoop1").addConfiguration("csdn", "123").build();
admin.createNamespace(namespaceDescriptor);
}
@Test
public void changeNameSpace() throws Exception{
NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create("hadoop").removeConfiguration("csdn").build();
admin.modifyNamespace(namespaceDescriptor);
}
@Test
public void deleteNameSpace() throws Exception{
admin.deleteNamespace("hadoop");
}
@Test
public void listNameSpace() throws Exception {
NamespaceDescriptor[] namespaceDescriptors = admin.listNamespaceDescriptors();
for (NamespaceDescriptor namespaceDescriptor : namespaceDescriptors) {
System.out.println(namespaceDescriptor.getName());
}
}
@Test
public void createTable() throws Exception {
/*
* 将表的名字信息封装到TableName中
* */
TableName tableName = TableName.valueOf("csdn:t_java");
/*
*
* 创建描述表的对象 并提供表的名字
* */
HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
/*
* 描述列簇的对象 并指定列簇的名字
* */
HColumnDescriptor cf1 = new HColumnDescriptor("cf1");
// 设置 最大可存的版本
cf1.setMaxVersions(3);
/*
* 描述列簇的对象 并指定列簇的名字
* */
HColumnDescriptor cf2 = new HColumnDescriptor("cf2");
// 设置 最大可存的版本
cf2.setMaxVersions(3);
// 在表中添加必要的属性:列簇
tableDescriptor.addFamily(cf1);
tableDescriptor.addFamily(cf2);
/*
* 使用admin对象创建表
* */
admin.createTable(tableDescriptor);
}
@Test
public void deleteTable() throws Exception {
TableName tableName = TableName.valueOf("csdn:t_java");
if (admin.tableExists(tableName)) {
admin.disableTable(tableName);
admin.deleteTable(tableName);
}
}
@Test
public void putData() throws Exception {
TableName tableName = TableName.valueOf("csdn:t_user");
Table table = connection.getTable(tableName);
/*
* 封装 一行 数据
* */
Put put = new Put("1".getBytes());
/*
* 参数列表 : 列簇 列名 值
* */
put.addColumn("cf1".getBytes(), "name".getBytes(), "zhangsan".getBytes());
put.addColumn("cf1".getBytes(), "pwd".getBytes(), "123".getBytes());
put.addColumn("cf2".getBytes(), "age".getBytes(), "18".getBytes());
put.addColumn("cf2".getBytes(), "salary".getBytes(), "1000".getBytes());
table.put(put);
table.close();
}
@Test
public void putManyData() throws Exception {
TableName tableName = TableName.valueOf("csdn:t_user");
Table table = connection.getTable(tableName);
/*
* 封装 一行 数据
* */
Put put = new Put("2".getBytes());
/*
* 参数列表 : 列簇 列名 值
* */
put.addColumn("cf1".getBytes(), "name".getBytes(), "lisi".getBytes());
put.addColumn("cf1".getBytes(), "pwd".getBytes(), "123".getBytes());
put.addColumn("cf2".getBytes(), "age".getBytes(), "20".getBytes());
put.addColumn("cf2".getBytes(), "salary".getBytes(), "20000".getBytes());
ArrayList<Put> puts = new ArrayList<Put>();
puts.add(put);
table.put(puts);
table.close();
}
@Test
public void putManyData() throws Exception {
TableName tableName = TableName.valueOf("csdn:t_user");
BufferedMutator bufferedMutator = connection.getBufferedMutator(tableName);
/*
* 封装 一行 数据
* */
Put put = new Put("2".getBytes());
/*
* 参数列表 : 列簇 列名 值
* */
put.addColumn("cf1".getBytes(), "name".getBytes(), "ls".getBytes());
put.addColumn("cf1".getBytes(), "pwd".getBytes(), "123".getBytes());
put.addColumn("cf2".getBytes(), "age".getBytes(), "20".getBytes());
put.addColumn("cf2".getBytes(), "salary".getBytes(), "20000".getBytes());
ArrayList<Put> puts = new ArrayList<Put>();
puts.add(put);
bufferedMutator.mutate(puts);
bufferedMutator.close();
}
@Test
public void deleteData() throws Exception {
TableName tableName = TableName.valueOf("csdn:t_user");
Table table = connection.getTable(tableName);
Delete delete = new Delete("2".getBytes());
table.delete(delete);
table.close();
}
@Test
public void deleteManyData() throws Exception {
TableName tableName = TableName.valueOf("csdn:t_user");
Table table = connection.getTable(tableName);
Delete delete = new Delete("1".getBytes());
table.delete(delete);
table.close();
}
@Test
public void getData() throws Exception{
Table table = connection.getTable(TableName.valueOf("csdn:t_user"));
Get get = new Get("2".getBytes());
Result result = table.get(get);
/*
* 列簇 列名
* */
byte[] name = result.getValue("cf1".getBytes(), "name".getBytes());
byte[] pwd = result.getValue("cf1".getBytes(), "pwd".getBytes());
byte[] age = result.getValue("cf2".getBytes(), "age".getBytes());
byte[] salary = result.getValue("cf2".getBytes(), "salary".getBytes());
System.out.println("名字为:"+Bytes.toString(name)+", 密码为:"+Bytes.toString(pwd)+",年龄为:"+Bytes.toString(age)+",工资为:"+Bytes.toString(salary));
}
@Test
public void getManyData() throws Exception {
Table table = connection.getTable(TableName.valueOf("csdn:t_user"));
Get get = new Get("2".getBytes());
get.setMaxVersions(3);
get.addColumn("cf1".getBytes(), "name".getBytes());
Result result = table.get(get);
List<Cell> columnCells = result.getColumnCells("cf1".getBytes(), "name".getBytes());
for (Cell columnCell : columnCells) {
byte[] rowData = CellUtil.cloneRow(columnCell);
byte[] cfData = CellUtil.cloneFamily(columnCell);
byte[] qualifierData = CellUtil.cloneQualifier(columnCell);
byte[] data = CellUtil.cloneValue(columnCell);
System.out.println("行健为:" + Bytes.toString(rowData) + ", 列簇为:" + Bytes.toString(cfData) + ",列名为:" + Bytes.toString(qualifierData) + ",名字为:" + Bytes.toString(data));
}
}
@Test
public void scanData() throws Exception {
Table table = connection.getTable(TableName.valueOf("csdn:t_user"));
Scan scan = new Scan();
// scan.addFamily("cf1".getBytes());
//scan.addColumn("cf1".getBytes(),"name".getBytes());
PrefixFilter prefixFilter1 = new PrefixFilter("1".getBytes());
PrefixFilter prefixFilter2 = new PrefixFilter("2".getBytes());
FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ONE, prefixFilter1, prefixFilter2);
scan.setFilter(filterList);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
System.out.println("------------------");
byte[] name = result.getValue("cf1".getBytes(), "name".getBytes());
byte[] pwd = result.getValue("cf1".getBytes(), "pwd".getBytes());
byte[] age = result.getValue("cf2".getBytes(), "age".getBytes());
byte[] salary = result.getValue("cf2".getBytes(), "salary".getBytes());
System.out.println("名字为:" + Bytes.toString(name) + ", 密码为:" + Bytes.toString(pwd) + ",年龄为:" + Bytes.toString(age) + ",工资为:" + Bytes.toString(salary));
}
scanner.close();
table.close();
}
加更加更加更!献给每一个正在努力的我们,就算在忙,也要注意休息和饮食哦!我就是我,一个在互联网跌跌撞撞,摸爬滚打的热忱,给个三连吧~