【问题标题】:Pentaho (spoon): generate primary key for many rows in SQLPentaho (spoon):在 SQL 中为多行生成主键
【发布时间】:2018-06-18 16:24:15
【问题描述】:

我有一个案例:将数据从 access 数据库传输到 SQL。我遇到了如何在 SQL 中创建大量新主键的问题。

当我传输 previos 表时,我使用生成器随机值来生成随机字符串。新钥匙我削减到 5 位数。

enter image description here

但现在我有很多行 900 000 并且包含大约 200 个新键。

我无法使用添加序列,因为主键的长度为 5 个数字 (char(5))。

我想我可以使用 java 创建一个密钥并找到这个脚本,但我不明白它是如何工作的。

enter image description here

enter image description here

import java.security.SecureRandom;
import java.util.Locale;
import java.util.Objects;
import java.util.Random;
public class RandomString {
    public String nextString() {
        for (int idx = 0; idx < buf.length; ++idx)
            buf[idx] = symbols[random.nextInt(symbols.length)];
        return new String(buf);
    }
    public static final String upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static final String lower = upper.toLowerCase(Locale.ROOT);
    public static final String digits = "0123456789";
    public static final String alphanum = upper + lower + digits;
    private final Random random;
    private final char[] symbols;
    private final char[] buf;
    public RandomString(int length, Random random, String symbols) {
        if (length < 1) throw new IllegalArgumentException();
        if (symbols.length() < 2) throw new IllegalArgumentException();
        this.random = Objects.requireNonNull(random);
        this.symbols = symbols.toCharArray();
        this.buf = new char[length];
    }

enter image description here

我的数据库没有任何序列可以使用 Add sequence 和 Use DB to get sequence,但是有一个生成键的功能。我怎样才能使用这个功能?还是不行?

这是丢失数据的统计数据。在这种情况下我可以创建一个循环来重新生成密钥吗?

enter image description here

【问题讨论】:

    标签: sql primary-key pentaho kettle spoon


    【解决方案1】:

    不要创建随机键,而是使用序列将序列转换为具有高基数的数字(例如使用 javascript id_as_char = id_as_int.toString(36))。

    当然,你还有其他方法!

    【讨论】:

      【解决方案2】:

      感谢您的帮助。我在字符串前添加了一行以添加“0”。

      id = id.toString(36)
      while (id.length < (5 || 2)) {id = "0" + id;}
      

      00zzm 00zz 00zzo 00zzp 00zzq 00zz 00zz 00zzt 00zzu 00zv 00zzw 00zzx 00zzy 00zzz 01000 01001 01002 01003 01004 01005 01006 01007 01008 01009 0100a 0100b

      是这样的https://gist.github.com/endel/321925f6cafa25bbfbde

      Number.prototype.pad = function(size) {
        var s = String(this);
        while (s.length < (size || 2)) {s = "0" + s;}
        return s;
      }
      
      (1).pad(3) // => "001"
      (10).pad(3) // => "010"
      (100).pad(3) // => "100"
      

      【讨论】:

        【解决方案3】:

        为什么不将序列处理留给 sql server?您使用的是哪个数据库? 如果你使用 mysql 或 postgres,你可以创建一个序列类型的新列,这个序列唯一连续编号的创建将在 db 级别处理得更好。

        https://www.postgresql.org/docs/9.5/static/datatype-numeric.html#DATATYPE-SERIAL

        【讨论】:

        • 我使用 MS SQL 并且无法创建新列,因为我有一个具体的数据库。我不创建数据库,我尝试填写。
        • 在这种情况下,建议将 uuid 创建为主键,从而减少在唯一键之间发现冲突的选项。使用“生成随机值”步骤,并选择 UUID4
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多