Preparations

Updated at:
Copy as MD

Before you use MaxFrame, prepare the runtime environment. You can access MaxFrame by using the standard MaxFrame SDK (local client) or a DataWorks node.

Prerequisites

Step 1: Activate MaxFrame

MaxFrame is in invitational preview. Scan the following QR code with DingTalk to join the MaxFrame user support group and contact technical support to activate MaxFrame.

image

Step 2: Install and access MaxFrame

You can access MaxFrame by using a local client or a MaxFrame node in Dataworks.

Example description: The following example creates a MaxCompute table, uses MaxFrame to process data, and stores the results in another MaxCompute table.

Access MaxFrame from a local client

  1. In a system CLI, such as the Windows Command Prompt, run the pip command to install the MaxFrame client. Sample command:

    pip install maxframe
  2. Create a MaxFrame session.

    1. Create a .py file, such as test.py, copy the following sample code to the test.py file, and then save the file.

      import os
      import maxframe.dataframe as md
      from odps import ODPS
      from maxframe import new_session
      
      # Create a MaxCompute entry.
      o = ODPS(
          # Set the environment variable ALIBABA_CLOUD_ACCESS_KEY_ID to the AccessKey ID of your Alibaba Cloud account. 
          # Set the environment variable ALIBABA_CLOUD_ACCESS_KEY_SECRET to the AccessKey secret of your Alibaba Cloud account. 
          # We recommend that you do not directly use the actual AccessKey ID and AccessKey secret. 
          os.getenv('ALIBABA_CLOUD_ACCESS_KEY_ID'),
          os.getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET'),
          project='your-default-project',
          endpoint='your-end-point',
      )
      table = o.create_table("test_source_table", "a string, b bigint", if_not_exists=True)
      with table.open_writer() as writer:
          writer.write([
              ["value1", 0],
              ["value2", 1],
          ])
      
      # Create a MaxFrame session.
      session = new_session(o)
      
      df = md.read_odps_table("test_source_table",index_col="b")
      df["a"] = "prefix_" + df["a"]
      
      # Print DataFrame data.
      print(df.execute().fetch())
      
      # Write data from MaxFrame DataFrame to a MaxCompute table.
      md.to_odps_table(df, "test_prefix_source_table").execute()
      
      # Destroy the MaxFrame session.
      session.destroy()

      Parameters:

      • ALIBABA_CLOUD_ACCESS_KEY_ID: The AccessKey ID of the Alibaba Cloud account that has the MaxCompute permissions on the objects that you want to manage in your MaxCompute project. You can obtain the AccessKey ID on the AccessKey Pair page in the Alibaba Cloud Management Console.

      • ALIBABA_CLOUD_ACCESS_KEY_SECRET: The AccessKey secret that corresponds to the AccessKey ID.

      • your-default-project: The name of your MaxCompute project. Log on to the MaxCompute console and choose Workspace > Projects in the left-side navigation pane to view the project name.

      • your_end_point: The endpoint of the region in which your MaxCompute project resides. For more information, see Endpoints.

    2. Go to the path of the test.py file and run the file. Sample command:

      python test.py

Access MaxFrame by using DataWorks

DataWorks integrates MaxFrame with PyODPS 3 nodes, providing task scheduling capabilities. You can use PyODPS 3 nodes to develop and run MaxFrame jobs.

  1. Create a PyODPS 3 node.

    You can create a PyODPS 3 node on the DataStudio page of the DataWorks console. For more information, see Develop a PyODPS 3 task.

  2. Create a MaxFrame session.

    PyODPS 3 integrates MaxCompute user and project information, so you can create MaxFrame sessions directly. Sample code:

    import maxframe.dataframe as md
    from maxframe import new_session
    from maxframe.config import options
    options.sql.enable_mcqa = False
    
    table = o.create_table("test_source_table", "a string, b bigint", if_not_exists=True)
    with table.open_writer() as writer:
        writer.write([
            ["value1", 0],
            ["value2", 1],
        ])
    
    # Create a MaxFrame session.
    session = new_session(o)
    
    df = md.read_odps_table("test_source_table",index_col="b")
    df["a"] = "prefix_" + df["a"]
    
    # Print DataFrame data.
    print(df.execute().fetch())
    
    # Write data from MaxFrame DataFrame to a MaxCompute table.
    md.to_odps_table(df, "test_prefix_source_table").execute()
    
    # Destroy the MaxFrame session.
    session.destroy()
    

Step 3: Verify the result

Run the Python code and query data in the result table. If the result meets your expectation, MaxFrame is installed and connected to MaxCompute.

  1. Run the Python code. The following result is returned:

    b           a
    0  prefix_value1
    1  prefix_value2
  2. Execute the following SQL statement in the MaxCompute project to query data in the test_prefix_source_table table:

    SELECT * FROM test_prefix_source_table;

    The following result is returned:

    +------------+------------+
    | b          | a          |
    +------------+------------+
    | 0          | prefix_value1 |
    | 1          | prefix_value2 |
    +------------+------------+