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. parentNode、parentElement,childNodes、children 它们有什么区别呢?

    parentNode.parentElement,childNodes.children 它们有什么区别呢?parentElement 获取对象层次中的父对象. parentNode 获取文档层次中的 ...

  2. ORACLE执行详解

    本文源自TTT BLOG,原文地址:http://blog.chinaunix.net/u3/107265/showart_2192657.html 简介:     本文全面详细介绍oracle执行计 ...

  3. mysql 与 mycat集成读写分离

    1,先说下同步数据库原理,其实很简单.就是在主库做数据库操作的时候,会生成一个本地日志,日志会在my.ini当中配置看图一,读库会实时去扫描日志,如果有更新就直接在读库执行. 这个是主库my.ini配 ...

  4. Find Median from Data Stream

    常规方法 超时 class MedianFinder { vector<int> coll; public: MedianFinder(){ } void heapfu(vector< ...

  5. 【jq】c#零基础学习之路(3)继承和虚方法

    c#只能继承一个基类和多个接口(0+) 父类:Human: class Human { public virtual Move() { Console.WriteLine("Human的虚方 ...

  6. 启动struts2项目出现classnotfound错误

    由于工作需求.需要了解struts2项目,前几天部署了一个struts2的demo,研究url的解析过程,昨天还是好好的,今天修改了一下web.xml文件,然后启动Tomcat就报错,错误如下: 严重 ...

  7. epoll 反应堆

    epoll反应堆模型 ================================ 下面代码实现的思想:epoll反应堆模型:( libevent 网络编程开源库 核心思想) . 普通多路IO转接 ...

  8. LEETCODE —— Surrounded Regions

    Total Accepted: 43584 Total Submissions: 284350 Difficulty: Medium Given a 2D board containing 'X' a ...

  9. 基本的HTML标签

    html的基本标签: <html><body> <h1>My First Heading</h1><h2>this second headi ...

  10. 怎么样修改PHPStorm中文件修改后标签和文件名的颜色与背景色

    自从最近在PHPstrom里引入Git,并且使用MONOKAI_SUBLIME主题之后 ,当文件在PHPstrom中进行编辑,文档内容变化时,左侧项目文件列表中的文件名颜色以及右侧编辑区域标签卡的文件 ...