impala中使用复杂类型(Hive):
    如果Hive中创建的表带有复杂类型(array,struct,map),且储存格式(stored as textfile)为text或者默认,那么在impala中将无法查询到该表
解决办法:
    另建一张字段一致的表,将stored as textfile改为stored as parquet,再将源表数据插入(insert into tablename2 select * from tablename1),这张表即可在impala中查询。

查询方法:
    impala 和hive不同,对array,map,struct等复杂类型不使用explode,而使用如下方法:
select order_id,rooms.room_id, days.day_id,days.price from test2,test2.rooms,test2.rooms.days;
看起来是把一个复杂类型当作子表,进行join的查询
表结构:
test2 (
   order_id string,
   rooms array<struct<
         room_id:string,
         days:array<struct<day_id:string,price:int>>
         >
   >
)

Impala与HBase整合:
Impala与HBase整合,需要将HBase的RowKey和列映射到Impala的Table字段中。Impala使用Hive的Metastore来存储元数据信息,与Hive类似,在于HBase进行整合时,也是通过外部表(EXTERNAL)的方式来实现。

在HBase中创建表:

...
tname = TableName.valueOf("students");
HTableDescriptor tDescriptor = new HTableDescriptor(tname);
HColumnDescriptor famliy = new HColumnDescriptor("core");
tDescriptor.addFamily(famliy);
admin.createTable(tDescriptor);
//添加列:
...
HTable htable = (HTable) connection.getTable(tname);
//不要自动清理缓冲区
htable.setAutoFlush(false);
for (int i = 1; i < 50; i++) {
Put put = new Put(Bytes.toBytes("lisi" + format.format(i)));
//关闭写前日志
put.setWriteToWAL(false); put.addColumn(Bytes.toBytes("core"), Bytes.toBytes("math"), Bytes.toBytes(format.format(i)));
put.addColumn(Bytes.toBytes("core"), Bytes.toBytes("english"), Bytes.toBytes(format.format(Math.random() * i)));
put.addColumn(Bytes.toBytes("core"), Bytes.toBytes("chinese"), Bytes.toBytes(format.format(Math.random() * i)));
htable.put(put);
if (i % 2000 == 0) {
htable.flushCommits();
}
}

部分代码

在Hive中创建外部表:

...
state.execute("create external table if not exists students (" +
"user_name string, " +
"core_math string, " +
"core_english string, " +
"core_chinese string )" +
"row format serde 'org.apache.hadoop.hive.hbase.HBaseSerDe' " +
"stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' " +
"with serdeproperties ('hbase.columns.mapping'=':key,core:math,core:english,core:chinese') " +
"tblproperties('hbase.table.name'='students')");
...

部分代码

上面DDL语句中,在WITH SERDEPROPERTIES选项中指定Hive外部表字段到HBase列的映射,其中“:key”对应于HBase中的RowKey,名称为“lisi****”,其余的就是列簇info中的列名。最后在TBLPROPERTIES中指定了HBase中要进行映射的表名。

在Impala中同步元数据:
Impala共享Hive的Metastore,这时需要同步元数据,可以通过在Impala Shell中执行同步命令:
#INVALIDATE METADATA;
然后,就可以查看到映射HBase中表了

注意: impala支持select / insert , 不支持 delete/update单行语句,Impala不支持修改非kudu表,其他操作与Hive类似

Java操作:
maven 依赖:

        <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>com.cloudera.impala</groupId>
<artifactId>jdbc</artifactId>
<version>2.5.31</version>
</dependency>

maven

Java code:

import org.junit.After;
import org.junit.Before;
import org.junit.Test; import java.sql.*; /**
* @Author:Xavier
* @Data:2019-02-22 13:34
**/ public class ImpalaOptionTest {
private String driverName="com.cloudera.impala.jdbc41.Driver";
private String url="jdbc:impala://datanode02:21050/xavierdb";
private Connection conn=null;
private Statement state=null;
private ResultSet res=null; @Before
public void init() throws ClassNotFoundException, SQLException {
Class.forName(driverName);
conn= DriverManager.getConnection(url,"impala","impala");
state=conn.createStatement();
} //显示数据库
@Test
public void test() throws SQLException {
// ResultSet res=state.executeQuery("show databases");
// ResultSet res = state.executeQuery("show tables");
res = state.executeQuery("select * from students"); while(res.next()){
System.out.println(String.valueOf(res.getString(1)));
}
} // 释放资源
@After
public void destory() throws SQLException {
if (res != null) state.close();
if (state != null) state.close();
if (conn != null) conn.close();
} }

Java Code

impala操作hase、hive的更多相关文章

  1. [转]impala操作hive数据实例

    https://blog.csdn.net/wiborgite/article/details/78813342 背景说明: 基于CHD quick VM环境,在一个VM中同时包含了HDFS.YARN ...

  2. impala系列: 同步Hive元数据和收集统计信息

    ---====================-- Impala 获取hive 的 metadata ---====================Impala 通常和Hive共用同一个metadat ...

  3. Java实现impala操作kudu

    推荐阅读: 论主数据的重要性(正确理解元数据.数据元) CDC+ETL实现数据集成方案 Java实现impala操作kudu 实战kudu集成impala 对于impala而言,开发人员是可以通过JD ...

  4. Hive记录-Impala jdbc连接hive和kudu参考

    1.配置环境Eclipse和JDK 2.加载hive jar包或者impala jar包 备注:从CDH集群里面拷贝出来 下载地址:https://www.cloudera.com/downloads ...

  5. 使用impala操作kudu之创建kudu表(内部表和外部表)

    依次启动HDFS.mysql.hive.kudu.impala 登录impala的shell控制端: Impala-shell 1:使用该impala-shell命令启动Impala Shell .默 ...

  6. Spark记录-Spark-Shell客户端操作读取Hive数据

    1.拷贝hive-site.xml到spark/conf下,拷贝mysql-connector-java-xxx-bin.jar到hive/lib下 2.开启hive元数据服务:hive  --ser ...

  7. impala不能查询hive中新增加的表问题

         使用Cloudera Manager部署安装的CDH和Impala,Hive中新增加的表,impala中查询不到,其原因是/etc/impala/conf下面没有hadoop和hive相关的 ...

  8. Impala 加载Hive的UDF

    Impala的UDF有两种: Native Imapal UDF:使用C++开发的,性能极高,官方性能测试比第二种高出将近10倍 Hive的UDF:是Hive中的UDF,直接加载到Impala中,优点 ...

  9. Hive 表操作(HIVE的数据存储、数据库、表、分区、分桶)

    1.Hive的数据存储 Hive的数据存储基于Hadoop HDFS Hive没有专门的数据存储格式 存储结构主要包括:数据库.文件.表.试图 Hive默认可以直接加载文本文件(TextFile),还 ...

随机推荐

  1. html 常用button事件

    <input onclick="document.all.WebBrowser.ExecWB(1,1)" type="button" value=&quo ...

  2. Linux如何查看端口

    Linux如何查看端口 1.lsof -i:端口号 用于查看某一端口的占用情况,比如查看8000端口使用情况,lsof -i:8000 # lsof -i:8000 COMMAND PID USER ...

  3. LeetCode 112. Path Sum 二叉树的路径和 C++

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  4. Nexus构建

    参考文章: Maven远程仓库搭建与配置(https://my.oschina.net/u/1455528/blog/637063) Maven仓库—Nexus环境搭建及简单介绍(http://blo ...

  5. Delphi 窗口操作

    unit UnitWinUtils; interface uses Windows; Type TDWA128=Array [..] of LongWord; TDWA256=Array [..] o ...

  6. cxgrid属性说明,每次用的时候费时费力查找。

    由层得到数据表名: procedure TFB_PatientWaiting.cxgrdbtblvwGrid1DBTableView_MyPatienWaitingDblClick( Sender: ...

  7. C++并发编程学习笔记

    // //  main.cpp //  test1 // //  Created by sofard on 2018/12/27. //  Copyright © 2018年 dapshen. All ...

  8. CSS3奇特的渐变示例

    渐变 4个维度去理解渐变 线性渐变 径向渐变 新写法 老写法 最后的老写法镜像渐变可能不太准确.其余都完全正确 <!DOCTYPE html> <html> <head& ...

  9. Jquery实现点击表格行变色!

    时隔一年左右,学习了新的知识,从尝试Linux部署项目,网络安全,至后端开发,然后用起了Jquery, 而且是必须要做.也让自己见识可能会更广泛一些.对于一个刚毕业的大学生而言.方正我是没有用过jqu ...

  10. (转)IIS7无法读取配置文件解决办法

    web.config 太大导致”出现无法读取配置文件,因为它超过了最大文件大小”错误问题的解决方法,如下:HKLM\SOFTWARE\Microsoft\InetStp\Configuration\M ...