分享

大数据技术之高频面试题(五):涉及技术Sqoop、Scala

BGnv5 2020-10-19 15:45:44 发表于 经验分享 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 0 3299
问题导读:
1.Sqoop中常用的参数有哪些?
2.如何保证Sqoop数据导入导出时的一致性?
3.什么时Scala中的隐式转化?
4.什么时Scala中的柯里化?


大数据技术之高频面试题(三):涉及技术Flume、kafka
大数据技术之高频面试题(四):涉及技术Hive、Hbase

4.8 Sqoop参数
  1. /opt/module/sqoop/bin/sqoop import \
  2. --connect \
  3. --username \
  4. --password \
  5. --target-dir \
  6. --delete-target-dir \
  7. --num-mappers \
  8. --fields-terminated-by   \
  9. --query   "$2" ' and $CONDITIONS;'
复制代码

4.8.1 Sqoop导入导出Null存储一致性问题
        Hive中的Null在底层是以“\N”来存储,而MySQL中的Null在底层就是Null,为了保证数据两端的一致性。在导出数据时采用--input-null-string和--input-null-non-string两个参数。导入数据时采用--null-string和--null-non-string。

4.8.2 Sqoop数据导出一致性问题
        1)场景1:如Sqoop在导出到Mysql时,使用4个Map任务,过程中有2个任务失败,那此时MySQL中存储了另外两个Map任务导入的数据,此时老板正好看到了这个报表数据。而开发工程师发现任务失败后,会调试问题并最终将全部数据正确的导入MySQL,那后面老板再次看报表数据,发现本次看到的数据与之前的不一致,这在生产环境是不允许的。
        官网:http://sqoop.apache.org/docs/1.4.6/SqoopUserGuide.html
  1. Since Sqoop breaks down export process into multiple transactions, it is possible that a failed export job may result in partial data being committed to the database. This can further lead to subsequent jobs failing due to insert collisions in some cases, or lead to duplicated data in others. You can overcome this problem by specifying a staging table via the --staging-table option which acts as an auxiliary table that is used to stage exported data. The staged data is finally moved to the destination table in a single transaction.
复制代码
       –staging-table方式
  1. sqoop export --connect jdbc:mysql://192.168.137.10:3306/user_behavior --username root --password 123456 --table app_cource_study_report --columns watch_video_cnt,complete_video_cnt,dt --fields-terminated-by "\t" --export-dir "/user/hive/warehouse/tmp.db/app_cource_study_analysis_${day}" --staging-table app_cource_study_report_tmp --clear-staging-table --input-null-string '\N'
复制代码
       2)场景2:设置map数量为1个(不推荐,面试官想要的答案不只这个)
        多个Map任务时,采用–staging-table方式,仍然可以解决数据一致性问题。

4.8.3 Sqoop底层运行的任务是什么
        只有Map阶段,没有Reduce阶段的任务。

4.8.4 Sqoop数据导出的时候一次执行多长时间
        Sqoop任务5分钟-2个小时的都有。取决于数据量。

4.9 Scala
4.9.1 元组
        1)元组的创建
  1. val tuple1 = (1, 2, 3, "heiheihei")
  2. println(tuple1)
复制代码
       2)元组数据的访问,注意元组元素的访问有下划线,并且访问下标从1开始,而不是0
  1. val value1 = tuple1._4
  2. println(value1)
复制代码
       3)元组的遍历
  1. 方式1:
  2. for (elem <- tuple1.productIterator  ) {
  3.    print(elem)
  4. }
  5. println()
  6. 方式2:
  7. tuple1.productIterator.foreach(i => println(i))
  8. tuple1.produIterator.foreach(print(_))
复制代码

4.9.2 隐式转换
        隐式转换函数是以implicit关键字声明的带有单个参数的函数。这种函数将会自动应用,将值从一种类型转换为另一种类型。
  1. implicit def a(d: Double) = d.toInt
  2. //不加上边这句你试试
  3. val i1: Int = 3.5
  4. println(i1)
复制代码

4.9.3 函数式编程理解
        1)Scala中函数的地位:一等公民
        2)Scala中的匿名函数(函数字面量)
        3)Scala中的高阶函数
        4)Scala中的闭包
        5)Scala中的柯里化函数
                                
4.9.4 样例类
  1. case class Person(name:String,age:Int)
  2. 一般使用在 ds=df.as[Person]
复制代码

4.9.5 柯里化
        函数编程中,接受多个参数的函数都可以转化为接受单个参数的函数,这个转化过程就叫柯里化,柯里化就是证明了函数只需要一个参数而已。其实我们刚的学习过程中,已经涉及到了柯里化操作,所以这也印证了,柯里化就是以函数为主体这种思想发展的必然产生的结果。
        1)柯里化的示例
  1. def mul(x: Int, y: Int) = x * y
  2. println(mul(10, 10))
  3. def mulCurry(x: Int) = (y: Int) => x * y
  4. println(mulCurry(10)(9))
  5. def mulCurry2(x: Int)(y:Int) = x * y
  6. println(mulCurry2(10)(8))
复制代码
       2)柯里化的应用
        比较两个字符串在忽略大小写的情况下是否相等,注意,这里是两个任务:            
  • 全部转大写(或小写)
  • 比较是否相等
        针对这两个操作,我们用一个函数去处理的思想,其实无意间也变成了两个函数处理的思想。示例如下:
  1. val a = Array("Hello", "World")
  2. val b = Array("hello", "world")
  3. println(a.corresponds(b)(_.equalsIgnoreCase(_)))
复制代码
       其中corresponds函数的源码如下:
  1. def corresponds[B](that: GenSeq[B])(p: (A,B) => Boolean): Boolean = {
  2. val i = this.iterator
  3. val j = that.iterator
  4. while (i.hasNext && j.hasNex  t)
  5.   if (!p(i.next(), j.next()    ))
  6.     return fals
  7. !i.hasNext && !j.hasNext
  8. }
复制代码
       尖叫提示:不要设立柯里化存在义这样的命题,柯里化,是面向函数思想的必然产生结果。

4.9.6 闭包
        一个函数把外部的那些不属于自己的对象也包含(闭合)进来。
         案例1:
  1. def minusxy(x: Int) = (y: Int) => x - y
  2. 这就是一个闭包:
  3. 1) 匿名函数(y: Int) => x -y嵌套在minusxy函数中。
  4. 2) 匿名函数(y: Int) => x -y使用了该匿名函数之外的变量x
  5. 3) 函数minusxy返回了引用了局部变量的匿名函数
复制代码
       案例2
  1. def minusxy(x: Int) = (y: Int) => x - y
  2. val f1 = minusxy(10)
  3. val f2 = minusxy(10)
  4. println(f1(3) + f2(3))
  5. 此处f1,f2这两个函数就叫闭包。
复制代码

4.9.7 Some、None、Option的正确使用
  1. val map = Map("Tom"-> 23)
  2. map("Jack") // 抛出异常 java.util.NoSuchElementException: key not found: Jack
  3. map.get("Jack") // None
  4. map("Tom") // 23
  5. map.get("Tom") // Some(23)
  6. 使用模式匹配取出最后结果
  7. val optionAge = map.get("Tom")
  8. val age = optionAge match {
  9. case Some(x) => optionAge.get
  10. case None => 0
  11. }
复制代码




获取更多资源:
领取100本书+1T资源
http://www.aboutyun.com/forum.php?mod=viewthread&tid=26480

大数据5个项目视频
http://www.aboutyun.com/forum.php?mod=viewthread&tid=25235

名企资源、名企面试题、最新BAT面试题、专题面试题等资源汇总
https://www.aboutyun.com/forum.php?mod=viewthread&tid=27732


名企资源、名企面试题、最新BAT面试题、专题面试题等资源汇总
https://www.aboutyun.com/forum.php?mod=viewthread&tid=27732




没找到任何评论,期待你打破沉寂

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条