大数据学习练习
引入依赖:
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<version>1.8</version>
<scope>system</scope>
<systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
</dependency>
备注:
在对HBase执行增删改查时,只需要引入hbase-client模块即可,运行MR操作hbase时,需要引入hbase-server。
拷贝hdfs-site.xml文件到客户端的类路径下!
hbase-site.xml
配置文件
ConnectionUtil.java
案例
NameSpaceUtil.java
案例
TableUtil.java
案例
DataUtil.java
案例
案例
MR读取hbase中t2表的部分数据写入到t4表中
案例
MR读取HDFS中的数据并写入到hbase表中
判断质数函数(for循环判断)
案例
map()和foreach():案例
Iterator迭代器:案例
reduce():案例
折叠foldLeft():案例
scanLeft():案例
滑窗sliding():案例
拉链zip():案例
分组groupBy():案例
排序sorted()、sortBy()、sortWith():案例
Stream数据流:案例
并行集合:案例
6.4.1,map()映射应用案例
6.4.2,合并两个Map集合案例
6.4.3,MapIndexes案例
6.4.4,通过foldLeft折叠来模拟mkString方法案例
6.4.5,通过foldLeft折叠来模拟reverse反转方法案例
6.4.6,通过reduce聚合来求集合中的最大值案例
6.4.7,通过foldLeft折叠来同时求集合中的最大值和最小值案例
(1). 利用模式匹配,编写一个 swap(arr: Array[Int]) 函数,交换数组中前两个元素的位置
答案
package com.wenthomas.pattern.homework
/**
* @author Verno
* @create 2020-03-10 0:15
*/
/**
* 利用模式匹配,编写一个 swap(arr: Array[Int]) 函数,交换数组中前两个元素的位置
*/
object MySwap extends App {
val list1 = List(30, 50, 70, 60, 10, 20)
def mySwapFirstAndSecond(arr: Array[Int]): Array[Int] = {
val result = arr match {
case Array(a, b, _*) => {
arr(0) = b
arr(1) = a
arr
}
}
result
}
println(mySwapFirstAndSecond(list1.toArray).mkString(","))
}
(2). 编写一个函数,计算 List[Option[Int]] 中所有非None值之和。分别使用 match 和不适用 match 来计算
答案
package com.wenthomas.pattern.homework
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
/**
* @author Verno
* @create 2020-03-10 0:47
*/
/**
* 编写一个函数,计算 List[Option[Int]] 中所有非None值之和。分别使用 match 和不适用 match 来计算
*/
object MySumInt extends App {
val list1 = List(30, 50, 70, 60, 10, 20)
//List(Some(30), None, None, None, Some(10), Some(20))
private val options: List[Option[Int]] = list1.map(x => if (x < 40) Some(x) else None)
//方法一:match模式匹配实现
def mySumInt1(list: List[Option[Int]]): Int = {
list.foldLeft(0)((sum, x) => {
x.isEmpty match {
case true =>
sum
case false =>
sum + x.get
}})
}
println(mySumInt1(options))
//方法二:一般方法实现
def mySumInt2(list: List[Option[Int]]): Int = {
list.foldLeft(0)((sum, x) => {
if (x.isEmpty) sum else sum + x.get
})
}
println(mySumInt2(options))
}
(3). 我们可以用列表制作只在叶子节点存放值的树。举例来说,列表((3 8) 2 (5)) 描述的是如下这样一棵树: List[Any] = List(List(3, 8), 2, List(5))
*
/|\
* 2 *
/\ |
3 8 5
不过,有些列表元素是数字,而另一些是列表。在Scala中,你必须使用List[Any]。
编写一个leafSum函数,计算所有叶子节点中的元素之和.
答案
package com.wenthomas.pattern.homework
/**
* @author Verno
* @create 2020-03-10 1:22
*/
/**
* 我们可以用列表制作只在叶子节点存放值的树。举例来说,列表((3 8) 2 (5)) 描述的是如下这样一棵树:
* *
* /|\
* * 2 *
* /\ |
* 3 8 5
* List[Any] = List(List(3, 8), 2, List(5))
* 不过,有些列表元素是数字,而另一些是列表。在Scala中,你必须使用List[Any]。
* 编写一个leafSum函数,计算所有叶子节点中的元素之和.
*/
object MySumLeaf extends App {
val list:List[Any] = List(List(3, 8), 2, List(5))
//使用递归完成
def leafSum(list: List[Any]): Int = {
var sum = 0
list.foreach(x => {
x match {
case list:List[_] => sum += leafSum(list)
case i:Int => sum += i
}
})
sum
}
println(leafSum(list))
}
(4). 如果使用样例会更好一些, 从二叉树开始
sealed abstract class BinaryTree
case class Leaf(value : Int) extends BinaryTree
case class Node(left: BinaryTree, right: BinaryTree) extends BinaryTree
编写一个函数计算所有叶子节点的元素的和:
val r = Node(Leaf(8), Node(Leaf(3), Leaf(9)))
答案
package com.wenthomas.pattern.homework
/**
* @author Verno
* @create 2020-03-10 1:32
*/
/**
* 如果使用样例会更好一些, 从二叉树开始
* sealed abstract class BinaryTree
* case class Leaf(value : Int) extends BinaryTree
* case class Node(left: BinaryTree, right: BinaryTree) extends BinaryTree
* 编写一个函数计算所有叶子节点的元素的和
* val r = Node(Leaf(8), Node(Leaf(3), Leaf(9)))
*/
sealed abstract class BinaryTree
case class Leaf(value : Int) extends BinaryTree
case class Node(left: BinaryTree, right: BinaryTree) extends BinaryTree
object MySumLeaf2 extends App {
val r = Node(Leaf(8), Node(Leaf(3), Leaf(9)))
//使用递归实现
def mySumLeaf3(tree: BinaryTree): Int = {
def loop(n: BinaryTree): Int = {
var sum = 0
n match {
case leaf: Leaf => sum += leaf.value
case node: Node => sum += (loop(node.left) + loop(node.right))
case _ => sum += 0
}
sum
}
loop(tree)
}
println(mySumLeaf3(r))
}
(5). 附加题(认为自己够牛逼的可以完成下):
大公司面试题: 使用递归
假设某国的货币有若干面值,现给一张大面值的货币要兑换成零钱,问有多少种兑换方式。
答案
package com.wenthomas.pattern.homework
/**
* @author Verno
* @create 2020-03-10 2:11
*/
/**
* 附加题(认为自己够牛逼的可以完成下):
* 大公司面试题: 使用递归
* 假设某国的货币有若干面值,现给一张大面值的货币要兑换成零钱,问有多少种兑换方式
*/
object MyCashExchange extends App {
def myCashExchange(cash: Int, coins: List[Int]): Int = {
val tuple = (cash, coins)
tuple match {
case (a, b) if a == 0 =>
1
case (a, b) if a <0 || b.size == 0 =>
0
case (a, b) =>
myCashExchange(a, b.tail) + myCashExchange(a - b.head, b)
}
}
val coins = List(1, 5, 10, 20, 50, 100);
println(myCashExchange(100, coins))
}