Phoenix shell:
1、启动:phoenix/bin/sqlline.py hadoop:2181
(说明:phoenix也是基于zookeeper的,启动时后面需要跟zookeeper集群信息,进入后可通过 !list查看jdbc连接地址)
2、帮助:help 查看具体的使用命令
3、查看元数据 :
select TENANT_ID,TABLE_SCHEM,TABLE_NAME,COLUMN_NAME,COLUMN_FAMILY,DATA_TYPE,COLUMN_SIZE,DECIMAL_DIGITS from SYSTEM.CATALOG;
(说明:通过查看元数据可以看到表字段名、字段类型、映射hbase的列族名、字段大小)
4、查看指定表元数据:
select TENANT_ID,TABLE_SCHEM,TABLE_NAME,COLUMN_NAME,COLUMN_FAMILY,DATA_TYPE,COLUMN_SIZE,DECIMAL_DIGITS from SYSTEM.CATALOG where TABLE_NAME='ADS_WTCHK_SALES_DASHBOARD_H_REALTIME_TMP';
(说明:表名大写时,需用 ‘table_name’ 单引号,phoenix对大小写敏感,当hbase和phoenix表名为小写时,select需用 “table_name” )
5、添加数据:
UPSERT INTO "yul_test"("id","BU","StoreNo","TicketNo","PosID","DepartmentNumber","ItemNo","CardNumber","Qty","Price","Amount","TransTime")
VALUES('000001','yulin',27,'etl',334,34342324,'gwet',4554,1323,4543.45,34.1,'dgdg');
(说明:当字段类型为char时需用 '单引号)
6、CREATE INDEX IDX 创建索引:
CREATE INDEX IDX_INSERT_TEST_USERID ON "Insert_test"("UserID");//全局二级索引
CREATE LOCAL INDEX IDX_INSERT_TEST_USERID ON "Insert_Test"("UserID");//本地索引
(说明:同一张表,索引数量不得超过10,索引表越多,插入数据越慢!)
explain //执行计划,在sql语句前加,查看sql准备做些什么。
SELECT /*+ INDEX(my_table my_index) */ v2 FROM my_table WHERE v1 = 'foo';//强制指定索引
DROP INDEX IDX_INSERT_TEST_USERID ON "Insert_test";//删除索引
(注:SELECT字段含有非索引的字段,则索引不会被使用)
7、删除
DELETE FROM SYSTEM.CATALOG where TABLE_NAME='ads_idmapping_paltform_ids'; //删除表元数据,删除时要把hbase表数据清空
ALTER TABLE ADS_WTCHK_SALES_DASHBOARD_D_REALTIME_TMP DROP COLUMN "store_target"; //删除指定字段
(说明:删除表数据和字段时,需要清空当前表数据或者所在字段数据)
8、添加字段
ALTER TABLE my_table ADD dept_name char(50) //添加指定字段
ALTER TABLE ADS_WTCHK_SALES_DASHBOARD_D_TOTAL_REALTIME_TMP ADD "info"."flow_count" DOUBLE VERSIONS=10 //添加字段并加上列族
9、建表实例:
--总total每小时销售
create table ADS_WTCHK_SALES_DASHBOARD_H_TOTAL_REALTIME_TMP(
"info"."total_sales_h" DOUBLE,
"info"."total_txn_h" DOUBLE,
"stat_date" VARCHAR(20) NOT NULL,
CONSTRAINT pk PRIMARY KEY ("stat_date")
) SALT_BUCKETS = 48;
create local index WTCHK_SALES_DASHBOARD_H_TOTAL_REALTIME_TMP ON ADS_WTCHK_SALES_DASHBOARD_H_TOTAL_REALTIME_TMP("stat_date");//本地索引
Phoenix scala连接:
package cn.com.pnshk.sink
import java.sql.{Connection, DriverManager, Statement}
import java.text.SimpleDateFormat
import java.util.{Date, Properties, TimeZone}
import cn.com.pnshk.pojo.SalesDetails
import cn.com.pnshk.utils.SqlUtils
import org.apache.flink.configuration.Configuration
import org.apache.flink.streaming.api.functions.sink.RichSinkFunction
class SinkPhoenix extends RichSinkFunction[SalesDetails]{
private var connection:Connection = null
private var statement:Statement = null
override def open(parameters: Configuration): Unit = {
val prop = new Properties()
val stream = this.getClass.getClassLoader.getResourceAsStream("prop.properties")
prop.load(stream)
val driver = prop.getProperty("org.apache.phoenix.jdbc.PhoenixDriver")
val url = prop.getProperty("jdbc:phoenix:hadoop:2181:/hbase-unsecure")
Class.forName(driver)
connection = DriverManager.getConnection(url)
statement = connection.createStatement()
statement.setQueryTimeout(6000)
}
//每个元素的插入,都要触发一次invoke,这里主要进行invoke插入
override def invoke(sales:SalesDetails): Unit = {
try {
val sqlList = SqlUtils.getSql(sales)
for (sql <- sqlList) {
statement.execute(sql)
connection.commit()
}
val dateFormat: SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
val today = dateFormat.format(new Date())
println("success" + today)
}
catch {
case e: Exception => println(e.getMessage)
}
}
//释放资源
override def close(): Unit = {
if(statement != null){
statement.close()
}
if(connection != null){
connection.close()
}
}
}
(注:上面的RichSinkFunction我是配合Flink的addSink使用)
来源:CSDN
作者:Lone--Wolf
链接:https://blog.csdn.net/qq_39140816/article/details/103909946