cd /usr/localwget https://emr-serverless-hbase-1259353343.cos.ap-guangzhou.myqcloud.com/client/emr-serverless-hbase-client.tar.gztar -zxvf emr-serverless-hbase-client.tar.gzcd emr-serverless-hbase-client
vi conf/hbase-site.xml<property><name>hbase.zookeeper.quorum</name><value>$quorum</value></property>
./bin/hbase shell
hbase:001:0> create 'test', 'cf'Created table testTook 1.5703 seconds=> Hbase::Table - test
hbase:002:0> list 'test'TABLEtest1 row(s)Took 0.0158 seconds=> ["test"]
hbase:003:0> put 'test', 'row1', 'cf:a', 'value1'Took 0.1766 secondshbase:004:0> put 'test', 'row2', 'cf:b', 'value2'Took 0.0160 secondshbase:005:0> put 'test', 'row3', 'cf:c', 'value3'Took 0.0149 seconds
hbase:006:0> scan 'test'ROW COLUMN+CELLrow1 column=cf:a, timestamp=2024-07-16T10:56:28.027, value=value1row2 column=cf:b, timestamp=2024-07-16T10:56:40.658, value=value2row3 column=cf:c, timestamp=2024-07-16T10:56:51.744, value=value33 row(s)Took 0.0682 seconds
hbase:007:0> get 'test', 'row1'COLUMN CELLcf:a timestamp=2024-07-16T10:56:28.027, value=value11 row(s)Took 0.0361 seconds
hbase:008:0> disable 'test'Took 0.6919 secondshbase:009:0> drop 'test'Took 0.3451 seconds
<mirror><id>nexus-tencentyun</id><mirrorOf>*</mirrorOf><name>Nexus tencentyun</name><url>http://mirrors.cloud.tencent.com/nexus/repository/maven-public/</url></mirror>
mvn archetype:generate -DgroupId=com.tencent.cloud-DartifactId=test-serverless-hbase -Dversion=1.0 -DarchetypeArtifactId=maven-archetype-quickstart-DarchetypeVersion=1.1
test-serverless-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><!-- 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 containing the main method entry point 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, stdoutlog4j.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 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;/*** Access the serverless HBase instance through the HBase Java API.*/public class App {public static void main(String[] args) throws IOException {// Obtain $quorum from the Zookeeper access address 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");Configuration conf = HBaseConfiguration.create();conf.set("hbase.zookeeper.quorum", "$quorum");// Establish a connection between the EMR Serverless HBase client and the data.Connection connection = ConnectionFactory.createConnection(conf);Admin admin = connection.getAdmin();// Once the connection is established, you can access the serverless HBase instance using the HBase Java API.TableDescriptorBuilder tableBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf("test1"));ColumnFamilyDescriptor cf = ColumnFamilyDescriptorBuilder.of("cf");tableBuilder.setColumnFamily(cf);TableDescriptor table = tableBuilder.build();System.out.println("Creating Table.");if (admin.tableExists(table.getTableName())) {admin.disableTable(table.getTableName());admin.deleteTable(table.getTableName());}admin.createTable(table);System.out.println("Inserting Data.");Table table1 = connection.getTable(TableName.valueOf("test1"));Put put1 = new Put(Bytes.toBytes("row1"));put1.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("a"),Bytes.toBytes("value1"));
mvn package
scp $localfile root@public 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
cd /usr/localwget https://go.dev/dl/go1.17.13.linux-amd64.tar.gztar -zxvf go1.17.13.linux-amd64.tar.gzvi /etc/profileAppend to /etc/profileexport GO_HOME=/usr/local/goexport PATH=${GO_HOME}/bin:$PATHSave and return to the command line.source /etc/profile
go mod init test-serverless-hbase
Golang Version | commit_id |
1.22.x | 731f0bdb6be56dfe0b5a5a79582161bc7fbed32b |
1.18.x~1.21.x | 1fee39f343954ca7501a6b8f25abd9f86eaf618b |
1.13.x~1.17.x | 05795eede1cb2442e6cc0313db93a4b544b49148 |
go env -w GOPROXY=https://goproxy.cngo get github.com/tsuna/gohbase@commit_id# Taking Version 1.17.13 as an Example: go get github.com/tsuna/gohbase@05795eede1cb2442e6cc0313db93a4b544b49148
test-serverless-hbase/├── demo.go├── go.mod└── go.sum
package mainimport ("context""fmt""github.com/tsuna/gohbase""github.com/tsuna/gohbase/hrpc""log")func main() {// Obtain $quorum from the Zookeeper access address found in the access method module under the instance information page of the console.// Example: quorum := "10.0.0.8,10.0.0.11,10.0.0.5"quorum := $quorum// Creating Table.tableName := "test2"var cfs = map[string]map[string]string{"cf1": {"MIN_VERSIONS": "1",},}admin := gohbase.NewAdminClient(quorum)crt := hrpc.NewCreateTable(context.Background(), []byte(tableName), cfs)err := admin.CreateTable(crt)if err != nil {log.Fatal(err)}client := gohbase.NewClient(quorum)// Write data.putRequest, err := hrpc.NewPutStr(context.Background(), tableName, "my_row_key", map[string]map[string][]byte{"cf1": map[string][]byte{"col1": []byte("value1"),"col2": []byte("value2"),},})if err != nil {log.Fatal(err)}
go build demo.go
go env -w GOOS=linuxgo build demo.go
scp $localfile root@public IP address:$remotefolder
chmod +x demo./demo
hbase:001:0> list 'test2'TABLEtest21 row(s)Took 0.3814 seconds=> ["test2"]hbase:002:0> scan 'test2'ROW COLUMN+CELLmy_row_key column=cf1:col1, timestamp=2024-07-17T21:16:49.302, value=value1my_row_key column=cf1:col2, timestamp=2024-07-17T21:16:49.302, value=value21 row(s)Took 0.1268 secondshbase:003:0> exit
Was this page helpful?