目录

schema操作

表结构操作

创建一个表

新增一列phone

删除一列phone

删除表drop table "foo"."student";

表数据操作(增删改查)

新增数据

修改数据

删除数据

视图

新增视图

修改视图

删除视图


schema操作

创建schema

create schema "foo";

切换schema
use "foo";

删除schema
drop schema if exists "foo";

0: jdbc:phoenix:thin:url=http://hb-bp1ya00iwt> create schema "foo";
No rows affected (0.234 seconds)
0: jdbc:phoenix:thin:url=http://hb-bp1ya00iwt> use "foo";
No rows affected (0.007 seconds)
0: jdbc:phoenix:thin:url=http://hb-bp1ya00iwt> drop schema if exists "foo";
No rows affected (0.24 seconds)

表结构操作

创建一个表

create table "foo"."student"(id varchar primary key,name varchar,sex varchar,age varchar);

新增一列phone

alter table "foo"."student" add "phone" varchar(12);

删除一列phone

alter table "foo"."student" drop column "phone";  

删除表
drop table "foo"."student";

0: jdbc:phoenix:thin:url=http://hb-bp1ya00iwt> create table "foo"."student"(id varchar primary key,name varchar,sex varchar,age varchar);
No rows affected (0.784 seconds)
0: jdbc:phoenix:thin:url=http://hb-bp1ya00iwt> alter table "foo"."student" add "phone" varchar(12);
No rows affected (6.009 seconds)
0: jdbc:phoenix:thin:url=http://hb-bp1ya00iwt> alter table "foo"."student" drop column "phone"; 
No rows affected (0.039 seconds)
0: jdbc:phoenix:thin:url=http://hb-bp1ya00iwt> drop table "foo"."student";
No rows affected (0.954 seconds)

表数据操作(增删改查)

新增数据

upsert into  "foo"."student" values('1001','zhangsan','male','23');

upsert into  "foo"."student" values('1002','lisi','male','45');

select * from "foo"."student";

upsert时候括号里面只能用单引号,但是如果要用SEQUENCE,并且名称是小写,就要用双引号。

0: jdbc:phoenix:thin:url=http://hb-bp1ya00iwt> upsert into  "foo"."student" values('1001','zhangsan','male','23');
1 row affected (0.021 seconds)
0: jdbc:phoenix:thin:url=http://hb-bp1ya00iwt> upsert into  "foo"."student" values('1002','lisi','male','45');
1 row affected (0.007 seconds)
0: jdbc:phoenix:thin:url=http://hb-bp1ya00iwt> select * from "foo"."student";
+-------+-----------+-------+------+
|  ID   |   NAME    |  SEX  | AGE  |
+-------+-----------+-------+------+
| 1001  | zhangsan  | male  | 23   |
| 1002  | lisi      | male  | 45   |
+-------+-----------+-------+------+
2 rows selected (0.019 seconds)

修改数据

修改id为1001的数据age为32

0: jdbc:phoenix:thin:url=http://hb-bp1ya00iwt> upsert into  "foo"."student" values('1001','zhangsan','male','32');
1 row affected (0.014 seconds)
0: jdbc:phoenix:thin:url=http://hb-bp1ya00iwt> select * from "foo"."student";
+-------+-----------+-------+------+
|  ID   |   NAME    |  SEX  | AGE  |
+-------+-----------+-------+------+
| 1001  | zhangsan  | male  | 32   |
| 1002  | lisi      | male  | 45   |
+-------+-----------+-------+------+
2 rows selected (0.026 seconds)

在phoenix创建hbase不存在的表时,默认列族为0

hbase(main):038:0> scan 'foo:student'
ROW                                                   COLUMN+CELL                                                                                                                                                  
 1001                                                 column=0:\x00\x00\x00\x00, timestamp=1651214690958, value=x                                                                                                  
 1001                                                 column=0:\x80\x0B, timestamp=1651214690958, value=zhangsan                                                                                                   
 1001                                                 column=0:\x80\x0C, timestamp=1651214690958, value=male                                                                                                       
 1001                                                 column=0:\x80\x0D, timestamp=1651214690958, value=32                                                                                                         
 1002                                                 column=0:\x00\x00\x00\x00, timestamp=1651214558037, value=x                                                                                                  
 1002                                                 column=0:\x80\x0B, timestamp=1651214558037, value=lisi                                                                                                       
 1002                                                 column=0:\x80\x0C, timestamp=1651214558037, value=male                                                                                                       
 1002                                                 column=0:\x80\x0D, timestamp=1651214558037, value=45                                                                                                         
2 row(s)
Took 0.0378 seconds                                                                                                                                                                                                
hbase(main):039:0> describe 'foo:student'
Table foo:student is ENABLED                                                                                                                                                                                       
foo:student, {TABLE_ATTRIBUTES => {coprocessor$1 => '|org.apache.phoenix.coprocessor.GetRegionObserver|805306366|', coprocessor$2 => '|org.apache.phoenix.coprocessor.ScanRegionObserver|805306366|', coprocessor$3
 => '|org.apache.phoenix.coprocessor.UngroupedAggregateRegionObserver|805306366|', coprocessor$4 => '|org.apache.phoenix.coprocessor.GroupedAggregateRegionObserver|805306366|', coprocessor$5 => '|org.apache.phoe
nix.coprocessor.ServerCachingEndpointImpl|805306366|', coprocessor$6 => '|org.apache.phoenix.hbase.index.Indexer|805306366|index.builder=org.apache.phoenix.index.PhoenixIndexBuilder,org.apache.hadoop.hbase.index
.codec.class=org.apache.phoenix.index.PhoenixIndexCodec'}                                                                                                                                                          
COLUMN FAMILIES DESCRIPTION                                                                                                                                                                                        
{NAME => '0', VERSIONS => '1', EVICT_BLOCKS_ON_CLOSE => 'false', NEW_VERSION_BEHAVIOR => 'false', KEEP_DELETED_CELLS => 'FALSE', CACHE_DATA_ON_WRITE => 'false', DATA_BLOCK_ENCODING => 'FAST_DIFF', TTL => 'FOREVE
R', MIN_VERSIONS => '0', REPLICATION_SCOPE => '0', BLOOMFILTER => 'ROW', CACHE_INDEX_ON_WRITE => 'false', IN_MEMORY => 'false', CACHE_BLOOMS_ON_WRITE => 'false', PREFETCH_BLOCKS_ON_OPEN => 'false', COMPRESSION =
> 'NONE', BLOCKCACHE => 'true', BLOCKSIZE => '65536'}                                                                                                                                                              
1 row(s)
Took 0.0443 seconds                          

删除数据

delete from 'foo'.'student';

0: jdbc:phoenix:thin:url=http://hb-bp1ya00iwt> delete from "foo"."student";
2 rows affected (0.019 seconds)
0: jdbc:phoenix:thin:url=http://hb-bp1ya00iwt> select * from "foo"."student";
+-----+-------+------+------+
| ID  | NAME  | SEX  | AGE  |
+-----+-------+------+------+
+-----+-------+------+------+
No rows selected (0.014 seconds)

视图

新增视图

CREATE VIEW "foo"."foo_student_view" AS SELECT * FROM "foo"."student";

0: jdbc:phoenix:thin:url=http://hb-bp1ya00iwt> CREATE VIEW "foo"."foo_student_view" AS SELECT * FROM "foo"."student"; 
No rows affected (0.042 seconds)
0: jdbc:phoenix:thin:url=http://hb-bp1ya00iwt> select * from "foo"."foo_student_view";
+-------+-----------+-------+------+
|  ID   |   NAME    |  SEX  | AGE  |
+-------+-----------+-------+------+
| 1001  | zhangsan  | male  | 32   |
| 1002  | lisi      | male  | 45   |
+-------+-----------+-------+------+
2 rows selected (0.02 seconds)

修改视图

alter view "foo"."foo_student_view" drop column sex;

0: jdbc:phoenix:thin:url=http://hb-bp1ya00iwt> alter view "foo"."foo_student_view" drop column sex;
No rows affected (0.012 seconds)
0: jdbc:phoenix:thin:url=http://hb-bp1ya00iwt> select * from "foo"."foo_student_view";
+-------+-----------+------+
|  ID   |   NAME    | AGE  |
+-------+-----------+------+
| 1001  | zhangsan  | 32   |
| 1002  | lisi      | 45   |
+-------+-----------+------+
2 rows selected (0.065 seconds)

删除视图

drop view "foo"."foo_student_view"; 

0: jdbc:phoenix:thin:url=http://hb-bp1ya00iwt> drop view "foo"."foo_student_view"; 
No rows affected (0.012 seconds)

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐