检查表是否存在并将数据加载到Hbase和HIve表中

时间:2014-05-21 04:25:41

标签: hadoop hive hbase file-exists

我有HDFS数据。我想将这些数据加载到hbase和hive表中。 我编写了一个bash shell脚本,其中我编写了一个猪脚本来将数据表格加载到HBASE,还编写了hive脚本,将数据从HDFS加载到HIVE表,这些都工作得很好。这里我的HDFS数据文件是我将所有数据文件加载到单个hbase和hive表中。

现在我的查询是假设我在HDFS目录中收到更多数据文件,如果我再次运行shell脚本,它将再次创建具有相同名称的hbase和hive表,并且告诉表已经存在。我如何编写一个hive和hbase查询,以便它首先检查表是否存在,如果表不存在,它将第一次创建表并将数据从HDFS加载到HBASE&蜂巢表。如果表已经存在,那么它只会将数据插入到现有的hbase和hive表中。它不应该覆盖表中存在的数据alreday。 如何做到这一点?

以下是我的脚本文件:myScript.sh

echo "create 'goodtable','gt'" | hbase shell    
pig -f a.pig -param input=/user/user/d/
hive -f h.hql

a.pig:

G = LOAD '$input' USING PigStorage(',') as (c1:chararray, c2:chararray,c3:chararray,c4:chararray,c5:chararray);
STORE G INTO 'hbase://goodtable' USING org.apache.pig.backend.hadoop.hbase.HBaseStorage('gt:name gt:state gt:phone_no gt:gender');

h.hql:

create external table hive_table(
id int,
name string,
state string,
phone_no int,
gender string) row format delimited fields terminated by ',' stored as textfile;
LOAD DATA INPATH '/user/user/d/' INTO TABLE hive_table;

3 个答案:

答案 0 :(得分:0)

对于HIVE,您可以在IF NOT EXISTS语句中添加命令CREATE TABLE。请参阅documentation

我在Hbase上没有多少经验,但我相信您可以使用EXISTS table_name命令来检查表是否存在,然后create表是不是&# 39; t存在。见here

答案 1 :(得分:0)

@visakh是正确的 - 您可以通过输入HBase shell并键入:exists '<tablename>

来查看HBase中是否存在表格

为了在不以交互方式进入HBase shell的情况下执行此操作,您可以创建一个简单的ruby脚本,如下所示:

   exists 'mytable'
   exit

假设您将其保存到名为tabletest.rb的文件中。然后,您可以通过调用hbase shell tabletest.rb来执行此脚本。这将创建以下输出,然后您可以从shell脚本中解析:

    Table tableisthere does exist                                                           
    0 row(s) in 0.9830 seconds

OR

    Table tableisNOTthere does not exist                                                           
    0 row(s) in 0.9830 seconds

为“all in one”脚本添加更多详细信息:

或者,您可以在ruby中创建一个更高级的脚本来检查表是否存在,然后在需要时创建它 - 这是从ruby脚本中调用HBaseAdmin java api完成的。

conf = HBaseConfiguration.new
hbaseAdmin = HBaseAdmin.new(conf)

if !hbaseAdmin.tableExists('mytable')
    hbaseAdmin.createTable('mytable',...)
end

答案 2 :(得分:0)

我只是想为HBase添加一个例子,因为之前已经涵盖了Hive:

if [[ $(echo "exists 'goodtable'" | hbase shell | grep 'not exist') ]]; 
then 
    echo "create 'goodtable','gt'" | hbase shell;   
fi