HBase2. 4.8 detailed tutorial (III) Java operation HBase

Miss Zhu 2022-02-13 08:37:16 阅读数:598

hbase2. hbase detailed tutorial iii

stay idea To establish Maven Java project .
stay pom.xml Import the corresponding version of HBase rely on :

 <dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-client -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.4.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase</artifactId>
<version>2.4.8</version>
<type>pom</type>
</dependency>
</dependencies>

Java Code , See the code and notes for details .

package com.alan;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class TestHBase {

private static Configuration conf = HBaseConfiguration.create();
private static Admin admin;
private static Connection connection = null;
static {

//HBase Master Address , stay hbase-site.xml Middle configuration 
conf.set("hbase.rootdir", "hdfs://hp301:9000/hbase");
//ZooKeeper Three server addresses 
conf.set("hbase.zookeeper.quorum", "hp301,hp302,hp303");
try {

connection = ConnectionFactory.createConnection(conf);
admin = connection.getAdmin();
} catch (IOException e) {

e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {

TestHBase test = new TestHBase();
// test.createTable("java_test");// Build table 
//test.createTable2("java_test","f1","f2");// Build table ( Make line keys )
//test.addData("java_test");// insert data 
//test.insertBatchData("java_test");// Insert multiple data 
//test.getData("java_test");// adopt Get Find single line data , Note the type of column value 
test.scanAllData("java_test");// adopt Scan Methods scan the whole table 
// test.deleteData("java_test");// Delete table data 
//test.deleteTable("java_test");// Delete table 
admin.close();
connection.close();
}
public void createTable2(String tableName, String... columnFamily) {

TableName tableNameObj = TableName.valueOf(tableName);
try {

if (admin.tableExists(tableNameObj)) {

System.out.println("Table: " + tableName + " already exists!");
} else {

HTableDescriptor tb = new HTableDescriptor(tableNameObj);
for (int i = 0; i < columnFamily.length; i++) {

HColumnDescriptor family = new HColumnDescriptor(columnFamily[i]);
tb.addFamily(family);
}
admin.createTable(tb);
System.out.println(tableName + " Table created successfully ");
}
} catch (IOException e) {

e.printStackTrace();
System.out.println(tableName + " Failed to create table ");
}
}
public void createTable(String tableName) throws IOException {

// Create a table from the administrator object 
HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
// Add a column family to the table 
HColumnDescriptor f1 = new HColumnDescriptor("f1");
HColumnDescriptor f2 = new HColumnDescriptor("f2");
// Set the two column families to In the created table 
hTableDescriptor.addFamily(f1);
hTableDescriptor.addFamily(f2);
// Create table 
admin.createTable(hTableDescriptor);
// Close the connection 
admin.close();
connection.close();
System.out.println(" Table created successfully ");
}
public void addData(String tableName) throws IOException {

// Get table object 
Table myuser = connection.getTable(TableName.valueOf(tableName));
Put put = new Put("0001".getBytes());
put.addColumn("f1".getBytes(), "id".getBytes(), Bytes.toBytes(1));
put.addColumn("f1".getBytes(), "name".getBytes(), Bytes.toBytes(" Xiao Ming "));
put.addColumn("f1".getBytes(), "age".getBytes(), Bytes.toBytes(22));
put.addColumn("f2".getBytes(), "sex".getBytes(), Bytes.toBytes(" male "));
put.addColumn("f2".getBytes(), "address".getBytes(), Bytes.toBytes(" Qingdao "));
put.addColumn("f2".getBytes(), "phone".getBytes(), Bytes.toBytes("15900000001"));
put.addColumn("f2".getBytes(), "say".getBytes(), Bytes.toBytes(" Hello "));
myuser.put(put);
// Close table 
myuser.close();
System.out.println(" Insert data succeeded ");
}
public void insertBatchData(String tableName) throws IOException {

// Get the table 
Table myuser = connection.getTable(TableName.valueOf(tableName));
// establish put object , And designate rowkey
Put put = new Put("0002".getBytes());
put.addColumn("f1".getBytes(), "id".getBytes(), Bytes.toBytes(1));
put.addColumn("f1".getBytes(), "name".getBytes(), Bytes.toBytes(" Xiaohong "));
put.addColumn("f1".getBytes(), "age".getBytes(), Bytes.toBytes(21));
put.addColumn("f2".getBytes(), "sex".getBytes(), Bytes.toBytes(" Woman "));
put.addColumn("f2".getBytes(), "address".getBytes(), Bytes.toBytes(" jinan "));
put.addColumn("f2".getBytes(), "phone".getBytes(), Bytes.toBytes("15900000002"));
put.addColumn("f2".getBytes(), "say".getBytes(), Bytes.toBytes("Hello!"));
Put put2 = new Put("0003".getBytes());
put2.addColumn("f1".getBytes(), "id".getBytes(), Bytes.toBytes(2));
put2.addColumn("f1".getBytes(), "name".getBytes(), Bytes.toBytes(" cockroach "));
put2.addColumn("f1".getBytes(), "age".getBytes(), Bytes.toBytes(22));
put2.addColumn("f2".getBytes(), "sex".getBytes(), Bytes.toBytes(" male "));
put2.addColumn("f2".getBytes(), "address".getBytes(), Bytes.toBytes(" Beijing "));
put2.addColumn("f2".getBytes(), "phone".getBytes(), Bytes.toBytes("15900000003"));
put2.addColumn("f2".getBytes(), "say".getBytes(), Bytes.toBytes(" An Beijing ……"));
Put put3 = new Put("0004".getBytes());
put3.addColumn("f1".getBytes(), "id".getBytes(), Bytes.toBytes(4));
put3.addColumn("f1".getBytes(), "name".getBytes(), Bytes.toBytes(" floret "));
put3.addColumn("f1".getBytes(), "age".getBytes(), Bytes.toBytes(23));
put3.addColumn("f2".getBytes(), "sex".getBytes(), Bytes.toBytes(" Woman "));
put3.addColumn("f2".getBytes(), "address".getBytes(), Bytes.toBytes(" Chongqing "));
put3.addColumn("f2".getBytes(), "phone".getBytes(), Bytes.toBytes("15900000004"));
put3.addColumn("f2".getBytes(), "say".getBytes(), Bytes.toBytes(" To have "));
Put put4 = new Put("0005".getBytes());
put4.addColumn("f1".getBytes(), "id".getBytes(), Bytes.toBytes(5));
put4.addColumn("f1".getBytes(), "name".getBytes(), Bytes.toBytes(" The little hero "));
put4.addColumn("f1".getBytes(), "age".getBytes(), Bytes.toBytes(24));
put4.addColumn("f2".getBytes(), "sex".getBytes(), Bytes.toBytes(" male "));
put4.addColumn("f2".getBytes(), "address".getBytes(), Bytes.toBytes(" Shanghai "));
put4.addColumn("f2".getBytes(), "phone".getBytes(), Bytes.toBytes("15900000005"));
put4.addColumn("f2".getBytes(), "say".getBytes(), Bytes.toBytes(" Alasanghai people "));
Put put5 = new Put("0006".getBytes());
put5.addColumn("f1".getBytes(), "id".getBytes(), Bytes.toBytes(6));
put5.addColumn("f1".getBytes(), "name".getBytes(), Bytes.toBytes(" Light rain "));
put5.addColumn("f1".getBytes(), "age".getBytes(), Bytes.toBytes(22));
put5.addColumn("f2".getBytes(), "sex".getBytes(), Bytes.toBytes(" Woman "));
put5.addColumn("f2".getBytes(), "address".getBytes(), Bytes.toBytes(" guangdong "));
put5.addColumn("f2".getBytes(), "phone".getBytes(), Bytes.toBytes("15900000006"));
put5.addColumn("f2".getBytes(), "say".getBytes(), Bytes.toBytes(" Sprinkle water "));
Put put6 = new Put("0007".getBytes());
put6.addColumn("f1".getBytes(), "id".getBytes(), Bytes.toBytes(7));
put6.addColumn("f1".getBytes(), "name".getBytes(), Bytes.toBytes(" Melon "));
put6.addColumn("f1".getBytes(), "age".getBytes(), Bytes.toBytes(22));
put6.addColumn("f2".getBytes(), "sex".getBytes(), Bytes.toBytes(" male "));
put6.addColumn("f2".getBytes(), "address".getBytes(), Bytes.toBytes(" jiangsu "));
put6.addColumn("f2".getBytes(), "phone".getBytes(), Bytes.toBytes("15900000007"));
put6.addColumn("f2".getBytes(), "say".getBytes(), Bytes.toBytes(" I'm from southern Jiangsu "));
List<Put> listPut = new ArrayList<Put>();
listPut.add(put);
listPut.add(put2);
listPut.add(put3);
listPut.add(put4);
listPut.add(put5);
listPut.add(put6);
myuser.put(listPut);
myuser.close();
System.out.println(" Insert multiple data successfully ");
}
public void getData(String tableName) throws IOException {

// Get table object 
Table myuser = connection.getTable(TableName.valueOf(tableName));
Get get = new Get("0005".getBytes());
get.addColumn("f2".getBytes(), "address".getBytes());// Column family ; Name 
// Get the return result 
Result result = myuser.get(get);
List<Cell> cells = result.listCells();
for (Cell cell : cells) {

// Get the name of the column family 
String familyName = Bytes.toString(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
// Get the name of the column 
String columnName = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
if (familyName.equals("f1") && columnName.equals("id") || columnName.equals("age")) {

int value = Bytes.toInt(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
System.out.println(" The names of the clans : " + familyName + " , Name : " + columnName + " , The column value :" + value);
} else {

String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
System.out.println(" The names of the clans : " + familyName + " , Name : " + columnName + " , The column value :" + value);
}
}
// Close table 
myuser.close();
}
public void scanAllData(String tableName) throws IOException {

// Get table object 
Table myuser = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
// Set the start and end rowkey, The scan result is :[) type 
scan.setStartRow("0001".getBytes());
scan.setStopRow("0008".getBytes());
ResultScanner scanner = myuser.getScanner(scan);
for (Result result : scanner) {

List<Cell> cells = result.listCells();
for (Cell cell : cells) {

String rowkey = Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
// Get the name of the column family 
String familyName = Bytes.toString(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
// Get the name of the column 
String columnName = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
if (familyName.equals("f1") && columnName.equals("id") || columnName.equals("age")) {

int value = Bytes.toInt(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
System.out.println(" The names of the clans : " + familyName + " , Name : " + columnName + " , The column value :" + value);
} else {

String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
System.out.println(" The names of the clans : " + familyName + " , Name : " + columnName + " , The column value :" + value);
}
}
}
// Get the return result 
myuser.close();
}
public void deleteData(String talbeName) throws IOException {

// Get table object 
Table myuser = connection.getTable(TableName.valueOf(talbeName));
Delete delete = new Delete("0001".getBytes());
myuser.delete(delete);
myuser.close();
System.out.println(" Delete successful ");
}
public void deleteTable(String tableName) throws IOException {

// Get the administrator object 
Admin admin = connection.getAdmin();
// Disable the table 
admin.disableTable(TableName.valueOf(tableName));
// Delete table 
admin.deleteTable(TableName.valueOf(tableName));
System.out.println(" Delete table succeeded ");
}
}

Run the query code , Output in console :
 Insert picture description here

copyright:author[Miss Zhu],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130837139648.html