Spark SQL Example

This example demonstrates how to use sqlContext.sql to create and load a table and select rows from the table into a DataFrame. The next steps use the
DataFrame API to filter the rows for salaries greater than 150,000 and show the resulting DataFrame.

  1. At the command-line, copy the Hue sample_07 data to HDFS:

    $ hdfs dfs -put HUE_HOME/apps/beeswax/data/sample_07.csv /user/hdfs

    where HUE_HOME defaults to /opt/cloudera/parcels/CDH/lib/hue (parcel installation) or /usr/lib/hue (package
    installation).

  2. Start spark-shell:
    $ spark-shell
  3. Create a Hive table:
    scala> sqlContext.sql("CREATE TABLE sample_07 (code string,description string,total_emp int,salary int) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' STORED AS TextFile")
  4. Load data from HDFS into the table:
    scala> sqlContext.sql("LOAD DATA INPATH '/user/hdfs/sample_07.csv' OVERWRITE INTO TABLE sample_07")
  5. Create a DataFrame containing the contents of the sample_07 table:
    scala> val df = sqlContext.sql("SELECT * from sample_07")
  6. Show all rows with salary greater than 150,000:
    scala> df.filter(df("salary") > 150000).show()

    The output should be:

    +-------+--------------------+---------+------+
    | code| description|total_emp|salary|
    +-------+--------------------+---------+------+
    |11-1011| Chief executives| 299160|151370|
    |29-1022|Oral and maxillof...| 5040|178440|
    |29-1023| Orthodontists| 5350|185340|
    |29-1024| Prosthodontists| 380|169360|
    |29-1061| Anesthesiologists| 31030|192780|
    |29-1062|Family and genera...| 113250|153640|
    |29-1063| Internists, general| 46260|167270|
    |29-1064|Obstetricians and...| 21340|183600|
    |29-1067| Surgeons| 50260|191410|
    |29-1069|Physicians and su...| 237400|155150|
    +-------+--------------------+---------+------+

Spark SQL Example的更多相关文章

  1. Spark SQL 之 Data Sources

    #Spark SQL 之 Data Sources 转载请注明出处:http://www.cnblogs.com/BYRans/ 数据源(Data Source) Spark SQL的DataFram ...

  2. Spark SQL 之 DataFrame

    Spark SQL 之 DataFrame 转载请注明出处:http://www.cnblogs.com/BYRans/ 概述(Overview) Spark SQL是Spark的一个组件,用于结构化 ...

  3. 【原】Learning Spark (Python版) 学习笔记(三)----工作原理、调优与Spark SQL

    周末的任务是更新Learning Spark系列第三篇,以为自己写不完了,但为了改正拖延症,还是得完成给自己定的任务啊 = =.这三章主要讲Spark的运行过程(本地+集群),性能调优以及Spark ...

  4. Spark 官方文档(5)——Spark SQL,DataFrames和Datasets 指南

    Spark版本:1.6.2 概览 Spark SQL用于处理结构化数据,与Spark RDD API不同,它提供更多关于数据结构信息和计算任务运行信息的接口,Spark SQL内部使用这些额外的信息完 ...

  5. 通过Spark SQL关联查询两个HDFS上的文件操作

    order_created.txt   订单编号  订单创建时间 -- :: -- :: -- :: -- :: -- :: order_picked.txt   订单编号  订单提取时间 -- :: ...

  6. Spark SQL 之 Migration Guide

    Spark SQL 之 Migration Guide 支持的Hive功能 转载请注明出处:http://www.cnblogs.com/BYRans/ Migration Guide 与Hive的兼 ...

  7. Spark SQL 官方文档-中文翻译

    Spark SQL 官方文档-中文翻译 Spark版本:Spark 1.5.2 转载请注明出处:http://www.cnblogs.com/BYRans/ 1 概述(Overview) 2 Data ...

  8. Spark SQL 之 Performance Tuning & Distributed SQL Engine

    Spark SQL 之 Performance Tuning & Distributed SQL Engine 转载请注明出处:http://www.cnblogs.com/BYRans/ 缓 ...

  9. 基于Spark1.3.0的Spark sql三个核心部分

    基于Spark1.3.0的Spark sql三个核心部分: 1.可以架子啊各种结构化数据源(JSON,Hive,and Parquet) 2.可以让你通过SQL,saprk内部程序或者外部攻击,通过标 ...

随机推荐

  1. bootstrap-14

    基础导航条: 使用方法:1. 首先在制作导航的列表(<ul class="nav">)基础上添加类名"navbar-nav" 2.在列表外部添加一个 ...

  2. [ JS 进阶 ] 基本类型 引用类型 简单赋值 对象引用

    ECMAScirpt 变量有两种不同的数据类型:基本类型,引用类型.也有其他的叫法,比如原始类型和对象类型,拥有方法的类型和不能拥有方法的类型,还可以分为可变类型和不可变类型,其实这些叫法都是依据这两 ...

  3. UVa 673 平衡的括号

    题意:给出包含"()"和"[]"的括号序列,判断是否合法. 用栈来完成,注意空串就行. #include<iostream> #include< ...

  4. 【转载】App.config/Web.config 中特殊字符的处理

    写一个网站,遇到一个问题,发布以后,提示错误,但是即使打开错误提示(在web.config中打开),还是只提示错误,没提示什么地方错误,这让我知道了:是webconfig本身的错误,经过排除,是链接字 ...

  5. HTML学习-日常问题-1

    最近入了Html的坑 所以开始了学习前端的漫长道路 所以打算将日常遇到的小问题记录下来 1.关于img自适应的问题 <div class="main"> <div ...

  6. get和post的差异

    主要差异: 1.get在地址栏上回显示用户信息,安全性低,post采用加密方式传输不显示,安全性高. 2.get相比post提交方式较快一点,因为post封装了一次消息再发送(加密). 3.get方式 ...

  7. Windows环境下npm install常见错误

    Windows环境下npm install安装包依赖时,常出现一些错误,下面为个人解决办法: 错误一 缺少python环境: G:\nodejs\moviesite\node_modules\bcry ...

  8. JSBinding / FAQ & Trouble Shooting

    Q: Why javascript file extension is .javascript?A: Because Unity treats .js files as Unity script an ...

  9. easycwmp的交叉编译

    原创作品,转载请注明出处 copyright:weishusheng   2015.3.18 email:642613208@qq.com 平台: Linux version 2.6.32-279.e ...

  10. JS正则截取两个字符串之间的字符串

    match方法 var str = "iid0000ffr"; var substr = str.match(/id(\S*)ff/); console.log(substr) 返 ...