How spark SQL Handles date types

Source: Internet
Author: User

How does spark SQL handle date types, time type JSON each object cannot wrap # #问题描述json How does the File date type work? How do I convert from a character type to a date or datetime type?
the JSON file is as follows, with character format type of date
"{" "Name": "Andy", "age": +, "time": "2015-03-03t08:25:55.769z"} {"name": "Justin", "age": +, "time": "2015-04-04 t08:25:55.769z "} {" name ":" Pan "," age ": $," Time ":" 2015-05-05t08:25:55.769z "} {" name ":" Penny "," Age ":" Time " : "2015-05-05t08:25:55.769z"} "' Default inferred Schema: ' Root |--_corrupt_record:string (nullable = True) |--Age:long (nullabl E = True) |--name:string (nullable = True) |--time200:string (nullable = True) '
Test Code "val fileName =" Person.json "val sc = sparkutils.getsclocal (" JSON file Test ") Val SqlContext = new Org.apac He.spark.sql.SQLContext (SC) val jsonfile = SqlContext.read.json (fileName) Jsonfile.printschema () "# #解决方案
### Programme I,JSON data time is long seconds or milliseconds
### Scenario two, custom schema ' val fileName = ' Person.json '     val sc = sparkutils.getsclocal ("JSON file Test")     val sqlcontext = new Org.apache.spark . sql. SqlContext (SC)     val schema:structtype = Structtype (mutable. Arrayseq (      Structfield ("name", StringType, True),      Structfield ("Age", StringType, True),      Structfield ("Time", Timestamptype, true));     val jsonfile = SqlContext.read.schema (Schema). JSON (FileName)     Jsonfile.printschema ()     jsonfile.registertemptable ("person")     Val Now:timestamp = New Timestamp (System.currenttimemillis ())      val teenagers = Sqlcontext.sql ("SELECT * FROM Person WHERE age >= <= and time <= ' +now+ "'")     Teenagers.foreach (println) &NBSP;&N bsp;  val dataFrame = Sqlcontext.sql ("SELECT * from", WHERE age >=, and age <=-and time <= ' 2015-03 -03 16: 25:55.769 ' ")     Dataframe.foreach (println)" # # #方案三, SQL Build table  CREATE TABLE SQL ' create temporary table person IF not EXISTS [(Age:long, Name:string, Time:timestamp)] USING org.apache.spark.sql . JSONOPTIONS (path ' Person.json ')Syntax create [temporary] TABLE [IF not EXISTS] [(Col-name Data-type [, ...])] USING [OPTIONS ...] [As] ""
### Programme IV,with textfile convert

From for notes (Wiz)

How spark SQL Handles date types

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.