morningman commented on a change in pull request #3230: Support load json-data into Doris by RoutineLoad or StreamLoad URL: https://github.com/apache/incubator-doris/pull/3230#discussion_r410713273
########## File path: be/src/exec/json_scanner.cpp ########## @@ -0,0 +1,485 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + + +#include "exec/json_scanner.h" + +#include "gutil/strings/split.h" +#include "runtime/exec_env.h" +#include "runtime/mem_tracker.h" +#include "runtime/raw_value.h" +#include "runtime/runtime_state.h" +#include "exprs/expr.h" +#include "env/env.h" +#include "exec/local_file_reader.h" +#include "exec/broker_reader.h" +#include "exprs/json_functions.h" + +namespace doris { + +JsonScanner::JsonScanner(RuntimeState* state, + RuntimeProfile* profile, + const TBrokerScanRangeParams& params, + const std::vector<TBrokerRangeDesc>& ranges, + const std::vector<TNetworkAddress>& broker_addresses, + ScannerCounter* counter) : BaseScanner(state, profile, params, counter), + _ranges(ranges), + _broker_addresses(broker_addresses), + _cur_file_reader(nullptr), + _next_range(0), + _cur_file_eof(false), + _scanner_eof(false) { + +} + +JsonScanner::~JsonScanner() { + close(); +} + +Status JsonScanner::open() { + return BaseScanner::open(); +} + +Status JsonScanner::get_next(Tuple* tuple, MemPool* tuple_pool, bool* eof) { + SCOPED_TIMER(_read_timer); + // Get one line + while (!_scanner_eof) { + if (_cur_file_reader == nullptr || _cur_file_eof) { + RETURN_IF_ERROR(open_next_reader()); + // If there isn't any more reader, break this + if (_scanner_eof) { + continue; + } + _cur_file_eof = false; + } + RETURN_IF_ERROR(_cur_file_reader->read(_src_tuple, _src_slot_descs, tuple_pool, &_cur_file_eof)); + + if (_cur_file_eof) { + continue; // read next file + } + COUNTER_UPDATE(_rows_read_counter, 1); + SCOPED_TIMER(_materialize_timer); + if (fill_dest_tuple(Slice(), tuple, tuple_pool)) { + break;// break if true + } + } + if (_scanner_eof) { + *eof = true; + } else { + *eof = false; + } + return Status::OK(); +} + +Status JsonScanner::open_next_reader() { + if (_cur_file_reader != nullptr) { + delete _cur_file_reader; + _cur_file_reader = nullptr; + if (_stream_load_pipe != nullptr) { + _stream_load_pipe.reset(); + } + } + if (_next_range >= _ranges.size()) { + _scanner_eof = true; + return Status::OK(); + } + const TBrokerRangeDesc& range = _ranges[_next_range++]; + int64_t start_offset = range.start_offset; + if (start_offset != 0) { + start_offset -= 1; + } + FileReader *file = NULL; + switch (range.file_type) { + case TFileType::FILE_LOCAL: { + LocalFileReader* file_reader = new LocalFileReader(range.path, start_offset); + RETURN_IF_ERROR(file_reader->open()); + file = file_reader; + break; + } + case TFileType::FILE_BROKER: { + BrokerReader* broker_reader = new BrokerReader( + _state->exec_env(), _broker_addresses, _params.properties, range.path, start_offset); + RETURN_IF_ERROR(broker_reader->open()); + file = broker_reader; + break; + } + + case TFileType::FILE_STREAM: { + _stream_load_pipe = _state->exec_env()->load_stream_mgr()->get(range.load_id); + if (_stream_load_pipe == nullptr) { + VLOG(3) << "unknown stream load id: " << UniqueId(range.load_id); + return Status::InternalError("unknown stream load id"); + } + file = _stream_load_pipe.get(); + break; + } + default: { + std::stringstream ss; + ss << "Unknown file type, type=" << range.file_type; + return Status::InternalError(ss.str()); + } + } + + std::string jsonpath = ""; + std::string jsonpath_file = ""; + if (range.__isset.jsonpath) { + jsonpath = range.jsonpath; + } else if (range.__isset.jsonpath_file) { + jsonpath_file = range.jsonpath_file; + } + _cur_file_reader = new JsonReader(_state->exec_env()->small_file_mgr(), _profile, file, jsonpath, jsonpath_file); + + return Status::OK(); +} + +void JsonScanner::close() { + if (_cur_file_reader != nullptr) { + delete _cur_file_reader; + _cur_file_reader = nullptr; + if (_stream_load_pipe != nullptr) { + _stream_load_pipe.reset(); + } + } +} + +////// class JsonDataInternal +JsonDataInternal::JsonDataInternal(rapidjson::Value* v) : + _jsonValues(v), _iterator(v->Begin()) { +} + +JsonDataInternal::~JsonDataInternal() { + +} +bool JsonDataInternal::isEnd() { + return _jsonValues->End() == _iterator; +} + +rapidjson::Value::ConstValueIterator JsonDataInternal::getNext() { + if (isEnd()) { + return nullptr; + } + return _iterator++; +} + + +////// class JsonReader +JsonReader::JsonReader( + SmallFileMgr *fileMgr, + RuntimeProfile* profile, + FileReader* file_reader, + std::string& jsonpath, + std::string& jsonpath_file) : + _next_line(0), + _total_lines(0), + _profile(profile), + _file_reader(file_reader), + _closed(false) { + _bytes_read_counter = ADD_COUNTER(_profile, "BytesRead", TUnit::BYTES); + _read_timer = ADD_TIMER(_profile, "FileReadTime"); + + //parse jsonpath + if (!jsonpath.empty()) { + if (!_jsonPathDoc.Parse(jsonpath.c_str()).HasParseError()) { + if (!_jsonPathDoc.HasMember("jsonpath") || !_jsonPathDoc["jsonpath"].IsArray()) { + _parseJsonPathFlag = -1;// failed, has none object + } else { + _parseJsonPathFlag = 1;// success + } + } else { + _parseJsonPathFlag = -1;// parse failed + } + } else if (!jsonpath_file.empty()) { + //Read jsonpath from file, has format: file_id:md5 + _parseJsonPathFlag = parseJsonPathFromFile(fileMgr, jsonpath_file); + } else { + _parseJsonPathFlag = 0; + } +} + +JsonReader::~JsonReader() { + close(); +} + +void JsonReader::close() { + if (_closed) { + return; + } + if (typeid(*_file_reader) == typeid(doris::BrokerReader) || typeid(*_file_reader) == typeid(doris::LocalFileReader)) { + _file_reader->close(); + delete _file_reader; + } + _closed = true; +} + +int JsonReader::parseJsonPathFromFile(SmallFileMgr *smallFileMgr, std::string& fileinfo ) { + std::vector<std::string> parts = strings::Split(fileinfo, ":", strings::SkipWhitespace()); + if (parts.size() != 2) { + LOG(WARNING)<< "parseJsonPathFromFile Invalid fileinfo: " << fileinfo; + return -1; + } + int64_t file_id = std::stol(parts[0]); + std::string file_path; + Status st = smallFileMgr->get_file(file_id, parts[1], &file_path); + if (!st.ok()) { + return -1; + } + std::unique_ptr<RandomAccessFile> jsonPathFile; + st = Env::Default()->new_random_access_file(file_path, &jsonPathFile); + if (!st.ok()) { + return -1; + } + uint64_t size = 0; + jsonPathFile->size(&size); + if (size == 0) { + return 0; + } + boost::scoped_array<char> pBuf(new char[size]); + Slice slice(pBuf.get(), size); + st = jsonPathFile->read_at(0, slice); + if (!st.ok()) { + return -1; + } + + if (!_jsonPathDoc.Parse(slice.get_data()).HasParseError()) { + if (!_jsonPathDoc.HasMember("jsonpath") || !_jsonPathDoc["jsonpath"].IsArray()) { + return -1;//failed, has none object + } else { + return 1;// success + } + } else { + return -1;// parse failed + } +} + +Status JsonReader::parseJsonDoc(bool *eof) { + // read all, must be delete json_str + uint8_t* json_str = nullptr; // + size_t length = 0; + RETURN_IF_ERROR(_file_reader->read(&json_str, &length)); + if (length == 0) { + *eof = true; + return Status::OK(); + } + // parse jsondata to JsonDoc + if (_jsonDoc.Parse((char*)json_str, length).HasParseError()) { + delete[] json_str; + return Status::InternalError("Parse json data for JsonDoc is failed."); + } + delete[] json_str; + return Status::OK(); +} + +size_t JsonReader::getDataByJsonPath() { Review comment: In c language code style, you should define this method as: ``` Status JsonReader::get_data_by_jsonpath(size_t* max_lines) { } ``` ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org