首页 文章 精选 留言 我的

精选列表

搜索[GA4集成],共10000篇文章
优秀的个人博客,低调大师

spark集成hbase与hive数据转换与代码练习

帮一个朋友写个样例,顺便练手啦~一直在做平台的各种事,但是代码后续还要精进啊。。。 1 import java.util.Date 2 3 import org.apache.hadoop.hbase.HBaseConfiguration 4 import org.apache.hadoop.hbase.client.{Put, Scan, Result} 5 import org.apache.hadoop.hbase.io.ImmutableBytesWritable 6 import org.apache.hadoop.hbase.mapred.TableOutputFormat 7 import org.apache.hadoop.hbase.mapreduce.TableInputFormat 8 import org.apache.hadoop.hbase.util.Bytes 9 import org.apache.hadoop.mapred.JobConf 10 import org.apache.log4j.{Level, Logger} 11 import org.apache.spark.rdd.RDD 12 import org.apache.spark.sql.DataFrame 13 import org.apache.spark.sql.hive.HiveContext 14 import org.apache.spark.{SparkContext, SparkConf} 15 16 /** 17 * Created by ysy on 2/10/17. 18 */ 19 object test { 20 21 case class ysyTest(LS_certifier_no: String,loc: String,LS_phone_no: String) 22 23 def main (args: Array[String]) { 24 val sparkConf = new SparkConf().setMaster("local").setAppName("ysy").set("spark.executor.memory", "1g") 25 val sc = new SparkContext(sparkConf) 26 val sqlContext = new HiveContext(sc) 27 sqlContext.sql("drop table pkq") 28 val columns = "LS_certifier_no,LS_location,LS_phone_no" 29 val hbaseRDD = dataInit(sc,"EVENT_LOG_LBS",columns).map(data =>{ 30 val id =Bytes.toString(data._2.getValue("f1".getBytes, "LS_certifier_no".getBytes)) 31 val loc = Bytes.toString(data._2.getValue("f1".getBytes, "LS_location".getBytes)) 32 val phone = Bytes.toString(data._2.getValue("f1".getBytes, "LS_phone_no".getBytes)) 33 (id,loc,phone) 34 }) 35 val showData = hbaseRDD.foreach(println) 36 val datas = hbaseRDD.filter(_._1 != null).filter(_._2 != null).filter(_._3 != null) 37 val hiveDF = initHiveTableFromHbase(sc:SparkContext,sqlContext,datas) 38 writeHiveTableToHbase(sc,hiveDF) 39 40 41 } 42 43 def initHiveTableFromHbase(sc:SparkContext,sqlContext: HiveContext,hiveRDD:RDD[(String,String,String)]) : DataFrame = { 44 val hRDD = hiveRDD.map(p => ysyTest(p._1,p._2,p._3)) 45 val hiveRDDSchema = sqlContext.createDataFrame(hiveRDD) 46 hiveRDDSchema.registerTempTable("pkq") 47 hiveRDDSchema.show(10) 48 hiveRDDSchema 49 } 50 51 def dataInit(sc : SparkContext,tableName : String,columns : String) : RDD[(ImmutableBytesWritable,Result)] = { 52 val configuration = HBaseConfiguration.create() 53 configuration.addResource("hbase-site.xml") 54 configuration.set(TableInputFormat.INPUT_TABLE,tableName ) 55 val scan = new Scan 56 val column = columns.split(",") 57 for(columnName <- column){ 58 scan.addColumn("f1".getBytes(),columnName.getBytes()) 59 } 60 val hbaseRDD = sc.newAPIHadoopRDD(configuration,classOf[TableInputFormat],classOf[org.apache.hadoop.hbase.io.ImmutableBytesWritable],classOf[org.apache.hadoop.hbase.client.Result]) 61 System.out.println(hbaseRDD.count()) 62 hbaseRDD 63 } 64 65 def writeHiveTableToHbase(sc : SparkContext,hiveDF : DataFrame) = { 66 val configuration = HBaseConfiguration.create() 67 configuration.addResource("hbase-site.xml ") 68 configuration.set(TableOutputFormat.OUTPUT_TABLE,"EVENT_LOG_LBS") 69 val jobConf = new JobConf(configuration) 70 jobConf.setOutputFormat(classOf[TableOutputFormat]) 71 72 val putData = hiveDF.map(data =>{ 73 val LS_certifier_no = data(0) 74 val LS_location = data(1) 75 val LS_phone_no = data(2) 76 (LS_certifier_no,LS_location,LS_phone_no) 77 }) 78 79 val rdd = putData.map(datas =>{ 80 val put = new Put(Bytes.toBytes(Math.random())) 81 put.addColumn("f1".getBytes(),"LS_certifier_no".getBytes(),Bytes.toBytes(datas._1.toString)) 82 put.addColumn("f1".getBytes(),"LS_location".getBytes(),Bytes.toBytes(datas._2.toString)) 83 put.addColumn("f1".getBytes(),"LS_phone_no".getBytes(),Bytes.toBytes(datas._3.toString)) 84 (new ImmutableBytesWritable, put) 85 }) 86 val showRdd = rdd.foreach(println) 87 rdd.saveAsHadoopDataset(jobConf) 88 } 89 90 }

优秀的个人博客,低调大师

【GitOps·Flux篇】通知与告警:Webhook 集成与事件通知

## 前言 GitOps 是自动化的,但出了问题你需要知道。Flux 的 notification-controller 可以在同步成功、失败、漂移等事件发生时自动发送通知到 Slack/钉钉/企微/邮件等渠道。本篇完整配置通知系统。 --- ## 一、通知架构 ``` Flux 事件流: Source/Sync 事件 → notification-controller → Provider → 通知渠道 (事件过滤器) (通知发送) ↓ Slack / 钉钉 / 企微 Teams / Discord Generic Webhook Rocket.Chat / Email ``` ### 三个 CRD | CRD | 作用 | |-----|------| | Provider | 定义通知渠道(如 Slack Webhook URL) | | Alert | 定义告警规则(什么事件发给谁) | | Receiver | 接收外部 Webhook 触发 Flux 操作 | --- ## 二、配置 Slack 通知 ### 创建 Provider ```yaml apiVersion: notification.toolkit.fluxcd.io/v1beta3 kind: Provider metadata: name: slack namespace: flux-system spec: type: slack channel: deployments # Slack 频道名 secretRef: name: slack-webhook-url # 引用 Webhook URL Secret username: Flux Bot # 显示名称 ``` ```bash # 创建 Slack Webhook Secret kubectl create secret generic slack-webhook-url \ --namespace=flux-system \ --from-literal=address=https://hooks.slack.com/services/xxx/yyy/zzz ``` ### 创建 Alert ```yaml apiVersion: notification.toolkit.fluxcd.io/v1beta3 kind: Alert metadata: name: deploy-alerts namespace: flux-system spec: providerRef: name: slack # 引用上面的 Provider # 事件源 eventSources: # 监控所有 GitRepository 事件 - kind: GitRepository name: "*" # 监控所有 Kustomization 事件 - kind: Kustomization name: "*" # 监控所有 HelmRelease 事件 - kind: HelmRelease name: "*" # 过滤严重级别 summary: "Flux deployment notifications" # 事件严重级别过滤 eventSeverity: - error # 只通知错误事件 - info # 也通知信息事件(成功同步) ``` ### Slack 消息格式 ``` Flux Bot [4:30 PM] ℹ️ Kustomization/myapp-prod Applied revision main/abc123 Message: Reconciliation finished ``` --- ## 三、配置钉钉通知 ### 钉钉 Provider ```yaml apiVersion: notification.toolkit.fluxcd.io/v1beta3 kind: Provider metadata: name: dingtalk namespace: flux-system spec: type: dingtalk secretRef: name: dingtalk-webhook # 钉钉不支持自定义 username ``` ```bash # 创建钉钉 Webhook Secret kubectl create secret generic dingtalk-webhook \ --namespace=flux-system \ --from-literal=address=https://oapi.dingtalk.com/robot/send?access_token=xxx ``` ### 钉钉 Alert ```yaml apiVersion: notification.toolkit.fluxcd.io/v1beta3 kind: Alert metadata: name: dingtalk-alerts namespace: flux-system spec: providerRef: name: dingtalk eventSources: - kind: Kustomization name: "*" namespace: flux-system - kind: HelmRelease name: "*" namespace: flux-system eventSeverity: - error - info ``` --- ## 四、配置企业微信通知 ### 企微 Provider ```yaml apiVersion: notification.toolkit.fluxcd.io/v1beta3 kind: Provider metadata: name: wecom namespace: flux-system spec: type: wecom secretRef: name: wecom-webhook ``` ```bash kubectl create secret generic wecom-webhook \ --namespace=flux-system \ --from-literal=address=https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxx ``` ### 企微 Alert ```yaml apiVersion: notification.toolkit.fluxcd.io/v1beta3 kind: Alert metadata: name: wecom-alerts namespace: flux-system spec: providerRef: name: wecom eventSources: - kind: Kustomization name: "*" - kind: GitRepository name: "*" eventSeverity: - error # 只通知错误 ``` --- ## 五、通用 Webhook 通知 ### 自定义 Provider ```yaml apiVersion: notification.toolkit.fluxcd.io/v1beta3 kind: Provider metadata: name: custom-webhook namespace: flux-system spec: type: generic address: https://my-notifier.com/api/flux-events secretRef: name: webhook-auth # 自定义 Headers headers: Authorization: "Bearer token" Content-Type: "application/json" ``` ```bash kubectl create secret generic webhook-auth \ --namespace=flux-system \ --from-literal=token=my-secret-token ``` ### 自定义消息模板 ```yaml apiVersion: notification.toolkit.fluxcd.io/v1beta3 kind: Provider metadata: name: custom-webhook namespace: flux-system spec: type: generic address: https://my-notifier.com/api/flux-events secretRef: name: webhook-auth # 自定义模板 template: # 自定义请求体 body: | { "event": "{{ .InvolvedObject.Kind }}", "name": "{{ .InvolvedObject.Name }}", "namespace": "{{ .InvolvedObject.Namespace }}", "severity": "{{ .Severity }}", "message": "{{ .Message }}", "timestamp": "{{ .Timestamp }}", "revision": "{{ .Revision }}", "summary": "{{ .Summary }}" } ``` --- ## 六、Webhook Receiver:接收外部触发 ### 用途 ``` 正常流程: Git push → Flux 检测(interval间隔)→ 同步 加 Receiver: Git push → Git Webhook → Flux Receiver → 立即同步 → 不用等 interval,即时响应 ``` ### 创建 Receiver ```yaml apiVersion: notification.toolkit.fluxcd.io/v1 kind: Receiver metadata: name: github-receiver namespace: flux-system spec: type: github # GitHub Webhook events: - push # 监听 push 事件 # 关联的资源 resources: - kind: GitRepository name: myapp-deploy namespace: flux-system # Webhook 密钥 secretRef: name: receiver-token ``` ```bash # 创建 Webhook 密钥 kubectl create secret generic receiver-token \ --namespace=flux-system \ --from-literal=token=my-webhook-secret ``` ### 暴露 Receiver 服务 ```bash # Flux 自动创建 Receiver Service kubectl get svc -n flux-system # webhook-receiver ClusterIP # 创建 Ingress 暴露到公网 kubectl apply -f - **培训要点**:Receiver 把 Flux 的同步延迟从分钟级降到秒级。没有 Receiver 时,Flux 按 `interval`(通常1分钟)轮询 Git;有了 Receiver,Git push 后 Webhook 直接触发同步。 --- ## 七、通知过滤与严重级别 ### 严重级别 | 级别 | 含义 | 示例事件 | |------|------|---------| | error | 错误 | 同步失败、镜像拉取失败 | | warning | 警告 | 健康检查不通过 | | info | 信息 | 同步成功、漂移修复 | ### 精确过滤 ```yaml # 只通知生产环境的事件 apiVersion: notification.toolkit.fluxcd.io/v1beta3 kind: Alert metadata: name: prod-only-alerts namespace: flux-system spec: providerRef: name: slack eventSources: # 只监控特定 Kustomization - kind: Kustomization name: "myapp-prod" namespace: flux-system - kind: HelmRelease name: "redis-prod" namespace: redis eventSeverity: - error # 生产环境错误才通知 - warning # 排除 dev 环境的事件 # 通过不在 eventSources 中列出 dev 的资源来实现 ``` --- ## 八、本篇要点回顾 1. 通知三件套:Provider(渠道)+ Alert(规则)+ Receiver(外部触发) 2. 支持 Slack/钉钉/企微/Teams/Discord/Generic Webhook 3. `eventSeverity` 控制通知级别:只通知 error 还是也通知 info 4. `eventSources` 精确控制监听哪些资源的哪些事件 5. Receiver 接收 Git Webhook,把同步延迟从分钟级降到秒级 6. 自定义模板可定制通知消息的格式和内容 **下一篇预告**:GitOps 进阶篇开始:《密钥管理:Sealed Secrets、SOPS 与 External Secrets》。

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册
修改密码
忘记密码