表格存储Wide Colum模型使用简介

文章目录

前言

一、表格存储入门

1.创建实例

 2.创建表

二、Java SDK使用介绍

1.官方示例

2.引入库

3.构建AsyncClient 工具类

3.插入数据

4.获取数据

5.官方最佳实际

三、Wide Cloum 模型介绍

四、表设计

总结




前言

因为学过的知识经常忘记,所以就以博客的方式记录下来,偶尔翻看一下加深一下印象。




一、表格存储入门

快速入门,参考官方文档:快速入门 - 表格存储 Tablestore - 阿里云https://help.aliyun.com/document_detail/27286.html

1.创建实例

进入控制台,选择自己合适的地区,创建实例。这里弹出两种购买方式,一种预留模式和按量模式。这里我选择按量模式,因为购买了资源包,一个月1亿的读写CU。预留模式自行研究。两者的区别的话参考官方文档的产品定价。

 2.创建表

创建实例完成后,点击实例进入实例管理页面。

 点击创建数据表

 

输入表名,以及主键名,第一个主键为分区键,默认情况下,阿里云会自动管理分区,如果需要预分区的话要联系阿里云技术支持。目前一个表最多支持4个主键,创建完成后点击确认即可创建表。



二、Java SDK使用介绍

1.官方示例

GitHub - aliyun/tablestore-examples: Example code for aliyun tablestore.Example code for aliyun tablestore. Contribute to aliyun/tablestore-examples development by creating an account on GitHub.https://github.com/aliyun/tablestore-examples



2.引入库

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.jackxue</groupId>
    <artifactId>tablestore-study</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>wide-column</module>
        <module>v2</module>
    </modules>

    <properties>
        <project-sourceEncoding>UTF-8</project-sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.aliyun.openservices</groupId>
            <artifactId>tablestore</artifactId>
            <version>5.4.0</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.25</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
    </dependencies>
    <build>
    </build>

</project>



3.构建AsyncClient 工具类

package com.jackxue.v2.util;


import com.alicloud.openservices.tablestore.AsyncClient;
import com.alicloud.openservices.tablestore.TunnelClient;
import com.google.gson.Gson;
import org.apache.commons.io.IOUtils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class TableStoreUtils {
    private static AsyncClient asyncClient;
    private static TunnelClient tunnelClient;
    private static Conf conf;

    class Conf {
        private String endpoint;
        private String accessId;
        private String accessKey;
        private String instanceName;

        public String getEndpoint() {
            return endpoint;
        }

        public void setEndpoint(String endpoint) {
            this.endpoint = endpoint;
        }

        public String getAccessId() {
            return accessId;
        }

        public void setAccessId(String accessId) {
            this.accessId = accessId;
        }

        public String getAccessKey() {
            return accessKey;
        }

        public void setAccessKey(String accessKey) {
            this.accessKey = accessKey;
        }

        public String getInstanceName() {
            return instanceName;
        }

        public void setInstanceName(String instanceName) {
            this.instanceName = instanceName;
        }
    }
    static {
        FileInputStream fin = null;
        try {
            fin = new FileInputStream(System.getProperty("user.home") + "/" + "tablestoreConf.json");
            String jsonString = IOUtils.toString(fin);
            Gson gson = new Gson();
            conf = gson.fromJson(jsonString, Conf.class);
            asyncClient = new AsyncClient(conf.getEndpoint(),
                    conf.getAccessId(),
                    conf.getAccessKey(),
                    conf.getInstanceName());
            tunnelClient = new TunnelClient(conf.getEndpoint(),
                    conf.getAccessId(),
                    conf.getAccessKey(),
                    conf.getInstanceName());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 获取一部客户端
     * @return
     */
    public static AsyncClient getAsyncClient(){
        return asyncClient;
    }

    /**
     * 关闭
     */
    public static void close(){
        if(asyncClient != null) asyncClient.shutdown();
        if(tunnelClient != null){
            tunnelClient.shutdown();
        }
    }

    public static TunnelClient getTunnelClient() {
        return tunnelClient;
    }
}

3.插入数据

/**
     * 更新设备最新数据
     */
    @Test
    public void test04() throws ParseException, IOException {
        String sensor = "270a";
        BufferedReader bufferedReader = new BufferedReader(new FileReader("E:\\JavaWorkSpaces\\tablestore-study\\sn3.txt"));
        String line;
        bufferedReader.readLine(); //去掉首行
        long currentLine = 0;
        while ((line = bufferedReader.readLine()) != null) {
            currentLine++;
            String[] split = line.split("\t");
            String sn = split[0];
            String parentSn = split[1];
            List<Column> columnList = new ArrayList<>();
            List<Column> columns = Arrays.asList(
                    new Column("parent_sn", ColumnValue.fromString(parentSn)),
                    new Column("parent_type", ColumnValue.fromString("采集器")),
                    new Column("status", ColumnValue.fromString("alert")),
                    new Column("update_time", ColumnValue.fromLong(System.currentTimeMillis())),
                    new Column("INV_ST1", ColumnValue.fromString("value1")),
                    new Column("Et_ge0", ColumnValue.fromString("value2")));
            columnList.addAll(columns);
            for (int j = 0; j < 200; j++) {
                columnList.add(new Column("col" + j, ColumnValue.fromString("col" + j)));
            }
            deviceLatestInfo.updateDeviceLatestInfo(sensor, sn, columnList);
            System.out.println("sn:" + sn + "  parent sn:" + parentSn + " line:" + currentLine);
        }
        bufferedReader.close();
    }
 /**
     * 更新设备最新的数据
     * @param sensor
     * @param sn
     */
    public void updateDeviceLatestInfo(String sensor, String sn, List<Column> columnList){
        RowUpdateChange rowUpdateChange = new RowUpdateChange(tableName, buildPrimaryKey(sensor, sn));
        rowUpdateChange.put(columnList);
        UpdateRowRequest updateRowRequest = new UpdateRowRequest(rowUpdateChange);
        asyncClient.asSyncClient().updateRow(updateRowRequest);
    }
 /**
     * 根据 设备类型和sn号生成主键
     * @param sensor
     * @param sn
     * @return
     */
    public PrimaryKey buildPrimaryKey(String sensor, String sn){
        PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
        primaryKeyBuilder.addPrimaryKeyColumn("sensor", PrimaryKeyValue.fromString(sensor));
        primaryKeyBuilder.addPrimaryKeyColumn("sn", PrimaryKeyValue.fromString(sn));
        return primaryKeyBuilder.build();
    }

4.获取数据

/**
     * 获取设备最新数据
     * @param sensor
     * @param sn
     * @return
     */
    public Row getDeviceLatestInfo(String sensor, String sn){
        SingleRowQueryCriteria rowQueryCriteria = new SingleRowQueryCriteria(tableName, buildPrimaryKey(sensor, sn));
        rowQueryCriteria.setMaxVersions(1);
        GetRowRequest getRowRequest = new GetRowRequest(rowQueryCriteria);
        GetRowResponse getRowResponse = asyncClient.asSyncClient().getRow(getRowRequest);
        return getRowResponse.getRow();
    }

5.官方最佳实际

三、Wide Cloum 模型介绍


 上图为官方的图,这个图非常形象的介绍了这个模式的组成。我感觉和hbase的存储模型类似。Hbase 的RowKey 和Primary Keys 相对应,这个模型感觉把Hbase的rowkey拆分成多个的感觉,其他没有什么太大不同。

四、表设计

在大数据的情况下,数据倾斜应该是影响查询等效率的最大原因之一。所以在选择使用分区键时需要考虑让数据尽量的分布均匀。

热点问题:



这个表设计有很明显的热点问题,每次数据写入都是在表的末尾追加数据,而数据是按照分区键的范围进行分区的,也就是每次数据写入都会写入最后一个分区,而无法把写入负载平衡到多台机器上。

解决方案一:

        使用MachineIp作为分区键

解决方案二:

        使用MachineIp作为分区键,把IP做一次MD5拼接,避免某个IP段热点数据问题,如下图:

  • 控制一个分区的数据大小在10GB以内

  • 不要使用递增的分区键
  • 主键长度不宜过长,控制在1K以内,尽可能小提升查询效率

总结

好记性不如烂笔头,下次把索引和通道总结一下。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值