rowkey | id | name | age | phone | birthday | createTime | type |
---|---|---|---|---|---|---|---|
加盐码,1,2018-08-20 | 1 | tom | 23 | 135862***** | 1997-01-31 | 2018-08-20 | 好人 |
CREATE TABLE IF NOT EXISTS BIGDATA.USER
(
ID UNSIGNED_INT NOT NULL,
F1.NAME VARCHAR,
F1.AGE UNSIGNED_INT,
F1.PHONE VARCHAR,
F1.BIRTHDAY VARCHAR,
CREATETIME VARCHAR,
TYPE VARCHAR
CONSTRAINT ID_CREATETIME PRIMARY KEY (ID,CREATETIME)
)
SALT_BUCKETS = 10,compression='snappy'
注意:
1.PRIMARY KEY 后面的字段顺序必须和匹配表的字段声名顺序,否则会报异常
例如:PRIMARY KEY (CREATETIME,ID)这样就会报异常
2.做主键的列不能指定列簇
CREATE INDEX USER_INDEX ON BIGDATA.USER
(
BIRTHDAY,
CREATETIME
)
INCLUDE
(
NAME,
PHONE
)
SALT_BUCKETS = 10,compression='snappy'
phoenix插入更新语句
phoenix 没有 UPDATE 也没有 INSERT 只有 UPSERT,并且只能根据主键去更新,也就是说UPSERT后面的字段必须包含主键(rowkey),如果该行不存在则执行插入,存在则更新。
UPSERT INTO BIGDATA.USER(ID,CREATETIME,AGE,NAME) VALUES(1,'2019-08-27',29,'lisa')
UPSERT INTO BIGDATA.USER(ID,CREATETIME,TYPE)
SELECT ID,CREATETIME,'男神女神'
FROM BIGDATA.USER
WHERE SUBSTR(BIRTHDAY,0,7)='2019-08'
DELETE FROM USER WHERE BIRTHDAY='2019-08-12'
public class CURDTest {
private PhoenixConnection conn = null;
private PreparedStatement ps = null;
public CURDTest(){
init();
}
public void init() throws ClassNotFoundException, SQLException {
Class.forName("org.apache.phoenix.jdbc.PhoenixDriver");
Properties pops = new Properties();
pops.setProperty("phoenix.functions.allowUserDefinedFunctions","true");
pops.setProperty("phoenix.mutate.batchSize","15000000");
pops.setProperty("phoenix.mutate.maxSize","2000000");
pops.setProperty("phoenix.mutate.maxSizeBytes","1048576000");
this.conn = (PhoenixConnection)DriverManager.getConnection("jdbc:phoenix:wlt-2,wlt-3,wlt-4:2181",pops);
conn.setAutoCommit(false);
}
public PhoenixConnection getConn(){
return this.conn;
}
public static void main(String[] args) {
CURDTest test = new CURDTest();
PhoenixConnection conn = test.getConn();
PreparedStatement ps = conn.prepareStatement("UPSERT INTO MY_TX_TABLE(id,name,age) VALUES(?,?,?)";
for(int i = 0; i < 10000; i++) {
ps.setString(1,i);
ps.setString(2,"lisa" + i);
ps.setString(3,i%30);
ps.addBatch();
if(i %2000 == 0){
ps.executeBatch();
conn.commit();
}
}
ps.executeBatch();
conn.commit();
ps.close();
conn.close();
}
}