hive,redis杂记

陆续归纳,整理

1.hive优化

1.1建表优化

建表语句demo,存储格式为ORC,并且设置索引,*“orc.bloom.filter.columns”=“distinct_id”,*这一句的意思是 把 distinct_id这一列作为索引。分隔符为 \27,有效防止导入数据的时候,发生数据错位。
参考链接:ORC原理及查询优化

create table if not exists  test.cust_events(
event string,
user_id bigint,
distinct_id string,
record_time timestamp,
credit_billing_source string,
transfer_account string,
)
partitioned by ( load_dt VARCHAR(10))
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\27'
STORED AS ORCFile TBLPROPERTIES
('orc.compress'='SNAPPY',
'orc.create.index'='true',
"orc.bloom.filter.columns"="distinct_id",
'orc.bloom.filter.fpp'='0.05',
'orc.stripe.size'='10485760',
'orc.row.index.stride'='10000');

2.使用java连接redis哨兵集群

连接哨兵,下面代码中的ip1,ip2,ip3,port1,port2,port3,请根据自己的环境进行配置

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisSentinelPool;

import java.util.HashSet;
import java.util.Set;


public class SentinelPool {

    private static JedisSentinelPool pool;
    private static Set<String> sentinel = new HashSet<>();

    static {
        sentinel.add("ip1:port1");
        sentinel.add("ip2:port2");
        sentinel.add("ip3:port3");
        initPool();
    }

    private static void initPool() {
        pool = new JedisSentinelPool("mymaster",
                sentinel,
                new GenericObjectPoolConfig(),
                3000);
    }

    public static Jedis getJedis() {
        return pool.getResource();
    }

    public static void returnBrokenResource(Jedis jedis) {
        pool.returnBrokenResource(jedis);
    }

    public static void returnResource(Jedis jedis) {
        pool.returnResource(jedis);
    }
}

对redis进行操作的工具类

import redis.clients.jedis.Jedis;

public class JedisSentinelUtil {


    /**
     * 设置 key
     * @param key   键
     * @param value 值
     * @return
     */
    public static String set(String key, String value) {
        Jedis jedis = null;
        String result = null;
        try {
            jedis = SentinelPool.getJedis();
            result= jedis.set(key, value);
        } catch (Exception e) {
            SentinelPool.returnBrokenResource(jedis);
            return result;
        }
        SentinelPool.returnResource(jedis);
        return result;
    }


    /**
     * 设置一个有过期时间的 kv
     * @param key
     * @param value
     * @param exTime
     * @return
     */
    public static String setEx(String key, String value,int exTime) {
        Jedis jedis = null;
        String result = null;
        try {
            jedis = SentinelPool.getJedis();
            result = jedis.setex(key, exTime, value);
        } catch (Exception e) {
            SentinelPool.returnBrokenResource(jedis);
            return result;
        }
        SentinelPool.returnResource(jedis);
        return result;
    }

    /**
     * key 不存在就设置
     * @param key
     * @param value
     * @return
     */
    public static Long setNx(String key, String value) {
        Jedis jedis = null;
        Long result = null;
        try {
            jedis = SentinelPool.getJedis();
            result= jedis.setnx(key, value);
        } catch (Exception e) {
            SentinelPool.returnBrokenResource(jedis);
            return result;
        }
        SentinelPool.returnResource(jedis);
        return result;
    }
    /**
     * 删除 key
     * @param key
     * @return
     */
    public static Long del(String key) {
        Jedis jedis = null;
        Long result = null;
        try {
            jedis = SentinelPool.getJedis();
            result = jedis.del(key);
        } catch (Exception e) {
            SentinelPool.returnBrokenResource(jedis);
            return result;
        }
        SentinelPool.returnResource(jedis);
        return result;
    }

    /**
     * 获取 Key 对应的值
     * @param key
     * @return
     */
    public static String get(String key) {
        Jedis jedis = null;
        String result = null;
        try {
            jedis = SentinelPool.getJedis();
            result = jedis.get(key);
        } catch (Exception e) {
            SentinelPool.returnBrokenResource(jedis);
            return result;
        }
        SentinelPool.returnResource(jedis);
        return result;
    }


    /**
     * 设置一个新的值,并返回旧的值
     * @param key
     * @param value
     * @return
     */
    public static String geSet(String key,String value) {
        Jedis jedis = null;
        String result = null;
        try {
            jedis = SentinelPool.getJedis();
            result = jedis.getSet(key, value);
        } catch (Exception e) {
            SentinelPool.returnBrokenResource(jedis);
            return result;
        }
        SentinelPool.returnResource(jedis);
        return result;
    }


    /**
     * 设置过期时间
     * @param key
     * @param exTime
     * @return
     */
    public static Long expire(String key, int exTime) {
        Jedis jedis = null;
        Long result = null;
        try {
            jedis = SentinelPool.getJedis();
            result= jedis.expire(key, exTime);
        } catch (Exception e) {
            SentinelPool.returnBrokenResource(jedis);
            return result;
        }
        SentinelPool.returnResource(jedis);
        return result;
    }
    /**
     * 获取 dbsize
     * @return
     */
    public static Long getDbSize() {
        Jedis jedis = null;
        Long result = null;
        try {
            jedis = SentinelPool.getJedis();
            result = jedis.dbSize();
        } catch (Exception e) {
            SentinelPool.returnBrokenResource(jedis);
            return result;
        }
        SentinelPool.returnResource(jedis);
        return result;
    }
}

参考链接:JedisSentinelPool 连接Redis 主节点工具类

### 安装配置及使用 ES HDFS Hive Redis Kafka #### 关于ES (Elasticsearch) 对于 Elasticsearch 的安装与基本配置,通常建议通过官方提供的包管理器或二进制分发版来完成。下载完成后解压,并按照官方文档调整 `elasticsearch.yml` 文件内的集群名称、节点名称等参数设置。为了确保稳定运行,在生产环境中应考虑多节点部署以及合理规划索引策略。 #### 使用HDFS作为分布式文件系统 HDFS 是专为大规模数据集设计的高容错性文件系统[^2]。其架构由 NameNode 和 DataNode 组成;前者负责管理和维护元数据信息,后者则用于实际的数据存储工作。要搭建一套完整的 HDFS 环境,需先准备好 Java 运行环境,接着从 Apache 官网获取最新版本源码编译构建或是直接采用预编译好的二进制包。之后依照官方指导手册逐步执行初始化命名空间、格式化磁盘分区等一系列命令操作即可成功启动服务实例。 ```bash hdfs namenode -format start-dfs.sh ``` #### 利用Hive实现SQL接口访问结构化数据 Apache Hive 构建在 Hadoop 基础之上,允许用户利用类似于 SQL 的语句查询存放在 HDFS 上面的大规模半结构化/非结构化的资料集合。安装过程涉及 JDK 版本确认、环境变量设定、依赖库加载等多个环节。当一切准备就绪后,可通过如下方式开启交互式的 Shell 工具来进行表定义创建、字段映射指定等工作: ```sql hive> CREATE TABLE IF NOT EXISTS sample_table(id INT, name STRING); hive> LOAD DATA LOCAL INPATH '/path/to/local/file' INTO TABLE sample_table; ``` #### 应用Redis缓存机制提升性能表现 Redis 不仅是一个简单的键值对数据库,更支持多种数据类型的高效处理能力。它能够显著改善应用程序响应速度,尤其是在面对频繁读写的场景下效果尤为明显。针对初次使用者而言,最简便的方法莫过于借助 Docker 快速拉起容器镜像实例[^4]。当然也可以自行编译安装,不过这需要额外关注操作系统兼容性和软件间的相互依赖关系等问题。 ```dockerfile FROM redis:latest CMD ["redis-server"] ``` #### 实现Kafka消息队列功能 Kafka 设计之初即定位于满足低延迟、高性能的消息传递需求。除了常规的主题订阅模式外,还特别强调了日志追加写入特性,非常适合用来承载物联网设备上传感器所采集到的时间序列型数据流[^1]。具体实施步骤包括但不限于:安装 Zookeeper 以协调集群成员间通信;编辑 broker.properties 来定制监听端口、日志目录等相关选项;最后运用脚本批量启停整个生态系统的各个组成部分。 ```shell # 启动Zookeeper bin/zookeeper-server-start.sh config/zookeeper.properties & # 启动Kafka Broker bin/kafka-server-start.sh config/server.properties & ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值