论坛首页 编程语言技术论坛

Thrift--JavaServer&PythonClient遇到的问题

浏览 1100 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2013-09-23  

thrift在java中的应用参考:http://hanqunfeng.iteye.com/blog/1936556

thrift在python中的应用参考:http://hanqunfeng.iteye.com/blog/1945208

 

Contact.thrift

 

namespace java thrift.service  
  
struct Book{
	1:i32 id
	2:string name
}

struct Contact{  
	1:i32 id  
	2:string name  
	3:i64 birthday  
	4:string phoneNo  
	5:string ipAddress  
	6:map<string,string> props  
	7:double salary
	8:set<Book> books
   
}  
  
service ContactManager{  
	void save(1:Contact contact)  
	void remove(1:i32 id)  
	list<Contact> getAll();  
	list<Contact> query(1:map<string,string> conditions)  
}  

 

 

生成java文件:

 

thrift  --gen java Contact.thrift

 生成python文件:

 

thrift  --gen py Contact.thrift

 

java服务器端参考:http://hanqunfeng.iteye.com/blog/1936556

package thrift.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.thrift.TException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import thrift.dao.ContactManagerDao;
import thrift.service.Contact;
import thrift.service.ContactManager;
@Service
public class ContactManagerImpl implements ContactManager.Iface{
	
        private static Logger logger = Logger.getLogger(ContactManagerImpl.class);
	@Override
	public void save(Contact contact) throws TException {
		// TODO Auto-generated method stub
		logger.info("ContactManagerDao save run");
	}

	@Override
	public void remove(int id) throws TException {
		// TODO Auto-generated method stub
		logger.info("ContactManagerDao remove run");
	}

	@Override
	public List<Contact> getAll() throws TException {
		// TODO Auto-generated method stub
		logger.info("ContactManagerDao getAll run");
		List<Contact> list = new ArrayList<Contact>();
		Book book = new Book();
		book.setId(10);
		book.setName("西游记");
		Book book2 = new Book();
		book2.setId(11);
		book2.setName("水浒传");
		Contact contract = new Contact();
		contract.setBirthday(100000000);
		contract.setId(101);
		contract.setIpAddress("192.168.0.101");
		contract.setName("超人");
		contract.setPhoneNo("13312345678");
		contract.setSalary(98456.36);
		Set<Book> set = new HashSet<Book>();
		set.add(book);
		set.add(book2);
		contract.setBooks(set);
		list.add(contract);
		logger.info(list.toString());
		return list;
	}

	@Override
	public List<Contact> query(Map<String, String> conditions)
			throws TException {
		// TODO Auto-generated method stub
		logger.info("ContactManagerDao query run");
		logger.info(conditions.toString());
		List<Contact> list = new ArrayList<Contact>();
		Contact contract = new Contact();
		contract.setBirthday(100000000);
		contract.setId(101);
		contract.setIpAddress("192.168.0.101");
		contract.setName("超人");
		contract.setPhoneNo("13312345678");
		contract.setSalary(98456.36);
		list.add(contract);
		
		Contact contract2 = new Contact();
		contract2.setBirthday(200000000);
		contract2.setId(101);
		contract2.setIpAddress("192.168.0.102");
		contract2.setName("超人2");
		contract2.setPhoneNo("13312345672");
		contract2.setSalary(98456.32);
		list.add(contract2);
		return list;
	}

}

 

python客户端参考:http://hanqunfeng.iteye.com/blog/1945208

# coding=utf-8
'''
Created on 2013-9-22

@author: hanqunfeng
'''

import sys
sys.path.append('../') #导入上下文环境

from servicePy import ContactManager
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
# from thrift.protocol import TBinaryProtocol
from thrift.protocol import TCompactProtocol
def hiveExe():
    try:
        transport = TSocket.TSocket('127.0.0.1', 7912) 
        transport = TTransport.TBufferedTransport(transport)
#         protocol = TBinaryProtocol.TBinaryProtocol(transport)
        protocol = TCompactProtocol.TCompactProtocol(transport)
        client = ContactManager.Client(protocol)
        transport.open()
        print "The return value is : " 
        print client.remove(12)
        list = client.query({'a':'韩群峰','b':'hello world'})
        print list
        for m in list:
            print m
            print m.salary
            print m.name
            print m.birthday
        
        print "............"
        transport.close()
    except Thrift.TException, tx:
        print '%s' % (tx.message)
if __name__ == '__main__':
    hiveExe()

 

 

问题:

这里,客户端调用query()方法,会正常得到输出:

 

[Contact(salary=1.900218491772795e-109, name='\xe8\xb6\x85\xe4\xba\xba', birthday=100000000, books=None, props=None, ipAddress='192.168.0.101', id=101, phoneNo='13312345678'), Contact(salary=-5.965150887436158e+213, name='\xe8\xb6\x85\xe4\xba\xba2', birthday=200000000, books=None, props=None, ipAddress='192.168.0.102', id=101, phoneNo='13312345672')]

 

但是,如果客户端调用getAll()方法会抛异常,TypeError: unhashable instance,提示类型不能转换,所以形如:

[Contact(id:101, name:超人, birthday:100000000, phoneNo:13312345678, ipAddress:192.168.0.101, props:null, salary:98456.36, books:[Book(id:11, name:水浒传), Book(id:10, name:西游记)])]

这样复杂的数据类型,python并不支持。

 

 

论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics