|
1 |
| -## Example 1: Create and query SparkSQL table map to a new HBase table (multiple columns map to hbase table rowkey) |
2 |
| -In this example, we create a new SparkSQL table and map it to a new HBase table with multiple column in rowkey. |
| 1 | +## Example 2: Create and query SparkSQL table map to existing Hbase table |
| 2 | +In this example, we create SparkSQL table and map it to a existing HBase table. (a single column map to hbase rowkey) |
3 | 3 |
|
4 | 4 | Steps:
|
5 |
| - |
6 |
| -(1) Create table in hbase-sql shell: |
7 |
| -``` |
8 |
| -$SPARK_HBASE_Home/bin/hbase-sql |
9 |
| -CREATE TABLE teacher(grade int, class int, subject string, teacher_name string, teacher_age int, PRIMARY KEY (grade, class, subject)) MAPPED BY (hbase_teacher, COLS=[teacher_name=teacher.name, teacher_age=teacher.age]); |
| 5 | +(1) Create table and populate data in HBase shell |
10 | 6 | ```
|
| 7 | +$HBase_Home/bin/hbase shell |
| 8 | +create 'hbase_numbers', 'f' |
| 9 | +for i in '1'..'100' do for j in '1'..'2' do put 'hbase_numbers', "row#{i}", "f:c#{j}", "#{i}#{j}" end end |
| 10 | +``` |
11 | 11 |
|
12 |
| -This command will create following tables: |
13 |
| -Tables : |
14 |
| - |
15 |
| - spark : teacher |
16 |
| - |
17 |
| - hbase : hbase_teacher |
18 |
| - |
19 |
| -Fields : |
20 |
| - |
21 |
| - [grade,int] |
22 |
| - |
23 |
| - [class,int] |
24 |
| - |
25 |
| - [subject,string] |
26 |
| - |
27 |
| - [teacher_name,string] |
28 |
| - |
29 |
| - [teacher_age,int] |
30 |
| - |
31 |
| - key columns : grade,class,subject |
32 |
| - non-key colums: teacher_name, teacher_age |
33 |
| - |
34 |
| -(2) Load data from a csv data file: |
| 12 | +(2) Map hbase table with sparksql table in hbase-sql shell |
35 | 13 | ```
|
36 |
| -LOAD DATA INPATH './examples/teacher1k.csv' INTO TABLE teacher FIELDS TERMINATED BY "," ; |
| 14 | +$SPARK_HBASE_Home/bin/hbase-sql |
| 15 | +CREATE TABLE numbers(rowkey STRING, a STRING, b STRING, PRIMARY KEY (rowkey)) MAPPED BY (hbase_numbers, COLS=[a=f.c1, b=f.c2]); |
37 | 16 | ```
|
38 | 17 |
|
39 |
| -(3) Query : |
| 18 | +(3) Query: |
40 | 19 | ```
|
41 |
| - // test where |
42 |
| - (1) select teacher_name,teacher_age from teacher where teacher_age > 25; |
43 |
| -
|
44 |
| - // test like in |
45 |
| - (2) select teacher_name,teacher_age,subject from teacher where teacher_name is not null and teacher_name like 'teacher_2_3%' and teacher_age not in (20,21,22,23,24,25) |
46 |
| -
|
47 |
| - // test subquery |
48 |
| - (3) select t1.teacher_name,t1.teacher_age from (select * from teacher where teacher_name like 'teacher_2_3%') t1 where t1.teacher_age < 25 |
49 |
| -
|
50 |
| - //test group by |
51 |
| - (4) select teacher_name, sum(teacher_age) from teacher where grade=1 group by teacher_name |
| 20 | + // test count * |
| 21 | + (1) select count(*) from numbers |
52 | 22 |
|
53 |
| - //test join |
54 |
| - (5) select t1.teacher_name, t2.subject, t1.teacher_age from (select teacher_name, teacher_age from teacher where teacher_age >=26 ) t1 join (select teacher_name, subject from teacher where teacher_name like 'teacher_2_3%')t2 on t1.teacher_name=t2.teacher_name |
| 23 | + // test group by |
| 24 | + (2) select a, b from numbers where b > "980" |
55 | 25 | ```
|
0 commit comments