Cette rubrique explique comment accéder à LindormTable à l'aide de l'API Java HBase et fournit des exemples de code.
Prérequis
Le kit de développement Java (JDK) version 1.8 ou ultérieure est installé.
Vous avez obtenu l'endpoint LindormTable pour l'API Java HBase.
Vous avez ajouté l'adresse IP de votre client à la liste d'autorisation Lindorm.
Vous avez installé le SDK Java HBase.
Procédure
-
Configurez le client pour qu'il se connecte à votre instance Lindorm en suivant l'une des méthodes ci-dessous.
-
Ajoutez les propriétés suivantes au fichier de configuration
hbase-site.xml.<configuration> <!-- The cluster endpoint. You can find this on the Database Connections page in the console. Note the distinction between the public and VPC endpoints. --> <property> <name>hbase.zookeeper.quorum</name> <value>ld-xxxx-proxy-hbaseue.lindormue.xxx.rds.aliyuncs.com:30020</value> </property> <!-- Set the username and password. The default value for both is root. You can change them as needed. --> <property> <name>hbase.client.username</name> <value>testuser</value> </property> <property> <name>hbase.client.password</name> <value>password</value> </property> </configuration> -
Intégrez l'extrait de code suivant à votre projet afin de créer un objet Configuration et de configurer les paramètres décrits dans le tableau ci-dessous.
// Create a new Configuration object. Configuration conf = HBaseConfiguration.create(); // The endpoint for the instance. You can obtain this on the Database Connections page in the console. conf.set("hbase.zookeeper.quorum", "host:port"); // Set the username and password for the connection. conf.set("hbase.client.username", "your_username"); conf.set("hbase.client.password", "your_password");Annotation
Example
Method to obtain the parameter value
host:port
ld-bp17j28j2y7pm****-proxy-lindorm-pub.lindorm.rds.aliyuncs.com:30020
L'endpoint de connexion pour LindormTable. Cet endpoint s'affiche dans la section Access by Using HBase Java API de la console. Pour plus de détails, consultez View endpoints.
The username.
testuser
Le nom d'utilisateur et le mot de passe que vous utilisez pour vous connecter à l'instance Lindorm. Si vous avez oublié le mot de passe, vous pouvez le modifier dans le système de gestion du cluster de LindormTable. Pour plus d'informations, consultez Change the password of a user.
Password
password
-
-
Établissez une connexion entre le client Lindorm et la base de données.
Connection connection = ConnectionFactory.createConnection(conf);RemarqueLa connexion n'est créée qu'une seule fois durant le cycle de vie du programme. Elle est thread-safe et peut être partagée entre tous les threads. Une fois le programme arrêté, fermez l'objet Connection pour éviter toute fuite de connexion. Vous pouvez également utiliser l'instruction try-finally pour prévenir ce type de fuite.
-
Une fois la connexion établie, utilisez l'API Java d'ApsaraDB for HBase pour accéder à LindormTable. Vous pouvez vous appuyer sur l'exemple de code Java suivant.
-
Opérations DDL
try (Admin admin = connection.getAdmin()){ // Create a table. HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("tablename")); htd.addFamily(new HColumnDescriptor(Bytes.toBytes("family"))); // Create a table with a single region. Creating a table with a single region limits parallel processing and load balancing, which can cause performance bottlenecks and data hotspots. We recommend pre-splitting the table into multiple regions based on your data's characteristics. admin.createTable(htd); // To create a table with multiple regions: // The following example pre-splits the table into two regions: [-∞, 10) and [10, ∞). Generate the split keys based on your business requirements. // int numRegions = 2; // Set the number of regions to create. // byte[][] splitKeys = new byte[numRegions-1][]; // for (int i = 1; i < numRegions; i++) { // splitKeys[i-1] = new byte[]{(byte)(i * 10)}; // } // admin.createTable(tableDescriptor, splitKeys); // Disable the table. admin.disableTable(TableName.valueOf("tablename")); // Truncate the table to clear all its data. You must disable the table first. admin.truncateTable(TableName.valueOf("tablename"), true); // Delete the table. You must disable the table first. admin.deleteTable(TableName.valueOf("tablename")); } -
Opérations DML
// The Table object is not thread-safe. Each thread must get its own Table instance from the Connection object. try (Table table = connection.getTable(TableName.valueOf("tablename"))) { // Insert data. Put put = new Put(Bytes.toBytes("row")); put.addColumn(Bytes.toBytes("family"), Bytes.toBytes("qualifier"), Bytes.toBytes("value")); table.put(put); // Read a single row. Get get = new Get(Bytes.toBytes("row")); Result res = table.get(get); // Delete a row. Delete delete = new Delete(Bytes.toBytes("row")); table.delete(delete); // Scan a range of rows. Scan scan = new Scan(Bytes.toBytes("startRow"), Bytes.toBytes("endRow")); ResultScanner scanner = table.getScanner(scan); for (Result result : scanner) { // Process the scan results. // ... } scanner.close(); }
-