一、创建Maven项目并添加依赖
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.3.5</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>2.3.5</version>
</dependency>
二、创建初始化方法与关闭方法
private static Configuration config = null;
private static Connection conn = null;
private static Admin admin = null;
static {
System.out.println("执行初始化方法");
config = HBaseConfiguration.create();
config.set(HConstants.HBASE_DIR,"hdfs://192.168.136.144:9000/hbase");
config.set(HConstants.ZOOKEEPER_QUORUM,"192.168.136.144");
config.set(HConstants.CLIENT_PORT_STR,"2181");
try {
conn = ConnectionFactory.createConnection(config);
admin = conn.getAdmin();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void close(){
System.out.println("执行close()");
try {
if (admin!=null){
admin.close();
}
if (conn!=null){
conn.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
三、创建命名空间
public void createNameSpace() throws IOException {
NamespaceDescriptor xsqone = NamespaceDescriptor.create("xsqone").build();
admin.createNamespace(xsqone);
this.close();
}
四、查询所有命名空间
public void getAllNameSpace() throws IOException {
String[] nps = admin.listNamespaces();
String s = Arrays.toString(nps);
System.out.println(s);
this.close();
}
五、查询命名空间中的表
public void getNameSpaceTable() throws IOException {
List<TableDescriptor> tableDescriptors = admin.listTableDescriptorsByNamespace("xsqone".getBytes());
System.out.println(tableDescriptors);
this.close();
}
六、创建表
public void createTable() throws IOException {
TableName tableName = TableName.valueOf("xsqone:student");
HTableDescriptor desc = new HTableDescriptor(tableName);
HColumnDescriptor family1 = new HColumnDescriptor("info1");
HColumnDescriptor family2 = new HColumnDescriptor("info2");
desc.addFamily(family1);
desc.addFamily(family2);
admin.createTable(desc);
this.close();
}
七、删除表
public void dropTable() throws IOException {
admin.disableTable(TableName.valueOf("xsqone:student"));
admin.deleteTable(TableName.valueOf("xsqone:student"));
this.close();
}
八、插入数据
public void insertData() throws IOException {
Table table = conn.getTable(TableName.valueOf("xsqone:student"));
Put put2 = new Put(Bytes.toBytes("student1"));
put2.addColumn("info1".getBytes(),"name".getBytes(),"zs".getBytes());
put2.addColumn("info2".getBytes(),"school".getBytes(),"td".getBytes());
Put put3 = new Put(Bytes.toBytes("student2"));
put3.addColumn("info1".getBytes(),"name".getBytes(),"ls".getBytes());
put3.addColumn("info2".getBytes(),"school".getBytes(),"aa".getBytes());
Put put4 = new Put(Bytes.toBytes("student3"));
put4.addColumn("info1".getBytes(),"name".getBytes(),"ww".getBytes());
put4.addColumn("info2".getBytes(),"school".getBytes(),"bb".getBytes());
ArrayList<Put> puts = new ArrayList<Put>();
puts.add(put2);
puts.add(put3);
puts.add(put4);
table.put(puts);
this.close();
}
九、查询数据
public void quaryData() throws IOException {
Table table = conn.getTable(TableName.valueOf("xsqone:student"));
Get get = new Get(Bytes.toBytes("student1"));
Result result = table.get(get);
byte[] value = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("name"));
System.out.println(Bytes.toString(value));
byte[] value1 = result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("school"));
System.out.println(Bytes.toString(value1));
this.close();
}
public void sccanData() throws IOException {
Table table = conn.getTable(TableName.valueOf("xsqone:student"));
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
byte[] value = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("name"));
System.out.println(Bytes.toString(value));
byte[] value1 = result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("school"));
System.out.println(Bytes.toString(value1));
}
this.close();
}