spark之Row

hkl曰:直接搞过来官方的API文档,看不懂英文没关系,看它的实例就可以了。

 

http://spark.apache.org/docs/1.3.1/api/scala/index.html#org.apache.spark.sql.Row

 

注意:import org.apache.spark.sql._  要先导入这个包 。

 

 

Represents one row of output from a relational operator. Allows both generic access by ordinal, which will incur boxing overhead for primitives, as well as native primitive access.

It is invalid to use the native primitive interface to retrieve a value that is null, instead a user must check isNullAt before attempting to retrieve a value that might be null.

To create a new Row, use RowFactory.create() in Java or Row.apply() in Scala.

Row object can be constructed by providing field values. Example:

import org.apache.spark.sql._

// Create a Row from values.
Row(value1, value2, value3, ...)
// Create a Row from a Seq of values.
Row.fromSeq(Seq(value1, value2, ...))

A value of a row can be accessed through both generic access by ordinal, which will incur boxing overhead for primitives, as well as native primitive access. An example of generic access by ordinal:

import org.apache.spark.sql._

val row = Row(1, true, "a string", null)
// row: Row = [1,true,a string,null]
val firstValue = row(0)
// firstValue: Any = 1
val fourthValue = row(3)
// fourthValue: Any = null

For native primitive access, it is invalid to use the native primitive interface to retrieve a value that is null, instead a user must check isNullAt before attempting to retrieve a value that might be null. An example of native primitive access:

// using the row from the previous example.
val firstValue = row.getInt(0)
// firstValue: Int = 1
val isNull = row.isNullAt(3)
// isNull: Boolean = true

In Scala, fields in a Row object can be extracted in a pattern match. Example:

import org.apache.spark.sql._

val pairs = sql("SELECT key, value FROM src").rdd.map {
  case Row(key: Int, value: String) =>
    key -> value
}

实在想看懂的,就直接用翻译插件吧。

 

这个都是它的具体的API,

还有一个要注意的是他的一个API      isNullAt()这个索引第一位的元素是  0  。

 

size 方法是显示row里元素的个数。

row的api很丰富,自己需要的话就查看文章顶部的链接。

### C++ 中 `std::map` 和 `std::multimap` 的区别及用法 #### 定义与基本概念 `std::map` 是一种关联容器,用于存储键值对(key-value pairs),其中每个键都是唯一的。它基于红黑树实现,因此提供了高效的查找、插入和删除操作[^1]。 相比之下,`std::multimap` 也是一种关联容器,但它允许同一个键存在多个映射值。换句话说,在 `std::multimap` 中,相同的键可以对应不同的值[^2]。 --- #### 主要区别 | 特性 | `std::map` | `std::multimap` | |---------------------|------------------------------------|-------------------------------------| | **唯一键约束** | 键必须唯一 | 同一键可有多个对应的值 | | **内部结构** | 基于平衡二叉搜索树 | 类似 `std::map` | | **迭代器行为** | 迭代器指向单个键值对 | 可能返回具有相同键的多个键值对 | --- #### 使用方法 以下是两个容器的基本使用方式: ```cpp #include <iostream> #include <map> int main() { std::map<int, std::string> myMap; myMap[1] = "Apple"; myMap[2] = "Banana"; for (const auto& pair : myMap) { std::cout << pair.first << ": " << pair.second << "\n"; } return 0; } ``` 对于 `std::multimap`,由于允许多个键的存在,其访问逻辑稍显复杂。下面是一个简单的例子展示如何遍历重复键的值: ```cpp #include <iostream> #include <map> int main() { std::multimap<int, std::string> myMultiMap; myMultiMap.insert({1, "Red"}); myMultiMap.insert({1, "Green"}); myMultiMap.insert({2, "Blue"}); for (const auto& pair : myMultiMap) { std::cout << pair.first << ": " << pair.second << "\n"; } // 查找特定键的所有条目 int keyToFind = 1; auto range = myMultiMap.equal_range(keyToFind); for (auto it = range.first; it != range.second; ++it) { std::cout << "Key " << keyToFind << " maps to " << it->second << "\n"; } return 0; } ``` 上述代码展示了如何通过 `equal_range()` 方法获取指定键的所有映射项。 --- #### 性能比较 两者都提供 O(log n) 时间复杂度的操作性能,但由于 `std::multimap` 支持多值映射,可能在某些场景下需要额外处理来管理冗余数据。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值