1. Table exists
package org.example; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.Admin; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import java.io.IOException; public class TestDemo { public static Connection connection=null; public static Admin admin=null; static { try { Configuration configuration = HBaseConfiguration.create(); configuration.set("hbase.rootdir", "hdfs://192.168.170.80:8020/hbase"); configuration.set("hbase.zookeeper.quorum","hadooplyf316"); connection= ConnectionFactory.createConnection(configuration); admin = connection.getAdmin(); } catch (IOException e) { e.printStackTrace(); } } //Determine whether the table exists public static boolean isTableExist(String tableName) throws IOException { boolean exists = admin.tableExists(TableName.valueOf(tableName)); return exists; } public static void close(){ if (admin!=null){ try { admin.close(); } catch (IOException e) { e.printStackTrace(); } } if (connection!=null){ try { connection.close(); } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) throws IOException { //System.out.println(isTableExist("Student")); if(isTableExist("Student")){ System.out.println("Table exists"); } else{ System.out.println("does not exist"); } //Close the resource close(); } }
2.scan
package org.example; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.*; import org.apache.hadoop.hbase.client.*; import java.io.IOException; import java.util.Scanner; public class scan { public static Configuration configuration; public static Connection connection; public static Admin admin; // establish connection public static void init() { configuration = HBaseConfiguration.create(); configuration.set("hbase.rootdir", "hdfs://192.168.170.80:8020/hbase"); configuration.set("hbase.zookeeper.quorum","hadooplyf316"); try { connection = ConnectionFactory.createConnection(configuration); admin = connection.getAdmin(); } catch (IOException e) { e.printStackTrace(); } } // close connection public static void close() { try { if (admin != null) { admin.close(); } if (null != connection) { connection.close(); } } catch (IOException e) { e.printStackTrace(); } } /** * * Find table information based on table name * */ public static void getData(String tableName) throws IOException { init(); Table table = connection.getTable(TableName.valueOf(tableName)); Scan scan = new Scan(); ResultScanner scanner = table.getScanner(scan); for (Result result : scanner) { showCell((result)); } close(); } /** * * Formatted output * * @param result * */ public static void showCell(Result result) { Cell[] cells = result.rawCells(); for (Cell cell : cells) { System.out.println("RowName(row key):" + new String(CellUtil.cloneRow(cell)) + " "); System.out.println("Timetamp(timestamp):" + cell.getTimestamp() + " "); System.out.println("column Family:" + new String(CellUtil.cloneFamily(cell)) + " "); System.out.println("column Name:" + new String(CellUtil.cloneQualifier(cell)) + " "); System.out.println("value:(value)" + new String(CellUtil.cloneValue(cell)) + " "); System.out.println(); } } public static void main(String[] args) throws IOException { // TODO Auto-generated method stub scan t = new scan(); System.out.println("Please enter the name of the table you want to view"); Scanner scan = new Scanner(System.in); String tableName = scan.nextLine(); System.out.println("The information is as follows:"); t.getData(tableName); } }
3. Create table
package org.example; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; import java.io.IOException; public class TableAdminTest { private Connection connection; private Admin admin; @BeforeTest public void beforeTest() throws IOException { // 1. Use HbaseConfiguration.create() to create Hbase configuration Configuration configuration = HBaseConfiguration.create(); configuration.set("hbase.rootdir", "hdfs://192.168.170.80:8020/hbase"); configuration.set("hbase.zookeeper.quorum","hadooplyf316"); // 2. Use ConnectionFactory.createConnection() to create an Hbase connection connection = ConnectionFactory.createConnection(configuration); // 3. To create a table, you need to obtain the admin management object based on the Hbase connection. // To create a table or delete a table, you need to connect to HMaster, so you need an admin object. admin = connection.getAdmin(); } @Test public void createTableTest() throws IOException { TableName tableName = TableName.valueOf("test"); // 1. Determine whether the table exists if(admin.tableExists(tableName)) { // a) exists, then exit return; } // Build table // 2. Use TableDescriptorBuilder.newBuilder to build the table description builder // TableDescriptor: Table descriptor, describing how many columns this table has and other attributes can be configured here TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tableName); // 3. Use ColumnFamilyDescriptorBuilder.newBuilder to build the column description builder // Creating a column also requires a column descriptor, which needs to be built with a ColumnFamilyDescriptor // A tool class is often used: Bytes (Bytes tool class under the hbase package) // This tool class can convert string, long, and double types into byte[] arrays // You can also convert the byte[] array to the specified type ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("C1")); // 4. Construct column description and table description ColumnFamilyDescriptor cfDes = columnFamilyDescriptorBuilder.build(); // Establish a relationship between tables and columns tableDescriptorBuilder.setColumnFamily(cfDes); TableDescriptor tableDescriptor = tableDescriptorBuilder.build(); admin.createTable(tableDescriptor); } @Test public void deleteTableTest() throws IOException { TableName tableName = TableName.valueOf("t"); if(admin.tableExists(tableName)) { admin.disableTable(tableName); admin.deleteTable(tableName); } } @AfterTest public void afterTest() throws IOException { admin.close(); connection.close(); } }
4.List
package org.example; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.*; import org.apache.hadoop.hbase.client.*; import java.io.IOException; public class list { public static Configuration configuration; public static Connection connection; public static Admin admin; /** * establish connection */ public static void init() { configuration = HBaseConfiguration.create(); configuration.set("hbase.rootdir", "hdfs://192.168.170.80:8020/hbase"); configuration.set("hbase.zookeeper.quorum","hadooplyf316"); try { connection = ConnectionFactory.createConnection(configuration); admin = connection.getAdmin(); } catch (IOException e) { e.printStackTrace(); } } /** * Close the connection */ public static void close() { try { if (admin != null) { admin.close(); } if (null != connection) { connection.close(); } } catch (IOException e) { e.printStackTrace(); } } /** * * View existing tables through the method listTables() * * @throwsIOException * */ public static void listTables() throws IOException { init(); HTableDescriptor hTableDescriptors[] = admin.listTables(); for (HTableDescriptor hTableDescriptor : hTableDescriptors) { System.out.println(hTableDescriptor.getNameAsString()); } close(); } public static void main(String[] args) { list t = new list(); try { System.out.println("The following is the table information stored in the Hbase database"); t.listTables(); } catch (IOException e) { e.printStackTrace(); } } }
The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Java skill treeUsing JDBC to operate databasesJDBC Overview 139502 people are learning the system