Overview
Scenarios
Product Architecture
Instance Types
Compatibility Notes
package com.example;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.client.*;import org.apache.hadoop.hbase.util.Bytes;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.TableName;public class HBaseCreateTableDemo {public static void main(String[] args) throws Exception {System.out.println("======================================================================");System.out.println("HBase Create Table Demo Start");System.out.println("======================================================================");Configuration config = HBaseConfiguration.create();config.setBoolean("hbase.defaults.for.version.skip", Boolean.TRUE);config.set("hbase.client.tdsql.quorum", "10.10.10.10:6752");config.setBoolean("hbase.tdsql.hash.partition", Boolean.TRUE);config.set("mysql.native.username", "test");config.set("mysql.native.password", "test123");// Define table name and column families.final TableName tableName = TableName.valueOf("demo_table");final byte[] INFO_CF = Bytes.toBytes("info");final byte[] DETAIL_CF = Bytes.toBytes("detail");try (Connection connection = ConnectionFactory.createConnection(config)) {try (Admin admin = connection.getAdmin()) {// Check whether the table already exists.if (admin.tableExists(tableName)) {System.out.println("Table " + tableName.getNameAsString() + " already exists");System.out.println("Skipping table creation...");} else {// Create table descriptor// info column family: min_versions=1, max_versions=2// detail column family: min_versions=2, max_versions=4System.out.println("\\n=== Creating table: " + tableName.getNameAsString() + " ===");TableDescriptorBuilder tdb = TableDescriptorBuilder.newBuilder(tableName);tdb.setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(INFO_CF).setMinVersions(1).setMaxVersions(2).build());tdb.setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(DETAIL_CF).setMinVersions(2).setMaxVersions(4).build());// Create tableadmin.createTable(tdb.build());System.out.println("Table created successfully: " + tableName.getNameAsString());}}}System.out.println("\\n======================================================================");System.out.println("HBase Create Table Demo Completed");System.out.println("======================================================================");}}
Parameter Name | Type | Required | Description | Example Value |
hbase.client.tdsql.quorum | String | Yes | RPC address and port for TDSQL Boundless service. | 10.10.10.10:6752 |
hbase.tdsql.hash.partition | Boolean | Yes | To enable HBase compatible mode, it must be set to true. | Boolean.TRUE |
mysql.native.username | String | Yes | User account. | user |
mysql.native.password | String | Yes | Account password. | passwd |
package com.example;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.client.*;import org.apache.hadoop.hbase.util.Bytes;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.TableName;/*** HBase modify column families sample program* Demonstrates how to modify the column family properties of a table.*/public class HBaseModifyColumnFamilyDemo {public static void main(String[] args) throws Exception {System.out.println("======================================================================");System.out.println("HBase Modify Column Family Demo Start");System.out.println("======================================================================");Configuration config = HBaseConfiguration.create();config.set("hbase.client.tdsql.quorum", "10.10.10.10:6752");config.setBoolean("hbase.tdsql.hash.partition", Boolean.TRUE);config.set("mysql.native.username", "test");config.set("mysql.native.password", "test123");// Define table name and column families.final TableName tableName = TableName.valueOf("demo_table");final byte[] FAMILY = Bytes.toBytes("info");final byte[] otherFamily = Bytes.toBytes("detail");try (Connection connection = ConnectionFactory.createConnection(config)) {try (Admin admin = connection.getAdmin()) {// Check whether the table exists.if (!admin.tableExists(tableName)) {System.out.println("✗ Table " + tableName.getNameAsString() + " does not exist");System.out.println("Please create the table first.");return;}System.out.println("Table " + tableName.getNameAsString() + " exists");// Obtain the descriptor of the current tableSystem.out.println("\\n=== Getting current table descriptor ===");TableDescriptor current = admin.getDescriptor(tableName);// Print the current column family configurationSystem.out.println("\\nCurrent Column Family Configuration:");for (ColumnFamilyDescriptor cf : current.getColumnFamilies()) {System.out.println(" CF: " + Bytes.toString(cf.getName()));System.out.println(" MinVersions: " + cf.getMinVersions());System.out.println(" MaxVersions: " + cf.getMaxVersions());System.out.println(" TimeToLive: " + cf.getTimeToLive());}// Modify the MaxVersions of the first CFSystem.out.println("\\n=== Modifying Column Family: " + Bytes.toString(FAMILY) + " ===");ColumnFamilyDescriptor updatedPrimary =ColumnFamilyDescriptorBuilder.newBuilder(current.getColumnFamily(FAMILY)).setMaxVersions(current.getColumnFamily(FAMILY).getMaxVersions() + 3).build();System.out.println(" Old MaxVersions: " + current.getColumnFamily(FAMILY).getMaxVersions());System.out.println(" New MaxVersions: " + updatedPrimary.getMaxVersions());// Modify the MinVersions and TimeToLive of the second CFSystem.out.println("\\n=== Modifying Column Family: " + Bytes.toString(otherFamily) + " ===");ColumnFamilyDescriptor updatedSecondary =ColumnFamilyDescriptorBuilder.newBuilder(current.getColumnFamily(otherFamily)).setMinVersions(current.getColumnFamily(otherFamily).getMinVersions() + 1).setTimeToLive(60).build();System.out.println(" Old MinVersions: " + current.getColumnFamily(otherFamily).getMinVersions());System.out.println(" New MinVersions: " + updatedSecondary.getMinVersions());System.out.println(" Old TimeToLive: " + current.getColumnFamily(otherFamily).getTimeToLive());System.out.println(" New TimeToLive: " + updatedSecondary.getTimeToLive());// Apply the modifications using modifyColumnFamilySystem.out.println("\\n=== Applying modifications ===");TableDescriptor updated = TableDescriptorBuilder.newBuilder(current).modifyColumnFamily(updatedPrimary).modifyColumnFamily(updatedSecondary).build();admin.modifyTable(updated);System.out.println("Table modified successfully");// Verify the modification resultsSystem.out.println("\\n=== Verifying modifications ===");TableDescriptor modified = admin.getDescriptor(tableName);System.out.println("\\nModified Column Family Configuration:");for (ColumnFamilyDescriptor cf : modified.getColumnFamilies()) {System.out.println(" CF: " + Bytes.toString(cf.getName()));System.out.println(" MinVersions: " + cf.getMinVersions());System.out.println(" MaxVersions: " + cf.getMaxVersions());System.out.println(" TimeToLive: " + cf.getTimeToLive());}}}System.out.println("\\n======================================================================");System.out.println("HBase Modify Column Family Demo Completed");System.out.println("======================================================================");}}
피드백