Ejemplo n.º 1
0
def mainController(cursor):
    # FileExporter is preparing a file for write
    exporter = predictionio.FileExporter(file_name="my_events.json")
    
    jsonSetItem(cursor, exporter)

    jsonSetUser(cursor, exporter)

    jsonUserViewItem(cursor, exporter)

    jsonUserBuyItem(cursor, exporter)
    
    # close the FileExporter when finish writing all events
    exporter.close()
    def test_export(self):
        app_info = subprocess.check_output(['pio', 'app', 'show', app_name])
        app_id = re.search('App ID: ([0-9]+)', app_info.decode('utf-8')).group(1)
        print(app_id)

        exporter = predictionio.FileExporter(file_name=filename)

        first_event_properties = {
            "prop1" : 1,
            "prop2" : "value2",
            "prop3" : [1, 2, 3],
            "prop4" : True,
            "prop5" : ["a", "b", "c"],
            "prop6" : 4.56 ,
            }
        first_event_time = datetime(
            2004, 12, 13, 21, 39, 45, 618000, pytz.timezone('US/Mountain'))
        exporter.create_event(
            event="my_event",
            entity_type="user",
            entity_id="uid",
            properties=first_event_properties,
            event_time=first_event_time,
            )

        # Second event
        second_event_properties = {
            "someProperty" : "value1",
            "anotherProperty" : "value2",
            }
        exporter.create_event(
            event="my_event",
            entity_type="user",
            entity_id="uid",
            target_entity_type="item",
            target_entity_id="iid",
            properties=second_event_properties,
            event_time=datetime(2014, 12, 13, 21, 38, 45, 618000, pytz.utc))

        exporter.close()

        subprocess.call(['pio', 'import', '--appid', app_id, '--input ', filename])
        sys.exit(1)

    logger.info(f'Accessing an access key')
    appid_cmd = f'{args.pio_cmd} app list'
    app_id = get_app_id(args)
    if len(app_id) == 0:
        logger.error('app_id is empty.')
        sys.exit(1)

    csv_file = f'{args.input_dir}/{args.csv_name}.csv'
    if not os.path.exists(csv_file):
        logger.error(f"{csv_file} is not found")
        sys.exit(1)

    logger.info(f'Loading {args.csv_name}.csv')
    exporter = predictionio.FileExporter(
        file_name=f'/tmp/{args.csv_name}.ndjson')
    df = pd.read_csv(csv_file, encoding="utf-8")
    for i, v in df.iterrows():
        event_response = exporter.create_event(
            event=f"{args.csv_name}",
            entity_type='data',
            entity_id=str(i),
            properties={x: str(v[x])
                        for x in df.columns})
    exporter.close()

    logger.info(f'Storing /tmp/{args.csv_name}.ndjson')
    subprocess.run(
        f'{args.pio_cmd} import --appid {app_id} --input /tmp/{args.csv_name}.ndjson'
        .split(' '))
Ejemplo n.º 4
0
    return (x, y)


def add_event(exporter, id, x, y):
    # Build event properties
    props = {}
    for i, xi in enumerate(x):
        props['x' + str(i)] = xi
    props['y'] = y

    exporter.create_event(event="$set",
                          entity_type="point",
                          entity_id=id,
                          properties=props)


if __name__ == '__main__':
    exporter = predictionio.FileExporter(
        file_name=EXPORTED_EVENTS_JSON_FILE_NAME)
    train_df = pd.read_csv(KAGGLE_HOUSE_PRICES_CSV_FILE_PATH)

    for (_, row) in train_df.iterrows():
        id = get_row_id(row)
        (x, y) = row_to_example(row)
        add_event(exporter, id, x, y)

    print('Exported event data to {}'.format(EXPORTED_EVENTS_JSON_FILE_NAME))

    exporter.close()
Ejemplo n.º 5
0
"""
Import sample data for recommendation engine
"""

import predictionio
from datetime import datetime
import pytz
import ujson
import mysql.connector

if __name__ == '__main__':
    exporter = predictionio.FileExporter(file_name="set_events.json")

    cnx = mysql.connector.connect(user='******',
                                  password='******',
                                  host='127.0.0.1',
                                  database='ren')

    cursor = cnx.cursor()

    query = ("SELECT hashid, info, url from product")
    cursor.execute(query)

    count = 0
    for (hashid, info, url) in cursor:
        info_string = ujson.loads(info)
        eid = info_string['Code']
        del info_string['Code']
        for key in info_string.keys():
            info_string[key] = [info_string[key]]
        event_properties = info_string
def new_exporter(file_name):
    return predictionio.FileExporter(file_name=file_name)
Ejemplo n.º 7
0
"""
Import sample data for recommendation engine
"""
import random
import predictionio
from datetime import datetime
import pytz
import ujson
import mysql.connector

if __name__ == '__main__':
    exporter = predictionio.FileExporter(file_name="hp_events.json")

    hp_list = [
        "9788498387568", "9788498383621", "9788700631625", "9780605039070",
        "9788884516374", "9780195799163"
    ]
    usr_hp = [1, 2, 3, 4, 5]
    count = 0
    for x in xrange(1, 100):
        exporter.create_event(
            event="view",
            entity_type="user",
            entity_id="l5;" + str(random.choice(usr_hp)),
            target_entity_type="item",
            target_entity_id="l5;" + random.choice(hp_list),
            event_time=datetime.utcnow().replace(tzinfo=pytz.utc))
        count = count + 1

    for x in xrange(1, 20):
        exporter.create_event(
Ejemplo n.º 8
0
 def _lazy_get_exporter_func(self, **kwargs):
     """kwargs must contain filename"""
     if self.exporter is None:
         self.filename = kwargs['filename']
         self.exporter = predictionio.FileExporter(file_name=self.filename)
     return self.exporter.create_event
Ejemplo n.º 9
0
                                        target_entity_type="item",
                                        target_entity_id=phrase,
                                        event_time=current_date)
                    #print "Event: " + SEARCH_EVENT + " entity_id: " + user_id + " target_entity_id: " + phrase + \
                    #      " current_date: " + current_date.isoformat()
                    current_date += event_time_increment
            count += 1
    f.close()
    print "%s lines are imported." % count


if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description="Import sample data for recommendation engine")
    parser.add_argument('--access_key', default='invald_access_key')
    parser.add_argument('--url', default="http://localhost:7070")
    parser.add_argument('--file', default="./data/sample-handmade-data.txt")
    parser.add_argument('--output', default="./data/ps-out.json")

    args = parser.parse_args()
    print args

    #    client = predictionio.EventClient(
    #        access_key=args.access_key,
    #        url=args.url,
    #        threads=5,
    #        qsize=500
    #    )
    client = predictionio.FileExporter(args.output)
    import_events(client, args.file)
import predictionio
from datetime import datetime
import pytz

exporter = predictionio.FileExporter(file_name="test.json")

first_event_properties = {
    "prop1": 1,
    "prop2": "value2",
    "prop3": [1, 2, 3],
    "prop4": True,
    "prop5": ["a", "b", "c"],
    "prop6": 4.56,
}
first_event_time = datetime(2004, 12, 13, 21, 39, 45, 618000,
                            pytz.timezone('US/Mountain'))
exporter.create_event(
    event="my_event",
    entity_type="user",
    entity_id="uid",
    properties=first_event_properties,
    event_time=first_event_time,
)

# Second event
second_event_properties = {
    "someProperty": "value1",
    "anotherProperty": "value2",
}
exporter.create_event(event="my_event",
                      entity_type="user",