Hive訪問(wèn)TableStore數(shù)據(jù)
本文通過(guò)示例為您介紹EMR Hive作業(yè)如何處理TableStore中的數(shù)據(jù)。
前提條件
已創(chuàng)建DataLake集群,詳情請(qǐng)參見(jiàn)創(chuàng)建集群。
已登錄集群,詳情請(qǐng)參見(jiàn)登錄集群。
已獲取下方JAR包并上傳到集群。
JAR包名稱
獲取方法
參考下載鏈接
emr-tablestore-X.X.X.jar
Maven庫(kù)中下載:emr-tablestore。
tablestore-Y.Y.Y-jar-with-dependencies.jar
Maven庫(kù)中下載:tablestore。
重要需要下載jar-with-dependencies。
使用限制
本文操作僅適用于DataLake集群。
DataLake集群和TableStore實(shí)例須在相同地域下,通過(guò)EMR VPC內(nèi)網(wǎng)可以訪問(wèn)TableStore。
操作步驟
在EMR Master節(jié)點(diǎn)上創(chuàng)建一個(gè)目錄,同時(shí)將Hive訪問(wèn)TableStore所需的JAR包復(fù)制到該目錄。
mkdir -p /path/to/tablestore/jars cp emr-tablestore-2.2.0.jar tablestore-5.13.11-jar-with-dependencies.jar \ /path/to/tablestore/jars
在EMR控制臺(tái),修改Hive服務(wù)配置,保存并開(kāi)啟自動(dòng)配置更新。
修改配置項(xiàng)詳情請(qǐng)參見(jiàn)修改配置項(xiàng)。
配置文件
配置項(xiàng)
修改內(nèi)容
hive-env.sh
hive_aux_jars_path
配置項(xiàng)末尾添加,/path/to/tablestore/jars。
hive-site.xml
hive.aux.jars.path
配置項(xiàng)末尾添加,/path/to/tablestore/jars。
在TableStore控制臺(tái)創(chuàng)建數(shù)據(jù)表,詳情請(qǐng)參見(jiàn)通過(guò)控制臺(tái)使用寬表模型。
本文示例中創(chuàng)建的數(shù)據(jù)表名稱為pet,表主鍵為name。
執(zhí)行以下命令,進(jìn)入Hive命令行。
hive
說(shuō)明如果使用Beeline,則需要重啟HiveServer2服務(wù)。
在Hive中創(chuàng)建并查詢表數(shù)據(jù)。
執(zhí)行以下命令,創(chuàng)建Hive表。
CREATE EXTERNAL TABLE pet (name STRING, owner STRING, species STRING, sex STRING, birth STRING, death STRING) STORED BY 'com.aliyun.openservices.tablestore.hive.TableStoreStorageHandler' WITH SERDEPROPERTIES( "tablestore.columns.mapping"="name,owner,species,sex,birth,death") TBLPROPERTIES ( "tablestore.endpoint"="https://<instance_name>.<region>.vpc.tablestore.aliyuncs.com", "tablestore.access_key_id"="<yourAccesskeyId>", "tablestore.access_key_secret"="<yourAccesskeyKey>", "tablestore.table.name"="pet");
執(zhí)行以下命令,向表中插入數(shù)據(jù)。
INSERT INTO pet VALUES("Fluffy", "Harold", "cat", "f", "1993-02-04", null); INSERT INTO pet VALUES("Claws", "Gwen", "cat", "m", "1994-03-17", null); INSERT INTO pet VALUES("Buffy", "Harold", "dog", "f", "1989-05-13", null); INSERT INTO pet VALUES("Fang", "Benny", "dog", "m", "1990-08-27", null); INSERT INTO pet VALUES("Bowser", "Diane", "dog", "m", "1979-08-31", "1995-07-29"); INSERT INTO pet VALUES("Chirpy", "Gwen", "bird", "f", "1998-09-11", null); INSERT INTO pet VALUES("Whistler", "Gwen", "bird", null, "1997-12-09", null); INSERT INTO pet VALUES("Slim", "Benny", "snake", "m", "1996-04-29", null); INSERT INTO pet VALUES("Puffball", "Diane", "hamster", "f", "1999-03-30", null);
執(zhí)行以下命令,查詢數(shù)據(jù)。
SELECT * FROM pet;
返回信息如下所示。
OK Bowser Diane dog m 1979-08-31 1995-07-29 Buffy Harold dog f 1989-05-13 NULL Chirpy Gwen bird f 1998-09-11 NULL Claws Gwen cat m 1994-03-17 NULL Fang Benny dog m 1990-08-27 NULL Fluffy Harold cat f 1993-02-04 NULL Puffball Diane hamster f 1999-03-30 NULL Slim Benny snake m 1996-04-29 NULL Whistler Gwen bird NULL 1997-12-09 NULL Time taken: 1.731 seconds, Fetched: 9 row(s)