• Stars
    star
    225
  • Rank 177,187 (Top 4 %)
  • Language
    Java
  • Created over 11 years ago
  • Updated almost 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Tool to generate a Hive schema from a JSON example doc

Overview

The best tool for using JSON docs with Hive is rcongui's openx Hive-JSON-Serde. When using that JSON Serde, you define your Hive schema based on the contents of the JSON.

Hive schemas understand arrays, maps and structs. You can map a JSON array to a Hive array and a JSON "object" to either a Hive map or struct. I prefer to map JSON objects to structs.

This tool will take a curated JSON document and generate the Hive schema (CREATE TABLE statement) for use with the openx Hive-JSON-Serde. I say "curated" because you should ensure that every possible key is present (with some arbitrary value of the right data type) and that all arrays have at least one entry.

If the curated JSON example you provide has more than one entry in an array, only the first one will be examined, so you should ensure that it has all the fields.

For more information on using the openx Hive-JSON-SerDe, see my blog post entry.

Build

mvn package

Creates json-hive-schema-1.0.jar and json-hive-schema-1.0-jar-with-dependencies.jar in the target directory.

Usage

with the non-executable jar

java -cp target/json-hive-schema-1.0.jar net.thornydev.JsonHiveSchema file.json

# optionally specify the name of the table
java -cp target/json-hive-schema-1.0.jar net.thornydev.JsonHiveSchema file.json my_table_name

with the executable jar

java -jar target/json-hive-schema-1.0-jar-with-dependencies.jar file.json

java -jar target/json-hive-schema-1.0-jar-with-dependencies.jar file.json my_table_name

Both print the Hive schema to stdout.

Example:

Suppose I have the JSON document:

{
  "description": "my doc",
  "foo": {
    "bar": "baz",
    "quux": "revlos",
    "level1" : {
      "l2string": "l2val",
      "l2struct": {
        "level3": "l3val"
      }
    }
  },
  "wibble": "123",
  "wobble": [
    {
      "entry": 1,
      "EntryDetails": {
        "details1": "lazybones",
        "details2": 414
      }
    },
    {
      "entry": 2,
      "EntryDetails": {
        "details1": "entry 123"
      }
    }
  ]
}

I recommend distilling it down to a doc with a single entry in each array and one that has all possible keys filled in - the values don't matter as long as they are present and a type can be determined.

So for the curated version of the JSON I've removed one of the entries from the "wobble" array and ensured that the remaining one has all the fields:

{
  "description": "my doc",
  "foo": {
    "bar": "baz",
    "quux": "revlos",
    "level1" : {
      "l2string": "l2val",
      "l2struct": {
        "level3": "l3val"
      }
    }
  },
  "wibble": "123",
  "wobble": [
    {
      "entry": 1,
      "EntryDetails": {
        "details1": "lazybones",
        "details2": 414
      }
    }
  ]
}

Now generate the schema:

$ java -jar target/json-hive-schema-1.0-jar-with-dependencies.jar in.json TopQuark
CREATE TABLE TopQuark (
  description string,
  foo struct<bar:string, level1:struct<l2string:string, l2struct:struct<level3:string>>, quux:string>,
  wibble string,
  wobble array<struct<entry:int, entrydetails:struct<details1:string, details2:int>>>)
ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe';

You can then load your data into Hive and run queries like this:

hive > select wobble.entry, wobble.EntryDetails.details1, wobble.EntryDetails[0].details2 from TopQuark;
entry   details1                    details2
[1,2]   ["lazybones","entry 123"]   414
Time taken: 15.665 seconds

More Repositories

1

merkle-tree

Merkle Tree implementation in Java
Java
130
star
2

mybatis-koans

A koan-style tutorial in Java for the MyBatis data mapper framework
Graphviz (DOT)
72
star
3

PlusCal-Examples

Algorithm examples in PlusCal, the algorithm language of Lamport's TLA+
55
star
4

land-of-lisp-in-clojure

Code from Barski's Land of Lisp book done in Clojure
Clojure
39
star
5

little-schemer

Implementation of code in Little Schemer in Scala and Clojure
Clojure
26
star
6

go-lightly

Clojure library to facilitate Go-like CSP concurrent programming
Clojure
21
star
7

simplecsv

CSV parser for Java, based on the OpenCSV model, but fixing many of the inconsistencies and defects of that codebase.
Java
12
star
8

kstorm

Example code showing how to use a Kafka Spout in Storm 0.9.3
Java
12
star
9

MyBatis-UserGuide-Companion-Code

Companion and Demo code for the MyBatis 3 User Guide
Java
9
star
10

Disruptor-Daj

Code using the Disruptor library from LMAX
Java
5
star
11

fslocate

Indexer of files and "locate" client to lookup files like the Unix locate/updatedb program
Go
4
star
12

cmblog

A Clojure blog based on 10gen Mongo DB course for Java devs.
Clojure
3
star
13

opencsv

Defunct fork of the opencsv Java project
Java
2
star
14

pikeregex

A small regex library in Go based on Rob Pike's C code in The Practice of Programming
Go
2
star
15

midcunit

Lightweight C Unit Test Framework
C
2
star
16

depot

Rails 3 Shopping Cart Application
Ruby
1
star
17

json-parsers

A collection of JSON parsers and writers for various languages
JavaScript
1
star
18

midcutils

A simple set of C utility functions
C
1
star
19

capslock

A tiny JavaScript library to detect the Caps Lock key in web browsers
JavaScript
1
star
20

emacs-setup

My emacs config (goes in ~/.emacs.d)
Emacs Lisp
1
star
21

rust-concurrency-examples

Translation of Go concurrency examples into Rust
Rust
1
star
22

clj-how-tos

Clojure how-to snippets and working code examples
Clojure
1
star
23

Staxxas

A sanity wrapper around JAXP's XMLStreamWriter
Java
1
star
24

mpblog

Sample Application while learning Rails
Ruby
1
star
25

password-safe

Simple Ruby-based password safes to keep your password collections encrypted
Ruby
1
star
26

emacs24-setup

My config files for emacs24
Emacs Lisp
1
star
27

tree

Versions of the Unix/Linux tree command in multiple languages
Java
1
star
28

assizes

"Courts of Assizes" assertion helper methods for use with the std Go testing library
Go
1
star