Git Product home page Git Product logo

thrift-clj's Introduction

thrift-clj

Using Thrift from Clojure as if it was Clojure.

Build Status endorse

Usage

Leiningen (via Clojars)

Clojars Project

Make sure to additionally include a slf4j-compatible logger - e.g. logback via:

[ch.qos.logback/logback-classic "1.0.13"]

Note: Tested with the Thrift 0.9.0 compiler. Since this depends massively on the generated code, make sure to use that version (or any other one that was tested with this library).

Automatic Thrift Compilation

I recommend lein-thriftc for automatic compilation of Thrift IDL files to Java class files.

Example

A working example demonstrating Service and Client implementation should always be available as thrift-clj-example. A small peek follows.

Accessing Types

Thrift

namespace java org.example

struct Person {
  1: optional string firstName,
  2: string lastName,
  3: byte age
}

Compile to Java using Thrift and add to Leiningen's classpath. (see :java-source-paths)

Clojure

(require '[thrift-clj.core :as thrift])
(thrift/import
  (:types [org.example Person]))

(def clj-p (Person. "Some" "One" 99))
;; => #ns_1071852349.Person{:firstName "Some", :lastName "One", :age 99}

(def thr-p (thrift/->thrift clj-p))
;; => #<Person Person(firstName:Some, lastName:One, age:99)>

(class clj-p) ;; => ns_1071852349.Person
(class thr-p) ;; => org.example.Person

Implementing a Service

Thrift

namespace java org.example

// ... 'Person' struct from above ...

service PersonIndex {
    bool storePerson(1:i32 id, 2:Person p),
    Person getPerson(1:i32 id)
}

Clojure

(require '[thrift-clj.core :as thrift])
(thrift/import
  (:types [org.example Person])
  (:services org.example.PersonIndex))

(defonce person-db (atom {}))
(thrift/defservice person-index-service
  PersonIndex
  (storePerson [id p]
    (boolean
      (when-not (@person-db id)
        (info "Storing Person:" p)
        (swap! person-db assoc id p)
        true)))
  (getPerson [id]
    (info "Retrieving Person for ID:" id)
    (@person-db id)))

(thrift/serve-and-block!
  (thrift/multi-threaded-server
    person-index-service 7007
    :bind "localhost"
    :protocol :compact))

Running a Client

(require '[thrift-clj.core :as thrift])
(thrift/import
  (:types [org.example Person])
  (:clients org.example.PersonIndex))

(with-open [c (thrift/connect! PersonIndex ["localhost" 7007])]
  (PersonIndex/storePerson c 1 (Person. "Some" "One" 99))
  (PersonIndex/getPerson c 1))

Tests

You can run Midje tests using the following Leiningen command:

lein midje-all

Make sure that the Apache Thrift compiler is installed.

Roadmap

  • asynchronous client/server
  • union?
  • exceptions
  • tests & documentation
  • ...

Related Work/Inspiration

License

The MIT License (MIT)

Copyright (c) 2013-2015 Yannick Scherer

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

thrift-clj's People

Contributors

bts avatar maxthomas avatar pingles avatar xsc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

thrift-clj's Issues

support for inherited services?

First, thanks for the work on this project - very useful and clean API with good docs/tests!

quick question: In the later versions of thrift, one can inherit in thrift services. Are there any plans to support this in the (near) future? Currently I receive various errors about namespace collisions when I import e.g. two services with the same inherited "base" service.

No default constructor for thrift types

Assuming I have a simple thrift struct

struct A {
 1: string a;
 2: i32 b;
} 

Generated class A.java have default constructor, but if I use (thrift/import (:types [A]))
no default constructor is available.

Though, it may be a useful as a restriction but prevent common way to fill java structure:

(doto (A.)
  (.setA "some)
  (.setB 42))

Working with unions

I'm fine playing around with structs, but when I add the following to the thrift file:

union Identity {
  1: string email;
  2: string ssn;
}

I can't instantiate the Identity union. It feels like I should be able to use:

(thrift/->thrift (map->Identity {:ssn "21582"}))

but this throws the error "Not an optional field: email"

Does the library currently support unions? If so, I'd be happy to add documentation to the readme.

No protocol version header

Thanks for your great work!
I encountered a problem and not sure what is the reason. I am using :binary protocol. It works well with Clojure server and client. But when trying with thriftpy client, it throws the error: cybin.ProtocolError: No protocol version header.

Below is the python client code I am trying. It works with the Java thrift server, but not Clojure server with thrift-clj.

import thriftpy
from thriftpy.rpc import make_client
api_thrift = thriftpy.load("./idl/api.thrift", module_name="myapi_thrift")
api_service = make_client(api_thrift.AService, '192.168.0.2', 9011)
print api_service.ping()

ClassCastException: Cannot convert String to ByteBuffer

In thrift IDL I have a struct with binary field

struct A {
 1: binary field;
}

What is correct way to create this field on server side?

For example to create binary from int I use

(doto (ByteBuffer/allocate 4)
  (.putInt (int 3))
  (.array)))

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.