簡體   English   中英

cassandra python“ item = val [i]”無法弄清楚

[英]cassandra python “ item = val[i] ” couldn't figure it out

我是cassandra python模塊的新手,所以我正在嘗試使用batchstatement將json文件插入表中,但是出現了'KeyError:0'錯誤,我知道所提供的文檔太多了。幾乎嘗試了一切,但不明白我的錯誤。請幫助!!!!!

import json
import logging
from cassandra.cluster import Cluster
import os
from uuid import uuid4
from cassandra.cluster import Cluster, BatchStatement
from cassandra import ConsistencyLevel
from myencoder import MyEncoder
import logging
import re
import ast


def parsing():

    with open('dfs.json', 'r', encoding="utf8") as json_file:
        data = json.load(json_file)
        aboutlegacy = data['aboutLegacy']
        accomplishments = data['accomplishments']
        profilealternative = data['profileAlternative']
        educations = data['educations']
        profileLegacy = data['profileLegacy']
        peopleAlsoviewed = data['peopleAlsoViewed']
        positions = data['positions']
        skills = data['skills']
        recommendations = data['recommendations']
        volunteerExperience = data['volunteerExperience']
        profile = data['profile']
        idd = uuid4()


        query = """
        INSERT INTO profile (id,profilelegacy,profilealternative,aboutlegacy,positions,educations,skills,recommendations,accomplishments,peoplealsoviewed,volunteerExperience,profile)
          VALUES (?,?,?,?,?,?,?,?,?,?,?,?);"""
        insert_user = session.prepare(query)
        batch = BatchStatement(consistency_level=ConsistencyLevel.ONE)

        batch.add(insert_user, (idd, profileLegacy, profilealternative, aboutlegacy, positions, educations,
                                skills, recommendations, accomplishments, peopleAlsoviewed, volunteerExperience, profile,))
        log = logging.getLogger()
        log.info('Batch Insert Completed')
        session.execute(batch)


if __name__ == "__main__":
    cluster = Cluster(['127.0.0.1'], port=9042)
    session = cluster.connect('profiles', wait_for_all_pools=True)
    session.execute('USE profiles')
    parsing()

這產生的錯誤:


File "cassandratest2.py", line 61, in <module>
    parsing()
  File "cassandratest2.py", line 51, in parsing
    skills, recommendations, accomplishments, peopleAlsoviewed, volunteerExperience, profile,))
  File "C:\Python\Python37\lib\site-packages\cassandra\query.py", line 815, in add
    bound_statement = statement.bind(() if parameters is None else parameters)
  File "C:\Python\Python37\lib\site-packages\cassandra\query.py", line 501, in bind
    return BoundStatement(self).bind(values)
  File "C:\Python\Python37\lib\site-packages\cassandra\query.py", line 627, in bind
    self.values.append(col_spec.type.serialize(value, proto_version))
  File "C:\Python\Python37\lib\site-packages\cassandra\cqltypes.py", line 723, in serialize
    return cls.serialize_safe(val, protocol_version)
  File "C:\Python\Python37\lib\site-packages\cassandra\cqltypes.py", line 942, in serialize_safe
    item = val[i]
KeyError: 0

這是json文件,只是格式,因為它包含機密信息


{
    "profileLegacy": {
        "name": "",
        "headline": "",
        "location": "",
        "connections": 0,
        "summary": ""
    },
    "profileAlternative": {
        "name": "",
        "headline": "",
        "location": "",
        "connections": 0
    },
    "aboutLegacy": {
        "text": ""
    },
    "positions": [{
        "org": "",
        "title": "",
        "end": "",
        "start": "",
        "desce": ""
    }],
    "educations": [{
        "major": "",
        "end": "",
        "name": "",
        "degree": "Maestr\u00eda en Finanzas",
        "start": "",
        "desce": ""
    }],
    "skills": [
        "Key Account Development",
        "Strategic Planning"
    ],
    "recommendations": {
        "givenCount": "0",
        "receivedCount": "0",
        "given": [],
        "received": []
    },
    "accomplishments": [],
    "peopleAlsoViewed": [{
        "url": "",
        "id": ""
    }],
    "volunteerExperience": [],
    "profile": {
        "name": "",
        "headline": "",
        "location": "",
        "connections": 0
    }
}  

這在cqlsh中創建表查詢

CREATE TABLE profile (
    id uuid PRIMARY KEY,
    profilelegacy frozen<profilelegacy>,
    profilealternative  frozen<profilelaternative>,
    aboutlegacy text,
    positions list<frozen<positions>>,
    educations set<frozen<educations>>,
    skills list<text>,
    recommendations frozen<recommendations>,
    accomplishments list<text>,
    peoplealsoviewed list<frozen<peoplealsoviewed>>,
    volunteerExperience list<text>,
    profile frozen<profilelaternative>
) WITH bloom_filter_fp_chance = 0.01
    AND caching = {'keys': 'ALL', 'rows_per_partition': 'NONE'}
    AND comment = ''
    AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold': '32', 'min_threshold': '4'}
    AND compression = {'chunk_length_in_kb': '64', 'class': 'org.apache.cassandra.io.compress.LZ4Compressor'}
    AND crc_check_chance = 1.0
    AND dclocal_read_repair_chance = 0.1
    AND default_time_to_live = 0
    AND gc_grace_seconds = 864000
    AND max_index_interval = 2048
    AND memtable_flush_period_in_ms = 0
    AND min_index_interval = 128
    AND read_repair_chance = 0.0
    AND speculative_retry = '99PERCENTILE';
and these are the declared types

CREATE TYPE profiles.peoplealsoviewed (
    url text,
    id text
);

CREATE TYPE profiles.profilelegacy (
    name text,
    headline text,
    location text,
    connections int,
    summary text
);

CREATE TYPE profiles.positions (
    org text,
    title text,
    end text,
    start text,
    desce text
);

CREATE TYPE profiles.recommendations (
    givencount text,
    receivedcount text,
    given frozen<list<text>>,
    received frozen<list<text>>
);

CREATE TYPE profiles.skills (
    title text,
    count text
);

CREATE TYPE profiles.educations (
    major text,
    end text,
    name text,
    degree text,
    start text,
    desce text
);

CREATE TYPE profiles.profilelaternative (
    name text,
    headline text,
    location text,
    connections int
);

當Cassandra嘗試根據用戶定義的類型進行填充時,它期望以與類型的字段相同的順序顯示值的tuple ,例如,對於profilelegacy ,它想要的(name, headline, location, connections, summary) 帶有屬性匹配的所討論的名字(例如,一些對象x為其是可以檢索x.name ,然后x.headline等)。

您正在加載一個JSON文件,該文件將JSON對象解碼為Python dict (使用基於鍵的查找),而不是Python對象(使用基於屬性的查找)。 您需要從dict轉換為適當的tuple或具有適當屬性的對象。

可能最簡單的方法是使用types.SimpleNamespace來使您從任意dict對象。 導入types並將json.load更改為類似以下內容:

data = json.load(json_file, object_hook=lambda d: types.SimpleNamespace(**d))

查找結果從dict -style更改為object-style:

aboutlegacy = data.aboutLegacy
accomplishments = data.accomplishments
# ... etc ...

應該使您更接近解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM