2015-01-02 2 views
2

자바를 사용하여 읽을 수있는 gtfs 실시간 데이터 (프로토콜 버퍼 형식)를 다운로드하려고하므로 텍스트 파일로 볼 수 있습니다.gtfs 실시간 데이터를 사람이 읽을 수있는 형식으로 스트리밍

접근 # 1 :

나는 방법의 몇 가지 시도

URL url = new URL(uri); 
byte[] buffer = new byte[4096]; 
InputStream is = url.openStream(); 
byte[] buffer = new byte[4096]; 
InputStream is = url.openStream(); 
File file = new File("c:/protobuf_data.txt"); 
OutputStream output = new FileOutputStream(file); 
int numOfBytesReadIntoBuffer = -1; 
while((numOfBytesReadIntoBuffer = is.read(buffer)) != -1){ 
    output.write(buffer, 0, numOfBytesReadIntoBuffer); 
} 

results (snippet): 
099700_L..S20150102*LÊ>0L 1637 8AV/RPY!¯¬œ¥¾¬œ¥"L22S(

접근 # 2 (접근 번호와 같은 동일한 결과를 1) : 수입 org.apache.commons.io.IOUtils;

URL url = new URL(uri); 
InputStream is = url.openStream(); 
File file = new File("c:/protobuf_data.txt"); 
OutputStream output = new FileOutputStream(file); 
byte[] bytes = IOUtils.toByteArray(is); 
output.write(bytes); 

같은 방식으로 OutputStream에 모두 쓰기 때문에 결과는 같습니다. When using google protocol buffers to transfer String character,got messy code

내가 프로토콜 버퍼 문서를 읽을 수 있지만 좀 더 혼란 가지고 :

는 또한 제안은 여기 있지만 난 그냥 점점 오류를 종료했습니다. https://developers.google.com/protocol-buffers/docs/encoding

com.sun.org.apache.xml.internal.security.utils.Base64를 사용했지만 오류가 발생합니다. 접근법 # 3

URL url = new URL(uri); 
InputStream is = url.openStream(); 

File file = new File("c:/users/Workstation/protobuf_data_bytes.txt"); 

OutputStream output = new FileOutputStream(file); 

byte[] bytes = IOUtils.toByteArray(is); 
Init.init(); 
byte[] decoded_bytes = Base64.decode(bytes); 

error: 
Exception in thread "main" com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException: Error while decoding 

I는 Base64로 인코딩 된 바이트 스트림을 디코딩하기위한 InputStream를 만들 java.util.Base64의 포장 방법을 이용하려고하지만, 데이터는 단지 더 혼용 얻었다.

+0

프로토콜 버퍼는 바이너리 메시지 형식이며 .proto 파일은 읽고있는 메시지의 필드와 데이터 형식을 설명합니다. https://developers.google.com/transit/gtfs-realtime/gtfs-realtime-proto의 GTFS 프로토콜 메시지 정의 파일이 필요합니다.이 파일은 GTFS 메시지를 인코딩하고 디코딩하는 방법을 설명합니다 (내가 GTFS의 올바른 의미를 짐작할 수 있습니다.) 프로토콜 버퍼 컴파일러 https://developers.google.com/protocol-buffers/docs/downloads도 필요합니다.이 파일은 .proto 파일을 입력으로 사용하고 해당 메시지를 인코딩하고 디코딩 할 Java 클래스를 생성합니다. –

답변

1

는 GTFS 실시간 스펙은 이제 다양한 언어로 GTFS 실시간 데이터를 구문 분석 코드 샘플을 포함

https://developers.google.com/transit/gtfs-realtime/code-samples

그것에서 GTFS 실시간 데이터 분석에 관해서 시작하기에 좋은 장소 좋아하는 언어.

+0

이 링크는 더 이상 작동하지 않습니다. ( –

+0

이 링크는 https://developers.google.com/transit/gtfs-realtime/examples/ – Dung

1

언급했듯이 gtfs- 실시간 파일은 바이너리이며 특수 프로토콜 버퍼 컴파일 코드가 필요합니다. Base64 디코딩으로 어디에도 가지 않을 것입니다.


대체 방법 (명령 줄 + JSON) : 한 사람이 읽을 수있는 형식으로 GTFS 실시간 파일을 구문 분석을 시도하는 경우

그러나는, 내가 그 변환하는 독립형 도구를 썼다 JSON에 GTFS 실시간 :

https://github.com/harrytruong/gtfs_realtime_json 그냥 (아무 설치)를 다운로드하지 않고, 실행 gtfs_realtime_json <feed_url>

는 여기 sample JSON output입니다.

+0

안녕하세요, Mac OS X 10.9.5에서 독립형 유틸리티를 사용해 보았습니다. got-bash : ./gtfs_realtime_json : 바이너리 파일을 실행할 수 없습니다. –

관련 문제