Apache Phoenix 使用小记

Apache Phoenix 使用小记

  1. phoenix建表语句
    在hbase的中namespace为bigdata中创建一个user表,将id和createTime作为主键ROWKEY,对边进行加盐分为10个Region,对数据进行压缩存储使用snappy的压缩方式
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.做主键的列不能指定列簇

  1. phoenix创建索引
    假设上面user表只有对id作为主键,我们要为user表创建二级索引,索引字段为birthday,createTime,并将name,phone字段带到索引表中,对标进行加盐分为10个region,使用snappy压缩存储。
CREATE INDEX USER_INDEX ON BIGDATA.USER
(
	BIRTHDAY,
	CREATETIME
)
INCLUDE
(
	NAME,
	PHONE
)
SALT_BUCKETS = 10,compression='snappy'

  1. phoenix插入更新语句

    phoenix 没有 UPDATE 也没有 INSERT 只有 UPSERT,并且只能根据主键去更新,也就是说UPSERT后面的字段必须包含主键(rowkey),如果该行不存在则执行插入,存在则更新。

  • UPSERT VALUES 语句:
    插入一条数据
UPSERT INTO BIGDATA.USER(ID,CREATETIME,AGE,NAME) VALUES(1,'2019-08-27',29,'lisa')
  • UPSERT SELECT 语句:
    更新多条数据,将出生日期为2019年8月的人,TYPE都设置为"男神女神"
UPSERT INTO BIGDATA.USER(ID,CREATETIME,TYPE) 
SELECT ID,CREATETIME,'男神女神' 
FROM BIGDATA.USER 
WHERE SUBSTR(BIRTHDAY,0,7)='2019-08'

  1. phoenix删除语句
  • DELETE FROM
    删除出生日期为‘2019-08-12’的人员信息
DELETE FROM USER WHERE BIRTHDAY='2019-08-12'

  1. phoenixJDBC编程
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();
	}
}

你可能感兴趣的:(Phoenix)