1>pom.xml
<dependency>
<groupId>com.aliyun.openservices</groupId>
<artifactId>tablestore-hbase-client</artifactId>
<version>1.2.0</version>
</dependency>
2>测试类
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class examp {
//表名
private static final byte[] TABLE_NAME = Bytes.toBytes("HelloTablestore5");
//索引主键
private static final byte[] ROW_KEY = Bytes.toBytes("row_1");
//列族名
private static final byte[] COLUMN_FAMILY_NAME = Bytes.toBytes("f");
//列名
private static final byte[] COLUMN_NAME = Bytes.toBytes("col_1");
//列值
private static final byte[] COLUMN_VALUE = Bytes.toBytes("col_value");
private static final String endPoint = "https://sfsf.cn-sfsf.ots.sfsf.com"; //实例访问地址
private static final String accessId = "LTAIQXTVenM8----"; //这里是阿里云上的AK
private static final String accessKey = "QRSxP8j2aSoMlJFIg------"; //这里是阿里云的AK
private static final String instanceName = "TableStoreDemo"; //ots的实例名
public static void main (String[] args) throws IOException {
helloWorld();
}
private static void helloWorld(){
try {
Configuration config = HBaseConfiguration.create();
//配置tablestore源
config.set("hbase.client.connection.impl","com.alicloud.tablestore.hbase.TablestoreConnection");
config.set("tablestore.client.endpoint",endPoint);
config.set("tablestore.client.instancename",instanceName);
config.set("tablestore.client.accesskeyid",accessId);
config.set("tablestore.client.accesskeysecret",accessKey);
//创建tablestore connection
Connection connection = ConnectionFactory.createConnection(config);
//创建admin负责创建,管理,删除
Admin admin = connection.getAdmin();
//创建一个HTableDescriptor
HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(TABLE_NAME));
/**
* 创建⼀个列族,MaxVersion 和 TimeToLive使⽤默认值,MaxVersion 默认值是 1,
* TimeToLive 默认值是 Integer.INF_MAX
*/
descriptor.addFamily(new HColumnDescriptor(COLUMN_FAMILY_NAME));
System.out.println("Create table " + descriptor.getNameAsString());
//使用admin创建表
admin.createTable(descriptor);
//创建TablestoreTable用于单表的操作
Table table = connection.getTable(TableName.valueOf(TABLE_NAME));
System.out.println("Write one row to the table");
//创建put对象
Put put = new Put(ROW_KEY);
// 增加⼀列,表格存储只⽀持单列族,列族名称在 hbase-site.xml 中配置,如果没
//有配置则默认是“f”,所以写⼊数据时 COLUMN_FAMILY_NAME 可以是空值
put.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME, COLUMN_VALUE);
table.put(put);
//读取ots内一行数据
Result getResult = table.get(new Get(ROW_KEY));
String value = Bytes.toString(getResult.getValue(COLUMN_FAMILY_NAME, COLUMN_NAME));
System.out.println("Get a one row by row key");
System.out.printf("\t%s = %s\n", Bytes.toString(ROW_KEY), value);
//范围读取数据,扫描全表数据
Scan scan = new Scan();
System.out.println("Scan for all rows:");
ResultScanner scanner = table.getScanner(scan);
for (Result row : scanner) {
byte[] valueBytes = row.getValue(COLUMN_FAMILY_NAME,COLUMN_NAME);
System.out.println('\t' + Bytes.toString(valueBytes));
}
//删除表
System.out.println("Delete the table");
// admin.disableTable(table.getName());
//admin.deleteTable(table.getName());
table.close();
admin.close();
connection.close();
} catch (IOException e) {
System.err.println("Exception while running HelloTable store: " + e.toString());
System.exit(1);
}
}
}
然后去到表格存储的控制台:
若无实例需先创建实例
结果: