CREATE TABLE input(clientGroup String, clientId int, loanAmount double, applicantIncome double);
INSERT INTO input VALUES ("Client Group 1", 0, 4583, 128),("Client Group 1", 1, 3000, 66),("Client Group 1", 2, 2583, 120),("Client Group 1", 3, 6000, 141),("Client Group 1", 4, 2333, 95);
INSERT INTO input VALUES ("Client Group 2", 0, 2571, 28),("Client Group 2", 1, 6000, 16),("Client Group 2", 2, 1583, 1200),("Client Group 2", 3, 4500, 1410),("Client Group 2", 4, 1133, 5);
CREATE TABLE result AS PREPARE *, kmeansCluster(4, 1000L, loanAmount, applicantIncome) as cluster FROM input PARTITION BY clientGroup;
Table input = SELECT * FROM input;
Table result = SELECT * FROM result;
// input =
// +--------------------------------------------------+
// | input |
// +--------------+--------+----------+---------------+
// |clientGroup |clientId|loanAmount|applicantIncome|
// |String |Integer |Double |Double |
// +--------------+--------+----------+---------------+
// |Client Group 1|0 |4583.0 |128.0 |
// |Client Group 1|1 |3000.0 |66.0 |
// |Client Group 1|2 |2583.0 |120.0 |
// |Client Group 1|3 |6000.0 |141.0 |
// |Client Group 1|4 |2333.0 |95.0 |
// |Client Group 2|0 |2571.0 |28.0 |
// |Client Group 2|1 |6000.0 |16.0 |
// |Client Group 2|2 |1583.0 |1200.0 |
// |Client Group 2|3 |4500.0 |1410.0 |
// |Client Group 2|4 |1133.0 |5.0 |
// +--------------+--------+----------+---------------+
//
// result =
// +----------------------------------------------------------+
// | result |
// +--------------+--------+----------+---------------+-------+
// |clientGroup |clientId|loanAmount|applicantIncome|cluster|
// |String |Integer |Double |Double |Integer|
// +--------------+--------+----------+---------------+-------+
// |Client Group 1|0 |4583.0 |128.0 |1 |
// |Client Group 1|1 |3000.0 |66.0 |2 |
// |Client Group 1|2 |2583.0 |120.0 |0 |
// |Client Group 1|3 |6000.0 |141.0 |1 |
// |Client Group 1|4 |2333.0 |95.0 |3 |
// |Client Group 2|0 |2571.0 |28.0 |1 |
// |Client Group 2|1 |6000.0 |16.0 |2 |
// |Client Group 2|2 |1583.0 |1200.0 |0 |
// |Client Group 2|3 |4500.0 |1410.0 |2 |
// |Client Group 2|4 |1133.0 |5.0 |3 |
// +--------------+--------+----------+---------------+-------+
//