<mirror><id>nexus-tencentyun</id><mirrorOf>*</mirrorOf><name>Nexus tencentyun</name><url>http://mirrors.cloud.tencent.com/nexus/repository/maven-public/</url></mirror><profiles><profile><id>Repository Proxy</id><activation><activeByDefault>true</activeByDefault></activation><repositories><repository><id>tencentemr-hbase-ha-client-releases</id><name>releases</name><url>https://tencentemr-maven.pkg.coding.net/repository/hbase-ha-client/releases/</url><releases><enabled>true</enabled></releases><snapshots><enabled>true</enabled></snapshots></repository></repositories></profile></profiles>
mvn archetype:generate -DgroupId=com.tencent.cloud -DartifactId=test-serverless-hbase -Dversion=1.0 -DarchetypeArtifactId=maven-archetype-quickstart
test-hbase├── pom.xml└── src├── main│ └── java│ └── com│ └── tencent│ └── cloud│ └── App.java└── test└── java└── com└── tencent└── cloud└── AppTest.java
<dependencies><!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-client --><dependency><groupId>org.apache.hbase</groupId><artifactId>hbase-client</artifactId><version>2.4.5</version></dependency><dependency><groupId>com.tencentcloud.emr</groupId><artifactId>hbase-ha-client</artifactId><version>1.0.0</version></dependency><!-- https://mvnrepository.com/artifact/log4j/log4j --><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.10.1</version><configuration><source>8</source><target>8</target><encoding>utf-8</encoding></configuration></plugin><plugin><artifactId>maven-assembly-plugin</artifactId><configuration><descriptorRefs><descriptorRef>jar-with-dependencies</descriptorRef></descriptorRefs><archive><manifest><!-- Specify the class for the main method entry here --><mainClass>com.tencent.cloud.App</mainClass></manifest></archive></configuration><executions><execution><id>make-assembly</id><phase>package</phase><goals><goal>single</goal></goals></execution></executions></plugin></plugins></build>
log4j.rootLogger=INFO, stdout, logfilelog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%nlog4j.appender.logfile=org.apache.log4j.FileAppenderlog4j.appender.logfile.File=target/serverless-hbase.loglog4j.appender.logfile.layout=org.apache.log4j.PatternLayoutlog4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
package com.tencent.cloud;import com.tencentcloud.emr.HAConnection;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 java.io.IOException;import static org.apache.hadoop.hbase.HConstants.REPLICATION_SCOPE_GLOBAL;/*** Access the Serverless HBase Dual-Active instances through the Java API.*/public class App {public static void main(String[] args) throws IOException {// $quorum and $standbyQuorum are the ZooKeeper addresses of the primary and secondary instances, respectively. They can be obtained from the ZooKeeper addresses found in the access method module under the instance information page of the console.// Example: conf.set("hbase.zookeeper.quorum", "10.0.0.8,10.0.0.11,10.0.0.5");// Example: conf.set("hbase.zookeeper.quorum.standby", "10.0.0.21,10.0.0.22,10.0.0.24");Configuration conf = HBaseConfiguration.create();conf.set("hbase.zookeeper.quorum", "$quorum");conf.set("hbase.zookeeper.quorum.standby", "$standbyQuorum");// hbase.client.pause specifies the client retry interval in milliseconds, with a default value of 100 milliseconds.// hbase.client.retries.number specifies the number of client retries; the default is 15 times.// After the maximum number of request retries for the same instance is reached, it will automatically switch to the secondary instance for retries. After the maximum number of request retries for both instances is reached , an error will be returned.conf.setInt("hbase.client.pause", 10);conf.setInt("hbase.client.retries.number", 5);HAConnection connection;connection = new HAConnection(conf);Admin admin = connection.getAdmin();String TABLE_NAME = "test1";String COLUMN_FAMILY = "cf";TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(TABLE_NAME));// Set global replication.tableDescriptorBuilder.setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(COLUMN_FAMILY)).setScope(REPLICATION_SCOPE_GLOBAL).build());TableDescriptor tableDescriptor = tableDescriptorBuilder.build();System.out.println("Creating table. ");if (admin.tableExists(tableDescriptor.getTableName())) {admin.disableTable(tableDescriptor.getTableName());admin.deleteTable(tableDescriptor.getTableName());}admin.createTable(tableDescriptor);System.out.println("Put data. ");Table table1 = connection.getTable(TableName.valueOf(TABLE_NAME));Put put1 = new Put(Bytes.toBytes("row1"));put1.addColumn(Bytes.toBytes(COLUMN_FAMILY), Bytes.toBytes("a"),Bytes.toBytes("value1"));table1.put(put1);Put put2 = new Put(Bytes.toBytes("row2"));put2.addColumn(Bytes.toBytes(COLUMN_FAMILY), Bytes.toBytes("b"),Bytes.toBytes("value2"));table1.put(put2);Put put3 = new Put(Bytes.toBytes("row3"));put3.addColumn(Bytes.toBytes(COLUMN_FAMILY), Bytes.toBytes("c"),Bytes.toBytes("value3"));table1.put(put3);System.out.println("Get data. ");Get get1 = new Get(Bytes.toBytes("row1"));Result row = table1.get(get1);if (row.isEmpty()) {System.out.println("No row data found");return;}System.out.println("Row key: " + Bytes.toString(row.getRow()));byte[] value = row.getValue(Bytes.toBytes(COLUMN_FAMILY), Bytes.toBytes("a"));if (value != null) {System.out.println("Value: " + Bytes.toString(value));}connection.close();System.out.println(" Done.");}}
mvn package
Scp $localfile root@public network IP address:$remotefolder
java -jar $package.jar
hbase:001:0> list 'test1'TABLEtest11 row(s)Took 0.4315 seconds=> ["test1"]hbase:002:0> scan 'test1'ROW COLUMN+CELLrow1 column=cf:a, timestamp=2024-07-16T16:20:38.685, value=value1row2 column=cf:b, timestamp=2024-07-16T16:20:38.690, value=value2row3 column=cf:c, timestamp=2024-07-16T16:20:38.695, value=value33 row(s)Took 0.1231 secondshbase:003:0> exit
Feedback