shell腳本基本命令,Phoenix命令及語法

 2023-11-16 阅读 19 评论 0

摘要:Phoenix命令及語法 官網 :http://phoenix.apache.org/language/index.html#select 基本命令: 登錄相應機器 啟動phoenix的客戶端 首先sqlline.py lyy1,lyy2,lyy3,lyy4:2181登錄到Phoenix的shell中,可以使用正常的SQL語句進行操作。 !table查看表信息 !de

Phoenix命令及語法

官網 :http://phoenix.apache.org/language/index.html#select

基本命令:

登錄相應機器
啟動phoenix的客戶端
首先sqlline.py lyy1,lyy2,lyy3,lyy4:2181登錄到Phoenix的shell中,可以使用正常的SQL語句進行操作。
!table查看表信息 
!describe tablename可以查看表字段信息
!history可以查看執行的歷史SQL
!dbinfo
!index tb;查看tb的索引
help查看其他操作

導入數據:

shell腳本基本命令,在phoenix 目錄下執行
hadoop jar /home/phoenix-4.12/phoenix-4.6.0-HBase-1.0-client.jar org.apache.phoenix.mapreduce.CsvBulkLoadTool -t POPULATION -i /datas/us_population.csv
-t :tableName
-i: input file 文件必須在hdfs文件上。

1.插入數據

在Phoenix中是沒有Insert語句的,取而代之的是Upsert語句。Upsert有兩種用法,
分別是:upsert into 和 upsert select

upsert into:
類似于insert into的語句,旨在單條插入外部數據
upsert into tb values(‘ak’,‘hhh’,222)
upsert into tb(stat,city,num) values(‘ak’,‘hhh’,222)

bat批處理文件語法?upsert select:
類似于Hive中的insert select語句,旨在批量插入其他表的數據。
upsert into tb1 (state,city,population) select state,city,population from tb2 where population < 40000;
upsert into tb1 select state,city,population from tb2 where population > 40000;
upsert into tb1 select * from tb2 where population > 40000;
注意:在phoenix中插入語句并不會像傳統數據庫一樣存在重復數據。
因為Phoenix是構建在HBase之上的,也就是必須存在一個主鍵。
后面插入的會覆蓋前面的,但是時間戳不一樣。

2.刪除數據

delete from tb; 清空表中所有記錄,Phoenix中不能使用truncate table tb;
delete from tb where city = ‘kenai’;
drop table tb;刪除表
delete from system.catalog where table_name = ‘int_s6a’;
drop table if exists tb;
drop table my_schema.tb;
drop table my_schema.tb cascade;用于刪除表的同時刪除基于該表的所有視圖。

3.修改數據

由于HBase的主鍵設計,相同rowkey的內容可以直接覆蓋,這就變相的更新了數據。
所以Phoenix的更新操作仍舊是upsert into 和 upsert select
upsert into us_population (state,city,population) values(‘ak’,‘juneau’,40711);

4.查詢數據

虛擬機怎么創建shell腳本,union all, group by, order by, limit 都支持
select * from test limit 1000;
select * from test limit 1000 offset 100;
select full_name from sales_person where ranking >= 5.0 union all select reviewer_name from customer_review where score >= 8.0

5.在Phoenix中是沒有Database的概念的,所有的表都在同一個命名空間。但支持多個命名空間
設置為true,創建的帶有schema的表將映射到一個namespace

phoenix.schema.isNamespaceMappingEnabled
true

6.創建表

A.SALT_BUCKETS(加鹽)
加鹽Salting能夠通過預分區(pre-splitting)數據到多個region中來顯著提升讀寫性能。
本質是在hbase中,rowkey的byte數組的第一個字節位置設定一個系統生成的byte值,
這個byte值是由主鍵生成rowkey的byte數組做一個哈希算法,計算得來的。
Salting之后可以把數據分布到不同的region上,這樣有利于phoenix并發的讀寫操作。

德語命令式語法。SALT_BUCKETS的值范圍在(1 ~ 256):
create table test(host varchar not null primary key, description varchar)salt_buckets=16;

upsert into test (host,description) values (‘192.168.0.1’,‘s1’);
upsert into test (host,description) values (‘192.168.0.2’,‘s2’);
upsert into test (host,description) values (‘192.168.0.3’,‘s3’);

salted table可以自動在每一個rowkey前面加上一個字節,這樣對于一段連續的rowkeys,它們在表中實際存儲時,就被自動地分布到不同的region中去了。
當指定要讀寫該段區間內的數據時,也就避免了讀寫操作都集中在同一個region上。
簡而言之,如果我們用Phoenix創建了一個saltedtable,那么向該表中寫入數據時,
原始的rowkey的前面會被自動地加上一個byte(不同的rowkey會被分配不同的byte),使得連續的rowkeys也能被均勻地分布到多個regions。

B.Pre-split(預分區)
Salting能夠自動的設置表預分區,但是你得去控制表是如何分區的,
所以在建phoenix表時,可以精確的指定要根據什么值來做預分區,比如:
create table test (host varchar not null primary key, description varchar) split on (‘cs’,‘eu’,‘na’);

C.使用多列族
列族包含相關的數據都在獨立的文件中,在Phoenix設置多個列族可以提高查詢性能。
創建兩個列族:
create table test (
mykey varchar not null primary key,
a.col1 varchar,
a.col2 varchar,
b.col3 varchar
);
upsert into test values (‘key1’,‘a1’,‘b1’,‘c1’);
upsert into test values (‘key2’,‘a2’,‘b2’,‘c2’);

D.使用壓縮
create table test (host varchar not null primary key, description varchar) compression=‘snappy’;

7.創建視圖,刪除視圖

create view “my_hbase_table”( k varchar primary key, “v” unsigned_long) default_column_family=‘a’;
create view my_view ( new_col smallint ) as select * from my_table where k = 100;
create view my_view_on_view as select * from my_view where new_col > 70
create view v1 as select * from test where description in (‘s1’,‘s2’,‘s3’)

drop view my_view
drop view if exists my_schema.my_view
drop view if exists my_schema.my_view cascade

8.創建二級索引

支持可變數據和不可變數據(數據插入后不再更新)上建立二級索引
create index my_idx on sales.opportunity(last_updated_date desc)
create index my_idx on log.event(created_date desc) include (name, payload) salt_buckets=10
create index if not exists my_comp_idx on server_metrics ( gc_time desc, created_date desc )
data_block_encoding=‘none’,versions=?,max_filesize=2000000 split on (?, ?, ?)
create index my_idx on sales.opportunity(upper(contact_name))
create index test_index on test (host) include (description);

刪除索引:
drop index my_idx on sales.opportunity
drop index if exists my_idx on server_metrics
drop index if exists xdgl_acct_fee_index on xdgl_acct_fee

默認是可變表,手動創建不可變表
create table hao2 (k varchar primary key, v varchar) immutable_rows=true;
alter table HAO2 set IMMUTABLE_ROWS = false; 修改為可變
alter index index1 on tb rebuild;索引重建是把索引表清空后重新裝配數據。

Global Indexing多讀少寫,適合條件較少
create index my_index on items(price);
調用方法:
1.強制索引
select /*+ index(items my_index) */ * from items where price=0.8824734;
drop index my_name on usertable;

2.覆蓋索引 Covered Indexes,需要include包含需要返回數據結果的列。
create index index1_c on hao1 (age) include(name); name已經被緩存在這張索引表里了。
對于select name from hao1 where age=2,查詢效率和速度最快
select * from hao1 where age =2,其他列不在索引表內,會全表掃描

Local Indexing寫多讀少,不是索引字段索引表也會被使用,索引數據和真實數據存儲在同一臺機器上(
create local index index3_l_name on hao1 (name);

異步創建索引,創建的索引表中不會有數據,單獨使用命令行工具來執行數據的創建
create index index1_c on hao1 (age) include(name) async;
hbase org.apache.phoenix.mapreduce.index.indextool
–schema my_schema --data-table my_table --index-table async_idx
–output-path async_idx_hfiles

9.與現有的HBase表關聯

首先創建一張HBase表,再創建的Phoenix表,表名必須和HBase表名一致即可。
create ‘stu’ ,‘cf1’,‘cf2’
put ‘stu’, ‘key1’,‘cf1:name’,‘luozhao’
put ‘stu’, ‘key1’,‘cf1:sex’,‘man’
put ‘stu’, ‘key1’,‘cf2:age’,‘24’
put ‘stu’, ‘key1’,‘cf2:adress’,‘cqupt’

create table “stu” (
id VARCHAR NOT NULL PRIMARY KEY ,
“cf1”.“name” VARCHAR ,
“cf1”.“sex” VARCHAR ,
“cf2”.“age” VARCHAR ,
“cf2”.“adress” VARCHAR );
upsert into “stu”(id,“cf1”.“name”,“cf1”.“sex”,“cf2”.“age”,“cf2”.“adress”) values(‘key6’,‘zkk’,‘man’,‘111’,‘Beijing’);

select * from “stu”;會發現兩張表是數據同步的。
這里查詢表名需要用雙引號括起來,強制不轉換為大寫。
這里一定要注意的是表名和列族以及列名需要用雙引號括起來,因為HBase是區分大小寫的,
如果不用雙引號括起來的話Phoenix在創建表的時候會自動將小寫轉換為大寫字母

10.在Spark運行環境中添加Phoenix依賴

spark-env.sh添加如下代碼:
#添加Phoenix依賴
for file in (find/opt/hbase?1.2.4/lib∣grepphoenix)doSPARKDISTCLASSPATH="(find /opt/hbase-1.2.4/lib |grep phoenix) do SPARK_DIST_CLASSPATH="(find/opt/hbase?1.2.4/libgrepphoenix)doSPARKD?ISTC?LASSPATH="SPARK_DIST_CLASSPATH:$file"
done
export SPARK_DIST_CLASSPATH
這樣每次啟動spark任務都會將phoenix的jar包添加到classpath了

總的來說:官網講解很詳細也很形象的
這里面涉及的問題只是單方面Phoenix的語法
如果跟hive和hbase之前的搭建,還涉及到很多問題,語法可能有所變化,

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/173654.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息