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. LeetCode 102. Binary Tree Level Order Traversal 二叉树的层次遍历 C++

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  2. HTTPS 基本流程 转载 https://zhuanlan.zhihu.com/p/27395037

    协议 1.HTTP 协议(HyperText Transfer Protocol,超文本传输协议):是客户端浏览器或其他程序与Web服务器之间的应用层通信协议 . 2.HTTPS 协议(HyperTe ...

  3. Spring MVC配置实例

    1.下载Jar文件,添加到项目 lib文件夹中. 使用eclipse新建 Web 项目.下载导入相关的 jar 和 Tomcat.我的java版本是JDK1.8 对应的 Tomcat 版本是 8.0. ...

  4. Zabbix11.3 Zabbix SNMP 常用OID列表

    点击获取CISCO设备OID 系统参数(1.3.6.1.2.1.1) OID 描述 备注 请求方式 .1.3.6.1.2.1.1.1.0 获取系统基本信息 SysDesc GET .1.3.6.1.2 ...

  5. void类型及void指针(转载)

    转载 https://www.cnblogs.com/pengyingh/articles/2407267.html 2.void的含义 void的字面意思是“无类型”,void *则为“无类型指针” ...

  6. springboot 开启事务以及手动提交事务

    添加依赖,sprongboot 会默认开启事务管理 org.springframework.boot spring-boot-starter-jdbc 在需要的服务类里添加注解 @Autowired ...

  7. kinect 深度图与彩色图对齐程序

    //#include "duiqi.hpp" #include "kinect.h" #include <iostream> #include &q ...

  8. Windows下nginx下安装amqp

    1.复制php_amqp.dll 到php/ext/ 2.复制rabbitmq.4.dll 到php/3.复制rabbitmq.4.dll 到windows/system32/ (如32位) ,64位 ...

  9. 浅谈openstack中使用linux_bridge实现vxlan网络

    openstack环境: 1 版本:ocata 2 系统:ubuntu16.04.2 3 控制节点 1个 + 计算节点 1个 4 控制节点网卡为ens33,ip = 172.171.5.200 ens ...

  10. 布局inline-block问题

    当在一行中需要展示多个拥有块级属性的标签元素时,通常选择display:inline-block; 优点:不用设置浮动或定位,浮动脱离文档流还需要清除浮动,定位降低扩展性. 问题: 1.标签元素之间会 ...