当前位置:网站首页>Figure seamless database integration tushare interface

Figure seamless database integration tushare interface

2022-06-11 07:49:00 Ma Chao's blog


Here’s the table of contents:

Figure seamless database integration Tushare Interface

     Use of third parties API, Help us quickly integrate data , Build the knowledge map data required for business analysis . This article mainly introduces how to Tushare HTTP Interface to graph database , And use Cypher Build a map of knowledge .

     Before we start Integration , Please make sure that your map database has APOC Components , And guarantee that apoc.load.jsonParams The process can be used normally .APOC It also supports JSON Path, That is to search in a specific mode JSON Data items in the document and return their contents , The concept is similar to that applied to XML Of XPath And apply to HTML Of jQuery. You can view more usage methods Neo4j An article by Dr. Yu, a community technology expert Neo4j Figure database advanced application series / Server extension guide APOC(5.5) - Import JSON data .

One 、Tushare Introduce

     Tushare It's a free one 、 Open source python Financial data interface package . It is mainly used to collect financial data such as stocks from data 、 The process from cleaning and processing to data storage , Be able to provide financial analysts with quick 、 Clean and tidy 、 And a variety of easy to analyze data , Greatly reduce their workload in data acquisition , Make them more focused on the research and implementation of strategies and models .

Tushare Official website

Two 、 Integrate Tushare Interface

     Before starting integration, make sure you have a Tushare Access account for .
     Find... In the installation directory of the diagram database conf Folder , And in neo4j.conf In the file Tushare HTTP API Of URL Define an alias , Add a new line of configuration . After modifying the configuration , Restart the database service .

apoc.json.tushare.url=http://api.tushare.pro

3、 ... and 、 Use interface data

     Now we can write Cypher Code can be easily accessed from Tushare Got the data . Next, I will demonstrate a case of the construction of shenwancheng's stock sharing map . Please pay attention to the use of Cypher Please set private when scripting token.

     The following code first calls from... Through a batch loop stock_basic Interface to get the stock code , Then use the stock code to obtain the time series data of shenwancheng shares . Each stock code calls index_member Before the interface , It is set to perform four million addition operations to indicate the delay 1~2 second , The purpose of this operation is to ensure that HTTP Do not exceed the interface frequency limit when calling interfaces .

  • stock_basic Interface
    stock_basic Interface

  • index_member Interface
    index_member Interface

// Cycle through all stock codes 
WITH RANGE(1,10) AS list,1000 AS limit
UNWIND list AS num
WITH num*limit AS offset,limit
// Get stock code in batches 
WITH 
    '{"api_name":"stock_basic","token":"xxxxxxxxxxx","params":{"limit":"'+limit+'","offset":"'+offset+'"},"fields":"ts_code"}' AS payload
CALL apoc.load.jsonParams(
    'tushare',
    NULL,
    payload,
    NULL,
    {}) yield value
WITH value.data.has_more AS has_more,value.data.items AS stocks
WHERE has_more
// Obtain Shenwan ingredient data 
WITH has_more,stocks
UNWIND stocks AS stock
WITH stock
// Delayed execution 【HTTP API There is a limit on the frequency of calls , Let the function perform four million additions , It takes about 1~2 second 】
WITH RANGE(1,2000) AS l,stock UNWIND l AS a UNWIND l AS b WITH SUM(a+b) AS delay,stock
WITH 
    '{"api_name":"index_member","token":"xxxxxxxxxxx","params":{"ts_code":"'+stock[0]+'"},"fields":"index_code,index_name,con_code,con_name,in_date,out_date,is_new"}' AS payload
CALL apoc.load.jsonParams(
    'tushare',
    NULL,
    payload,
    NULL,
    {}) yield value
WITH value.data.items AS items
// Build the component stock map of Shenwan industry 
UNWIND items AS item
WITH item
MERGE (stk: Stocks  {code:item[2]}) SET stk+={name:item[3]+'('+item[2]+')'}
MERGE (hy: Shenwan industry  {code:item[0]}) SET hy+={name:item[1]+'('+item[0]+')'}
CREATE (stk)-[r: Belong to ]->(hy) SET r+={in_date:item[4],out_date:item[5],is_new:item[6]}

 Time series of Shenwan industry constituent stocks

原网站

版权声明
本文为[Ma Chao's blog]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110743509273.html